JavaScript Data Type

JavaScript Data Type

JavaScript Data Type:

String, Number, Boolean, Array, Object.

JavaScript variables can store many types of data: number, string, array, object and many more.

var scores = 198;   // number
var name = "Mamun";   // string
var animals = ["Tiger", "Lion", "Elephant"];   // array
var a = {firstName: "Sakib", lastName: "Arif"};   // object

 

The idea of data type:

In the case of programming, data type is an important factor.

Knowing the data type is very important when working with variables.

Without data type, computer cannot solve these (data) properly

var a = 28 + "Masud";

Does it make sense to add "Tamim" to 28? Will it show an error or give results?

JavaScript will think of the above example as follows:

var a = "28" + "Masud";

enlightenedWhen a number is added to a string, JavaScript treats the number as a string.

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <h1>When a number and a string are added, the number in JavaScript counts as a string.</h1>
  8.      <p id="test"></p>
  9.      
  10.     <script>
  11.        var a = "28" + "Masud";
  12.        document.getElementById("test").innerHTML = a;
  13.     </script>
  14.   </body>
  15. </html>

 

Output:

                 

When a number and a string are added, the number in JavaScript counts as a string.

28Masud

 

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <h1>When a number and a string are added, the number in JavaScript counts as a string.</h1>
  8.      <p id="test"></p>
  9.     <script>
  10.        var a ="Masud" + 76 ;
  11.        document.getElementById("test").innerHTML = a;
  12.     </script>
  13.   </body>
  14. </html>

 

Output:

                 

When a number and a string are added, the number in JavaScript counts as a string.

Masud76

 

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.     <script>
  9.        var a =70 + 30 + "Masud" ;
  10.        document.getElementById("test").innerHTML = a;
  11.     </script>
  12.   </body>
  13. </html>

 

Output:

               

 100Masud

 

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.     <script>
  9.        var a = "Masud" + 70 + 30 ;
  10.        document.getElementById("test").innerHTML = a;
  11.     </script>
  12.   </body>
  13. </html>

 

Output:

               

 Masud7030

 

In the first example, 28 and 12 are added as numbers and then "Tamim" is a string so JavaScript considers the whole as a string.

In the second example, since the first operand is a string, all operands are treated as strings.


JavaScript dynamic data type:

Data type dynamics in JavaScript. This means that different types of data can be kept in the same variable.

var x; // Here a is undefined
var x = 10; // Here a is the number
var x = "Hamin"; // Here a is a string.

 

JavaScript String:

A string is a series of characters. Such as- "ArmanRahman".

The string has to be written in quotation marks. You can use single or double quotation:

var newCar = "Volvo XC60"; // Uses a double coat
var new Car = 'Volvo XC60'; // Using single quotes

You can also use quotations within a string, but not match quotations on either side of the string:

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.     <script>
  9.        var a = "Welcome To Only Learn 24 Academy";
  10.        var b = "Welcome To 'Only Learn 24' Academy";
  11.        var c = 'Welcome To "Only Learn 24" Academy';
  12.        document.getElementById("test").innerHTML = a + "<br>" + b + "<br>" + c;
  13.     </script>
  14.   </body>
  15. </html>

 

Output:

             

Welcome To Only Learn 24 Academy
Welcome To 'Only Learn 24' Academy
Welcome To "Only Learn 24" Academy

 

 


JavaScript number:

There is only one type of number in JavaScript.

Numbers can be written in addition to decimals or decimals

var a = 12.00; // Numbers with decimals
var b = 12; // Numbers without decimals

Extra large or extra small numbers can be written in an exponential manner

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.     <script>
  9.        var a = 20.00;
  10.        var b = 15;
  11.        var c = 34e4;
  12.        var d = 324e-4;
  13.        document.getElementById("test").innerHTML = a + "<br>" + b + "<br>" + c + "<br>" + d;
  14.     </script>
  15.   </body>
  16. </html>

 

Output:

         

20
15
340000
0.0324

 

