Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Datatypes in JavaScript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 95
    Comment on it

    JavaScript consist many type of datatypes, like:

    1. Boolean
    2. Number
    3. String
    4. Array
    5. Object

    To know what operation you are going to perform on variables, you need to have the knowledge what is the data types of the variable you are using.
    For example:

    var num =10+"Hello"

    This expression doesn't make any sense. You can't add 10 to a Hello. JS will not produce any error here. JavaScript treat this expression as:

    var num ="10"+"Hello"

    Therefore it is very important to define the datatype of the variables you are using.
    JS treats number as a string when we add any number to a string

    In JS the operation starts from left to right.The position of the values plays a very important role.

    Example 1

    var num =10+20 +"Hello";
    The result of this expression will be

    OUTPUT: 30Hello

    Example 2

    var num ="Hello"+10+20 ;
    The result of this expression will be

    OUTPUT: Hello1020

    In the first example JavaScript add 10 in 20 and then treats 30 as a string and not as a number and concatenate it with Hello.
    In the second example JavaScript concatenate Hello with 10 and 20.

    Defining different types of datatypes:

    1. Boolean
      Boolean variables can have only two values either TRUE or False
      var value1=true;
      var value2=false;
    2. Number
      Number type contain only one type of number. The number can either be with decimal or without decimal.
      var value1=2.5;
      var value2=2;
    3. String
      String is a series of character written either inside " " quotes or ' ' single quotes.
      var value1="Car";
      var value2='Car';
    4. Array
      Arrays in JS are used to hold multiple values.
      var name=["Neha", "Megha", "Shilpi",04]
      var name=new Array("Neha", "Megha", "Shilpi",04);
      These are two ways an array can be declared in JS.

    5. Object
      Objects are the real life entities which have properties and behaviours/methods..The values are written as name:value pairs (name and value separated by a colon).

      For example a car have properties like name:Toyota, weight:950kg and methods like start() or stop();.
      var name={firstName:"Neha", lastName:"Bhatt", age:23, eyeColor:"black"};

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: