JavaScript is a loosely typed {or Dynamic language}, hence any value can be assigned to variables in JavaScript.
{ Note: Loosely type means that we don't have to define variable's data type in advance like string, float, int, etc. }
In JavaScript, we have 7 Data Types which includes 6 Primitive Data Types & 1 Object.
Number - Represents the Integer and Floating values.
String - Represents the textual data.
Boolean - Logical entity with values True or False.
Undefined - Represents variable whose value is not assigned yet.
Null - Represents intentional absence of value.
Symbol - Represents unique value.
The 6 Data Types mentioned above are Primitive Data Types.
Object - Represents key-value pair.
All of the Data Types except Object have Immutable values, i.e. values that can't be changed.
Hey, don't worry I will talk about these Data Types in detail later in this blog but first, let me tell you about ~ What is a Primitive Data Type?
What is a Primitive Data Type?
A primitive Data Type is a data type that has a single value with no additional methods or properties because of its immutability.
Numbers
The number type represents variables whose value is either Integer or Float.
COPY
let integerNumber = 1997;
let floatNumber = 19.97;
Numbers can range between-( 2^(53) - 1 ) and ( 2^(53) - 1 ).
Special Numeric Values
+ Infinity, - Infinity & NaN (Not-A-Number)*are some values which are present in the Numeric Data type.*
COPY
console.log( 1/0 ); // "Infinity" will come as an output.
console.log( "not a number" / 5 ); // "NaN" will come as an output.
Strings
In JavaScript, strings are pieces of text which are enclosed in quotes.
We can use both Single Quotes and Double Quotes to write a String as there is no difference between them in JavaScript.
COPY
let myName = " Maanil "; // Try once in compiler it works.
let myNewName = ' Akshay '; // This works too.
let emptyString = " "; // We can use empty strings too.
We can access each element of the string using Indexing.The first element has Index ' 0 '.
COPY
let newString = " Hello World " ;
//then
newString[1]; // 'e' will be the output as we are accessing element with Index 1.
Boolean ( Logical Data Type )
I'm sorry, but it seems like you haven't provided any text for correction. Could you please provide the markdown-formatted text that needs to be improved?
COPY
let whichNumberIsGreater = 10 > 4;
console.log(whichNumberIsGreater); // " True " will be output for the comparison.
Undefined
Undefined is the value assigned to the variable that has not yet been assigned any value.
We can also explicitly assign an 'undefined' value to a variable, but that does not make any sense due to its meaning.
COPY
let email;
console.log(email); // It will return "undefined" as value is not assigned.
Null
'null' is the value that represents a reference that points to a non-existent object or address. This means that there is an absence of a value. ( Value is empty or unknown. )
COPY
let email = null;
Symbol
A Symbol is a unique identifier that contains a unique value. Symbol will always be unique even if the descriptors are the same. Let's understand by an example:
COPY
let companyId = Symbol("idNumber");
let companyId_1 = Symbol("idNumber");
// Check inside Symbol( ) both descriptors are same but still variable contains unique value.
// Let us verify the above line
console.log( companyId == companyId_1 ); //Returns False as output which proves the above
line.
Object
In JavaScript, Objects are a collection of properties in a Key-Value Pair*.
These objects can be related to real-life objects, like similar objects have the same type of properties, but they differ from each other.
Let's understand in layman terms,
Let's assume 'Ball' is an Object with properties ' Shape ' and ' Radius '. Now, you can relate that every ball will have the same properties but their values associated with them will be different.
Some Important points about Objects are -
● Object contains properties separated with a comma( , ).
● Each property is represented in a Key-Value pair.
● Key and Value are separated using a colon( : ).
● The key can be a string or variable name that does not contain any Special Characters, except for underscore( _ ).
● The value can contain any type of data - Primitive, Non-Primitive, and even a Function.
● The objects are passed by reference to a function.
COPY
let myDetails = {
name: "Akaash",
age: 21,
favouriteColor : " Red "
};
// This how we create an Object
// Accessing the properties of an Object.
console.log(myDetails.name); // This will return Akaash
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
So, here concludes the detailed description of Data Types in JavaScript. I hope now your concepts around Data Types might be clear.
Before you leave, let me tell you one thing that in this blog " Object " Data Type is explained briefly. It has more theory to it which I will be discussing in my next blog.
Till then keep JavaScripting and Stay Tuned !!