Datatype in JavaScript

Datatype in JavaScript

Datatype in JavaScript

Datatype in JavaScript
Datatype in JavaScript

 

Hey Learners,

Welcome to the flight!  Where are we going?  Well, today we’ll explore the Javascript datatype.  So, fasten your seat and belts and get ready for the trip.
 
Data type javascript | easy explanation - codewithrandom
image credit edureka

 

So, before taking takeoff you must be wondering with a bundle of questions that, what is this and why it is.  So don’t worry and stay tuned with me because I’m going to discuss it here. 
 
Prerequisite ~ You should know basic javascript and variables.
If you wish to learn these things then you can search these topics in Codewithrandom blogs.
Let’s hustle ourselves quickly 🏃‍♂️🏃‍♀️
 
If you are learning front-end or backend you may know or you should know that only with javascript we can play for both sides. These are small things that make Javascript a dynamic language.

DATA TYPE

The data type is used to identify which type of data can be held and manipulated. This is a very common definition in almost every language.
Example – int p, here p is defined by int,  that p is integer datatype.
                    const pi = 3.14, here pi is defined as constant datatype this is commonly used in javascript when we don’t want to change the value of our data.
As Javascript is a dynamically treated language it is so due to lots of reasons. One of them is that the same variable can be used to store the different data
//Javascript dynamically behaviour  
 var sum = 35; // can store an integer   
 var name = 'code'; // can also store a string.  
 var logic = true; // can also store a boolean.

As we are learning a newer version of  Javascript ES6. So in this generally we have 7 defined Data Types. We will learn one by one.

  • Numbers: 8,5,9.5 etc.
  • String: “Hello Learners”, “Codewithrandom” etc.
  • Boolean: It deals with the logical operation and it has two values: true or false.
  • Null: This type has only one value: null.
  • Undefined: When we don’t assign any value to our variable then it holds a value of  Undefined.
  • Symbol  – That represents a unique value.
  • Object: It is the most important data type and it is the basic element for building the OOPS concept in Javascript.
 

The Type of Operator

You can use the Javascript type of operator to find the type of a Javascript variable.
The typeof operator returns the type of a variable or an expression.
type of("Code wave"); //string
type of(5); //number

typeof (true); //Boolean

Let x;

typeof (x); //undefined

Here are a few examples of Type of use.

A NUMBER

It is a data type that numbers (Numbers in the sense both integer and floating). It also represents some characters such as Infinity and NaN.
// the Number Datatype in Javascript dynamically behaviour.

let num = 6; // integer

console.log(typeof(num));

let num2 = 3.14; // floating-point number

console.log(typeof(num2));

let num3 = Infinity; // Infinity

console.log(typeof(num3)); the

let num4 = 'something here too'/2; // NaN

console.log(typeof(num4));

Simply by this code, we can check the number data type with the type of operator.

Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)

String: 

It is a group of characters that is covered up by quotes. In Javascript, there are three types of quotes.
let str = "Hello world";
let str2 = 'Somewhere in the world, it gets hard to spend the day in winter ';
let phrase = `printing ${str} program in JS`;
console.log(phrase);

So, by the above code, we can see there are three types of quotes.

1st one is ” ” it is a double quote simply we use it for quotation in a general way but here we use it to enclose string characters.
In the second code we have a single inverted comma, ‘ ‘ It is used when we have to use the double quotation inside of any statement.
* In Js or even in other of the languages we can’t use a double quotation as;
    ” “ALEXA”  Hii! welcome to the central bureau. Please verify yourself.”
so for resolving this we have a single quote.
3rd quote is string literal, we use it when we have to target any key element of JS inside of the quotation as you refer from the last code example.

50+ HTML, CSS & JavaScript Projects With Source Code

A Boolean

It is generally used for logical interpretation as it has only two values: true or false. It stores the value of Yes means true or No means false.
console.log("These are the examples of Boolean Datatype");
console.log(10>9);
console.log("str"==10);
console.log(5!=4);

NULL 

The ‘null’ data type defines a special data type that represents nothing.  We give it to the variable when we don’t want to assign a value to the variable.
console.log("These are the examples of NULL Datatype");
Let name = null;
Const fixed quantity = null;

From the above code, you are observing that we have declared the variable and didn’t give any value to it may be for future use. But for keeping ourselves on the safe side, we declare the variable type is null.

Undefined

It is the same as a NULL data type in the sense of meaning, but the only difference is that Undefined datatype is the defaults data type which is introduced by Js Engin by itself at the time when we create a variable and we don’t assign any value to it. That time the same variable stores any garbage value.
//few examples of Undefined data type
let x;  
console.log(x); // undefined  
Var sum;  
console.log(sum);//undefined

Symbol

This data type is introduced by the new Javascript version ES6. A “symbol” represents a unique identifier means this is immutable.
A value of this data type can be created by using Symbol();
// id is a new symbol
let id = Symbol();
console.log(typeof(id));
// id is a symbol with the description "id"
let batched = Symbol("Charleston 2307");
console.log(batchid);

As it is part of Javascript datatype so we are discussing it here but it is less in use.

Object

This is only a non-primitive datatype in Javascript. As I have discussed this is the building block for the OOPS concept. That’s why it is very important in javascript and why we should have a good understanding of this. So, without wasting any single second let’s directly dive into its concepts.
Objects are used to store keyed collection of various datatype and more complex entities.
We can create objects in multiple ways. one is by object literals {} in which we store key-value pair. Another way is by using an object constructor which is with new. 
// an object
let car = {
name: "Audi", // by key "name" store value "Audi"
manufacturer year: 2021, // by key "year" store value 2021
colour: "purple-black" // by key "colour" store value purple-black.
};

This is how we can create an object with the help of object literals. This is widely used in Javascript.

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

Hereby this code, if you want to access any properties of this “car” object. So we can simply access it by just keeping this syntax as an object name. property.
For Example car.colour;
 
Finally………. we have reached the destination. 
By this blog, we have learnt all the Datatype of Javascript from scratch. we looked at the example of each datatype. Their behaviour is the way of using it. Now I’m looking for your reviews.
So, How was the trip 😁, Learners!
I hope you have learnt something new from this blog. If you faced any difficulty feel free to drop a comment down your problems and if you liked it, please show your love in the comment section. This fills blogger hearts with enthusiasm for writing more and new blogs.
You can follow me on Instagram 
Written by @Ankit kumar


Leave a Reply