You will learn more about numbers in the next chapter of this tutorial.


JavaScript Boolean:

Booleans have only two values: true or false.

var a = true;
var b = felse;

Booleans are often used to perform condition tests.

You will learn more about the Condition Test in the next chapter of this tutorial.

 

JavaScript Array:

JavaScript arrays have to be written with a third bracket (Square Bracket).

The items in the array have to be separated by commas (,).

In the following example, an array variable named animals is declared, which has three values:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.           var animals = ["Lion", "Tiger", "Horse", "Hen"];
  10.           document.getElementById("test").innerHTML = animals[2];
  11.       </script>
  12.  
  13.   </body>
  14. </html>

 

Output:

             

Horse

 

The index of the array starts with 0, i.e. the first element will be [0], the second element will be [1], and so on.

You will learn more about arrays in the next chapter of this tutorial.


JavaScript Object:

The JavaScript object is written through the second bracket (curly bracket).

The properties of the object have to be written as name: value in pairs, multiple properties have to be separated by commas (,).

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       var allmember = {firstName:"Asif", lastName:"Rahman", age:28, eyeColor:"black"};
  10.       document.getElementById("test").innerHTML =
  11.       allmember.firstName + " " + allmember.lastName + " " + "is" + " " + allmember.age + " " + " years old.";
  12.       </script>
  13.  
  14.   </body>
  15. </html>

 

Output:

             

Asif Rahman is 28 years old.

 

In the example above, the person object has four properties: firstName, lastName, age, and eyeColor.


typeof operator:

You can use typeof operator to know the type of JavaScript variable:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       document.getElementById("test").innerHTML =
  10.           typeof "Habib" + "<br>" +
  11.           typeof 9.64 + "<br>" +
  12.           typeof NaN + "<br>" +
  13.           typeof false + "<br>" +
  14.           typeof new Date() + "<br>" +
  15.           typeof function () {} + "<br>" +
  16.           typeof newDay + "<br>" +
  17.           typeof [1,2,3,4,5,6] + "<br>" +
  18.           typeof {name:'Asif', age:27} + "<br>" +
  19.           typeof null;
  20.       </script>
  21.  
  22.   </body>
  23. </html>

 

Output:

       

string
number
number
boolean
object
function
undefined
object
object
object

 

An array in JavaScript is a special type of object. So array type object.


Undefined:

In JavaScript, declaring a variable without a value has an undefined value. The value of typeof is also undefined.

 

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       var allmember;
  10.       document.getElementById("test").innerHTML =
  11.       allmember + "<br>" + typeof allmember;
  12.       </script>
  13.  
  14.   </body>
  15. </html>

 

Output:

             

undefined
undefined

 

The value of the variable can be set to undefined and its value can be left blank.


Empty Values:

Empty value and undefined are two different things.

Empty strings have both value and type.

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       var allmember = "";
  10.       document.getElementById("test").innerHTML =
  11.       allmember + "<br>" + typeof allmember;
  12.       </script>
  13.  
  14.   </body>
  15. </html>

 

Output:

 
string

 

 


Null:

Null in JavaScript means "nothing". It means that which has no existence.

In JavaScript, the data type of null is the object.

enlightenedIn JavaScript, the value of null's typeof operator is the object, you can think of it as a bug, because it should be null.

You can set the value of the object to null and leave it blank:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       var allmember = null;
  10.       document.getElementById("test").innerHTML =
  11.       allmember + "<br>" + typeof allmember;
  12.       </script>
  13.  
  14.   </body>
  15. </html>

 

Output:

             

null
object

 

 


The difference between undefined and null:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.      <p id="test"></p>
  8.       <script>
  9.       document.getElementById("test").innerHTML =
  10.       typeof undefined + "<br>" +
  11.       typeof null + "<br>" +
  12.       (null === undefined) + "<br>" +
  13.       (null == undefined);
  14.       </script>
  15.  
  16.   </body>
  17. </html>

 

Output:

             

undefined
object
false
true

 

 

Thank You...........