JavaScript data type is an important concept. Data types are the categorizations we give to different kinds of data that we use in programming. There are 7 data types available in JavaScript. These are-

1. Number;
2. String;
3. Boolean;
4. Undefined;
5. Null;
6. Symbol &
7. Object Type.

All of them except Object have immutable values, i.e. the values which can not be changed. Let me explain the above JavaScript Data Type in detail.

1. Number: Number represents any number, including numbers with decimals such as 2, 6, 23.5, etc.
Example:

var a = 20; //without decimals
var b = 23.5; // with decimals

Extra large or extra small numbers can be written with scientific notation.
Such as:

var a = 246e5; // 24600000
var b = 246e-5; // 0.00246

2. String: In JavaScript, string is used to represent textual data. String are always are written with single or double quotes.
Examples:

“smartsourav”; //with double quotes
‘smartsourav’; //with single quote

 

Keep in mind that there is no difference between single or double-quotes. In JavaScript, you can use any one of them.
Also remember, each element in string occupies a position in the string. The first element is at index 0, the next at index 1, and so on.
The length of a string is the number of elements in it. In the string “smartsourav”, m is at index 1, t is at index 4, and so on.

index of an element of a string
3. Boolean Type: Boolean represents a logical entity and it can have only two values: true or false. Booleans are often used conditional testing.

4. Undefined: In JavaScript, a variable without a value, has the value undefined. That means if a variable has been declared, but no value has been assigned to it, it has the value undefined.
Also, we can explicitly, assign ‘undefined’ value to a variable, but that does not make any sense due to its meaning.

Example:

var a;

define a variable that has not been assigned any value.
If you write on the console as console.log(a); then undefined will be printed.

5. Null Type: Null is the value that represents a reference that points to a non-existent object or address. This means there is an absence of a value i.e there is no value.
Remember, it is not equivalent to 0 or an empty string (” “), simply it is nothing.

6. Symbol Type: In JavaScript, symbol type is a new concept. Symbols are unique identifiers. These are useful in more complex coding.
No need to worry about this for now. We will cover this concept later. But remember, in some programming languages, symbols are called “atoms”.

7. Object Type: In JavaScript, object is the most important concept. If you understand the object, no doubt, I can say, you understand JavaScript. Simply, objects are the collection of property and its value. In JavaScript, almost everything is an object.
Let’s discuss an example before going deeper. In real life, a bicycle is an object. It has properties like color and weight. It also has methods like start and stops.

Object-Properties-Methods

All bicycles have the same properties, but the property values differ from bicycle to bicycle. All bicycles have the same methods, but the methods are performed at different times.

In JavaScript, you know that variables are the containers for data values. Objects are variable too but the main difference is an object can contain many values.

Look at the below code. It assigns many values to the variable named bicycle.

var bicycle = {

 

        name : “Hero”,

 

        color : “Red”,

 

        weight : “20kg”,

 

};

 

Here bicycle is an object. The values are written as name: value pairs. Here, name and value are separated by a colon. The name: values pairs are called properties.

JavaScript-Data Type-Object Property Name:Value Pair

%d bloggers like this: