20 Feb JavaScript Data Types
The data types in JavaScript are divided into the following:
- Primitive Data Types: String, Number, Boolean, Undefined, Null,
- Non-Primitive: Object, Array
Let us understand the data types one by one with examples:
JavaScript String Data-Type
The string data type in JavaScript represents a sequence of characters, for example, “amit”, “john”, etc.
Let us see an example to work on the string data type in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>Data Types in JavaScript</h1> <h2>String data type</h2> <p id="test"></p> <script> let a = "Amit"; document.getElementById("test").innerHTML = a; </script> </body> </html> |
Output
JavaScript Number Data-Type
The number data type in JavaScript not only represents integers but float values as well.
Let us see an example to work on number data type in the form of integer number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <body> <h1>Data Types in JavaScript</h1> <h2>Number data type</h2> <p>In the form of integer number...</p> <p id="test"></p> <script> let a = 100; document.getElementById("test").innerHTML = a; </script> </body> </html> |
Output
Let us see an example to work on number data type in the form of floating-point numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <body> <h1>Data Types in JavaScript</h1> <h2>Number data type</h2> <p>In the form of floating-point number...</p> <p id="test"></p> <script> let a = 15.7; document.getElementById("test").innerHTML = a; </script> </body> </html> |
Output
JavaScript Boolean Data-Type
The Boolean data type in JavaScript represents the Boolean values i.e. true or false.
Let us see an example to work on boolean data type in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>Data Types in JavaScript</h1> <h2>Boolean data type</h2> <p id="test"></p> <script> let pass = true; document.getElementById("test").innerHTML = pass; </script> </body> </html> |
Output
JavaScript Undefined Data-Type
The Undefined data type is used to represent an undefined value.
Let us see an example to work on undefined data type in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <body> <h1>Data Types in JavaScript</h1> <h2>Undefined data type</h2> <p id="test"></p> <script> let a; document.getElementById("test").innerHTML = a; </script> </body> </html> |
Output
JavaScript Null Data-Type
The Null data type is used to represent null. The null means no value.
Let us see an example to work on null data type in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <body> <script> let a = null; alert(a) </script> </body> </html> |
Output
To view the output on the console, press F12 on the web browser. Then, click the Console tab as shown below:
JavaScript Object Data-Type
The object data type represents name:value pairs, surrounded with curly braces.
Let us see an example to work on object data type in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <script> var student = { "id": "S05", "name": "Amit", "marks": 95, "rank": 1 } console.log(car); </script> </body> </html> |
Output
To view the output on the console, press F12 on the web browser. Then, click the Console tab as shown below:
JavaScript Array Data-Type
The Array data type represents more than one value in a single variable. The items in the array are separated by commas. It is enclosed by square brackets. The index of an array begins from 0.
Let us see an example to work on array data type in JavaScript. We will declare an array and display all the items:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <body> <script> // Declare student names array // The array consists of four items let names = ["Amit", "John", "Tim", "Jacob"]; // Display the Array alert(names); </script> </body> </html> |
Output
Let us see an example to display specific array items:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <body> <script> // Declare student names array // The array consists of four items let names = ["Amit", "John", "Tim", "Jacob"]; // Display the 1st array item alert(names[0]); </script> </body> </html> |
Output
No Comments