JavaScript Data Types
The Term Data Types refers to the Types of Data that you can use in Javascript.
For Example :
"Hello World" is a String Data Types
10 is an Integer Data Type.
25.7 is a Float Data Type.
Let's See Them in Detail
Arbitrary Position Represents that calculations are performed on numbers whose digits of precision are limited only by the available memory of the Parent System.
Primitive Data Type Only Stores Single Value ex: Integer whereas, Non-Primitive Data Type Stores Collection of Data example: Object.
String
Introduction
String is a Data Type that is used to store storing and manipulating text.
Strings are written inside Double Quotes or Single Quotes.
Example :
"hello" , "Hey There" , "Any Text" , "123", "World123"
Any Thing you write inside Double Quotes is considered to be a Text by Javascript.
Syntax
var str = "Hello World";
console.log(str);
Here,
It will Print "Hello World" in Console Tab.
Numbers
Introduction
Number is a Data Type that is used to store Integers or Floating Point (decimals and exponentials).
Example :
55 , 500 , 9999 , 999.99 ,3e-2 etc
Syntax
var myNum = 998.79;
console.log(myNum);
Here,
It will Print 998.79 in Console Tab.
Note Numbers Data Type can only Represent Numbers Less Than (253 - 1) and more than -(253 - 1).
If you Want to type, Number Greater than (253 - 1) or Smaller Than -(253-1) then you have to use BigInt
A BigInt number is created by appending n to the end of an integer.
Example :
99975389745896468469n , 84546984290051247n etc.
var myBigNo = 84546984290051247n;
console.log(myBigNo);
Here,
It will Print 84546984290051247n in the Console Tab
If you do not add n at the end of number then Javascript will Consider it a Normal Number. Thus, It will not be able to Read that number and It will Output the Wrong Number.
Boolean
Boolean is used to represent True and False. If any Statement of Expression is True Boolean will Result True otherwise It will Result False, you can also Create a Variable and Store Its Value to Be True or False.
Example
var MyTask = True;
console.log(MyTask);
Here,
It will Output True in the Console Tab.
Object
An Object is a Non-Primitive Data Type which is used to Store Collection of Data.
Example:
var identity = {name: 'john',age: 20};
console.log(identity.name);
console.log(identity.age);
Here,
It will Output the name i.e "john" and age i.e. 20.
You will See Objects in Details in Later Sections.
Symbol
It is Introduced in the New ES6 Version of Javascript.
It is an Immutable (Cannot Be Changed) and Unique Primitive Data Type.
Example: If You have declared "Hello World" Value to 2 Different Variables then, Both Variables Contain the Same Value. But According To Javascript These are Not the Same Because They are using Symbol Datatype that is Unique.
var x = Symbol("Hello World");
var y = Symbol("Hello World");
console.log(x);
console.log(y);
console.log(x==y);
Here,
We have declared value of x and y by using Symbol Data Type. Then, We are Printing the Value of x and y to be Sure Both Values are Assigned.
In the Third Log, We are trying to Compare x and y, according to us both contain the same value so it should return True but As it Contains Symbol Data Type Both Values are Unique, and Thus it Will Result in False.
Null
Null Data Type Represents Blank or Empty Value.
var x = null;
console.log(x);
Here,
null represents Blank Value so Value of x will be Empty.
Nothing will be printed in the Console Tab.
null is different from Null or NULL. Only null represents Blank Value.
Undefined
If any Variable is Declared, but its Value is not Defined then it is Undefined Data Type.
Example:
var x, var k etc.
Example
var x;
console.log(x);
Here,
It will Show Variable x is Undefined in the Console Tab.
Javascript Typeof
Typeof is a Javascript Keyword used to output the Data Type of Variable.
Example: 123 is a Number , "Hello World" is a String. etc.
Syntax
typeof(variableName);
Example
var x = 123;
console.log(typeof(x));
var y = "Hello World";
console.log(typeof(y));
Here,
First For the x it will print "number" and for the y it will print "string" in the Console Tab.