Edit

JavaScript Data Types

Data types basically specify what kind of data can be stored and manipulated within a program.

There are six basic data types in JavaScript which can be divided into three main categories: primitive (or primary), composite (or reference), and special data types. String, Number, and Boolean are primitive data types. Object, Array, and Function (which are all types of objects) are composite data types. Whereas Undefined and Null are special data types.

The String Data Type

The string data type is used to represent textual data (i.e. sequences of characters). Strings are created using single or double quotes surrounding one or more characters, as shown below:

Example of Code

<script>
    var a = 'Hi there!';  // using single quotes
    var b = "Hi there!";  // using double quotes
    // Printing variable values
    document.write(a + "<br>");
    document.write(b);
</script>

Output

The Number Data Type

The number data type is used to represent positive or negative numbers with or without decimal place, or numbers written using exponential notation e.g. 1.5e-4 (equivalent to 1.5x10-4).

Example of Code

<script>
    var a = 25;
    var b = 80.5;
    var c = 4.25e+6;
    var d = 4.25e-6;
    // Printing variable values
    document.write(a + "<br>");
    document.write(b + "<br>");
    document.write(c + "<br>");
    document.write(d);
</script>

Output

The Boolean Data Type

The Boolean data type can hold only two values: true or false. It is typically used to store values like yes (true) or no (false), on (true) or off (false), etc. as demonstrated below:

Example of Code

<script>
    var a = 2, b = 5, c = 10;
    document.write(b > a) // Output: true
    document.write("<br>");
    document.write(b > c) // Output: false
</script>

Output

The Undefined Data Type

The undefined data type can only have one value-the special value undefined. If a variable has been declared, but has not been assigned a value, has the value undefined.

The Null Data Type

This is another special data type that can have only one value-the null value. A null value means that there is no value. It is not equivalent to an empty string ("") or 0, it is simply nothing.

A variable can be explicitly emptied of its current contents by assigning it the null value.

Example of Code

<script>
    var a = null;
    document.write(a + "<br>"); // Print: null
    var b = "Hello World!"
    document.write(b + "<br>"); // Print: Hello World!
    b = null;
    document.write(b) // Print: null
</script>

Output

The Object Data Type

The object is a complex data type that allows you to store collections of data.

An object contains properties, defined as a key-value pair. A property key (name) is always a string, but the value can be any data type, like strings, numbers, booleans, or complex data types like arrays, function and other objects. You'll learn more about objects in upcoming chapters.

Example of Code

<script>
    var emptyObject = {};
    var person = {"name": "Clark", "surname": "Kent", "age": "36"};
    // For better reading
    var car = {
        "modal": "BMW X3",
        "color": "white",
        "doors": 5
    } 
    // Print variables values in browser's console
    console.log(person);
    console.log(car);
</script>

Output

The Array Data Type

An array is a type of object used for storing multiple values in single variable. Each value (also called an element) in an array has a numeric position, known as its index, and it may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. The array index starts from 0, so that the first array element is arr[0] not arr[1].

Example of Code

<script>
    // Creating arrays
    var colors = ["Red", "Yellow", "Green", "Orange"];
    var cities = ["London", "Paris", "New York"];
    // Printing array values
    document.write(colors[0] + "<br>");   // Output: Red
    document.write(cities[2]);   // Output: New York
</script>

Output

The Function Data Type

The function is callable object that executes a block of code. Since functions are objects, so it is possible to assign them to variables, as shown in the example below:

Example of Code

<script>
    var greeting = function(){ 
        return "Hello World!"; 
    }
    // Check the type of greeting variable
    document.write(typeof greeting) // Output: function
    document.write("<br>");
    document.write(greeting());     // Output: Hello World!
</script>

Output