Awesome Club

JavaScript Data types

Understanding JavaScript Data Types

Variables in JavaScript are dynamic meaning they can contain all data types. Some common JavaScript data types are strings, objects, arrays, and numbers.


Here are some examples of variables with different data types.


        
            let a = 15;                             //Number
            let b = "Billy";                        //String
            let c = {name: "Billy", age: 15};       //Object
            let d = [1, 2, 3, 4, 5];                //Array
            let e = true;                           //Boolean 
            let f = undefined;                      //Undefined
        
    

Something to note with JavaScript numbers is that sometimes working with them can have unexpected results.

For example, consider the following


        
            let sum = "Word" + 10;
        
    

Will the program crash, throw an error, or produce an unexpected result?

The value of sum would actually be "Word10"

This is because when a number is being added after a string, the number will be taken as a string

Additionally, consider the following


        
            let sum = "Word" + 10 + 4;
        
    

Any guess what this value of sum would be?

Sum would be equal to "Word104" because a string was first and therefore the following numbers will be taken as strings.

Lastly, consider the following

        
            let sum = 10 + 4 + "Word";
        
    

If you guessed that sum would be equal to "14Word" then you were right!


JavaScript data types are relatively easy to understand and work with as variables do not have to be assigned a data type.