Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - DATA TYPES


We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.


i.e.


var x = 5

Or


var y = "Hello World"

But how will JavaScript come to know, if the value is a number or a name?


Well! Once again, that's the beauty of JavaScript. JavaScript makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.


But! Just think for a moment. JavaScript must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.


And thus Data Types comes into picture.


Note : In other languages, you have to know all the Data Types to declare a variable. But in JavaScript, it is not necessary for you to know the Data Types. You can just assign the values and JavaScript will do the task of determining the Data Types.

Data Types


Data Types are the hidden gem that is used by JavaScript to determine if the variable is going to hold a number or a name.


But why does JavaScript needs to determine if the variable is going to hold a number or a name? Can it not create one type of variable and store all the values there.


It cannot do that. Because if you want to store a number. It would take less space.

java_Collections

On the other hand, if you want to store a String like Hello World. It would take a larger space.

java_Collections

So, whenever you want to create assign a String to a variable.


var y = "Hello World"

JavaScript makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.

java_Collections

The above Data Type for String is called string Data Type.


And similarly for a number, for a decimal value e.t.c. JavaScript has different Data Types.


List of all Data Types used in JavaScript

  1. String



    As we have seen in the above example, a String is a set of letters/alphabets.

    1. string Data Type



      The Data Type for String is string.

      When JavaScript finds data inside Double Quotes ("") of Single Quotes (''). It creates a String type variable for that value.

      	y = "Hello World"


      Or

      	y = 'Hello World'


      A string variable can alse be declared using new String(), with the string inside the bracket ().

      So, the Data Type of a variable is determined by JavaScript and is hidden from us.

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      
      		var x = "Hello World"
      		document.write(x)
      		
      	</script>
      	</body>
      	</html>
      


      Output :



        Hello World

  2. Numbers



    Numbers could be a floating point number (i.e. 5.3322) or a whole number (i.e. 7 or 5).

    The data type for Number is number. A number variable can be declared by assigning value to a variable directly.

    var x = 5
    
    A Number variable can alse be declared using new Number(), with the number inside the
    bracket ().


    Let us see them below:

    1. Whole Number



      As we have discussed above, the Data Type for a whole number is number.

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      
      		var x = 5
      		document.write(x)
      		
      	</script>
      	</body>
      	</html>
      


      Output :



        5

    2. Floating point number



      The Data Type for a floating point number is also number.

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      
      		var x = 5.987
      		document.write(x)
      		
      	</script>
      	</body>
      	</html>
      


      Output :



        5.987

  3. Arrays



    So far we have seen, how to store a Name, a sentence or numbers in separate variables. But what if, we want to store a name and a number in the same variable?

    Well! Array is the Solution. Array, is going to store multiple values in one variable.

    1. Array



      An Array is a type of Collection that holds multiple values.

      The declaration of an Array is super easy. You can place the multiple values inside square brackets [] and JavaScript will understand that it is an array.

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      
      		var x = [5, "John", "JavaScript"]
      		document.write(x)
      		
      	</script>
      	</body>
      	</html>
      


      Output :



        5,John,JavaScript


      An Array can also be declared using new Array(), with the elements inside parenthesis ().

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      
      		var x = new Array(5, "John", "JavaScript")
      		document.write(x)
      		
      	</script>
      	</body>
      	</html>
      


      Output :



        5,John,JavaScript

  4. Booleans



    Boolean represents just two values, i.e. true or false.

    1. boolean Data Type



      A boolean Data Type can accept only true or false.

      Example :



      	<html>
      	<body>
      	<script language = "javascript" type = "text/javascript">
      	
      		var x = new Boolean(true)
      		var y = false
      		document.write(x)
      		document.write(y)
      	
      	</script>
      	</body>
      	</html>
      


      Output :



        true false


      Just remember all the letters of true and false should be in lower case.

      x = true is invalid.

  5. Objects



    We will be discussing more about objects in the coming tutorials. For now, let's understand in brief.

    We have seen that an Array is capable of holding some values. Now, what if, we want the details of a student to be stored.

    Say, a student can have a name, a roll number and age. And we wanted to store it in a variable in the below way:

    name : John
    roll : 38
    age  : 12


    And this is where Object data type comes into picture.

    Declaring an Object is super simple. Just put the above values inside {}. And the values should be separated by ':'.

    var student = {name : John, roll : 38, age  : 12}


    And we are done.

    Example :



    <html>
    <body>
    <script language = "javascript" type = "text/javascript">
    	
    	var student = {name : "John", roll : 38, age  : 12}
    	document.write(student.name, ", ")
    	document.write(student.roll, ", ")
    	document.write(student.age)
    	
    </script>
    </body>
    </html>
    


    Output :



      John, 38, 12


    So, in the above code, we have declared an object student,

    var student = {name : "John", roll : 38, age  : 12}


    Now, if we want to get the name of the student, we can just type,

    student.name


    And the name of the student would be displayed.

    Similarly, if we want to get the roll or age, the below statement works,

    student.roll
    student.age

  6. Undefined



    Undefined as the name suggests, if you do no assign a value to a variable, it would have the data type as undefined.

    Say, for example, the below variable x is not assigned any value,

    var x


    And has the data type as undefined.

  7. Function



    Programmers coming from other backgrounds like Java or JavaScript might be confused that how come Function could be a data type?

    Well! In JavaScript Function is also a data type.

    A Function is something that performs a set of tasks. We will learn more on Functions in a separate tutorial.

    For now just remember a function can be defined in the below way,

    function myFunction(){}