JavaScript Variable And Data Types

avatar

image.png

Image Source

In JavaScript, variables are used to store a value in certain part of the memory and that you can use to manipulate your program to do the certain task. var keyword is used to declare a variable in JavaScript like: var x. Doing so, the variable 'x' initially have an empty value.

However you can initialzie the value of x during the variable declaration like var x=5. It is also possible to declare multiple variable with "var" like var x=5, i=7, name="pqr". Lets try declaring a variable in our VS Code.

image.png

Now if we turn on the live server and see the output on browser window, we will get the output as "undefined".

image.png

Also we will see a basic declaration of data types in JavaScript. There are 7 basic data types in JavaScript that falls under three categories.

  1. Primitive Data Types: This includes Number, String and Boolean.
  2. Trivial Data Types: This includes undefined and null.
  3. Composite Data Types: This includes object and array.
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Variable</title>
<script>
    var a=8;
    var b="raven";
    var c = true;
    var bike = ["Pulsar", "Apache", "Duke"];

    var student = {firstName: "john", lastName: "Dukey", age: 15};

    document.write(typeof(a));

    document.write("<br>");

    document.write(typeof(b));

    document.write("<br>");

    document.write(typeof(c));

    document.write("<br>");

    document.write(typeof(bike));

    document.write("<br>");

    document.write(typeof(student));

</script>

</head>
<body>
</body>
</html>

For better understanding we will visualize this code from the following picture:

image.png

Line number 6, 7, 8, 9 and 11 respectively holds the value for Number, String, Boolean, Array and Object data type. And in Line number 13, 17, 21, 25 and 29 we printed their type using typeof() function in JavaScript. The browser window will show the following output:

image.png

We already saw about "undefined" data type above as well. The data types of array and null is shown as "object" in JavaScript and I knew that it was one of the error during the design and can't be fixed while researching.

image.png

Image Source

In JavaScript, variables are used to store a value in certain part of the memory and that you can use to manipulate your program to do the certain task. var keyword is used to declare a variable in JavaScript like: var x. Doing so, the variable 'x' initially have an empty value.

However you can initialzie the value of x during the variable declaration like var x=5. It is also possible to declare multiple variable with "var" like var x=5, i=7, name="pqr". Lets try declaring a variable in our VS Code.

image.png

Now if we turn on the live server and see the output on browser window, we will get the output as "undefined".

image.png

Also we will see a basic declaration of data types in JavaScript. There are 7 basic data types in JavaScript that falls under three categories.

  1. Primitive Data Types: This includes Number, String and Boolean.
  2. Trivial Data Types: This includes undefined and null.
  3. Composite Data Types: This includes object and array.
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Variable</title>
<script>
    var a=8;
    var b="raven";
    var c = true;
    var bike = ["Pulsar", "Apache", "Duke"];

    var student = {firstName: "john", lastName: "Dukey", age: 15};

    document.write(typeof(a));

    document.write("<br>");

    document.write(typeof(b));

    document.write("<br>");

    document.write(typeof(c));

    document.write("<br>");

    document.write(typeof(bike));

    document.write("<br>");

    document.write(typeof(student));

</script>

</head>
<body>
</body>
</html>

For better understanding we will visualize this code from the following picture:

image.png

Line number 6, 7, 8, 9 and 11 respectively holds the value for Number, String, Boolean, Array and Object data type. And in Line number 13, 17, 21, 25 and 29 we printed their type using typeof() function in JavaScript. The browser window will show the following output:

image.png

We already saw about "undefined" data type above as well. The data types of array and null is shown as "object" in JavaScript and I knew that it was one of the error during the design and can't be fixed while researching.



0
0
0.000
0 comments