Welcome to the JavaScript Tutorials, and we have already covered popups in JavaScripts. Today we will talk about JavaScript data types, to hold different types of values javascript have different data types.The JavaScript data types have two types:

  • Primitive data types
  • Non-primitive data types


Note: You can use ‘var’ to specify any data types, it will hold any value like number,strings, array etc. That is why JavaScript is a dynamic type language.

Check JavaScript data types

JavaScript Data types – Primitive Type

JavaScript consider five data types as a primitive JavaScript data types, and these are:

  • String – It will represent the characters, words or can say a sequence of letters. For example “ sharplesson”,”Quick tips” etc.
  • Number – It will only hold the number values, like 02,56 etc.
  • Boolean – It will hold the values like True or False.
  • Undefined – undefined values will be represented by Undefined.
  • Null – It will hold no value, it means nothing.

JavaScript Data types – Non-primitive Type

  • Object – It will hold the instance, through which we can access members.
  • Array – It will hold the group of similar terms or value.
  • RegExp – Regular expression

JavaScript Data types – Examples

Now we will try to understand the data types with the use of the examples. Because practical things are easy to understand:

String – Example

Unlike other languages, JavaScript has ‘var’ to hold any value, for example, if you want ‘var’ to declare the string, then you can do like:

Var bestWebsite = “SharpLesson”;

Var bestWebsite = ‘SharpLesson’;

Note: You can declare string either in a double quote or in a single quote.

Number – Example

We can use the same var to declare the number as well:

Var num = 10;

Var num = 10.5;

Note: you can declare any number (decimal or non-decimal) without any double or single quotes.

Boolean – Example

As we know Boolean has only two values: True or False

Var bool = false;

Var bool = true;

Note: If your code requires a conditional testing, then you can use Boolean. Don’t worry we will show the use of it in our examples.

Arrays – Example

Now we will see, how to declare the arrays using var. Array items are separated by commas.

Var sharplesson = [“HTML”, “CSS”, “JavaScript” ];

We have declared an array sharplesson with three array items.

Note: Arrays have to be written in square brackets.

Objects – Example

Now lets see how to declare the objects, JavaScript objects are to be written in a curly bracket.

Var cycle = { Brand:”sharplesson”, Model:”xy”, Price:” thousand“};

We have taken cycle as an object, and it has three properties and three values for the same.

Undefined – Example

If you declare a variable without a value, then it is considered to have an undefined value. Let’s say for example:

Var price;

You can see we have not assigned any value, and indirectly can’t figure out its type as well.

Example – to test

Now we will see how to implement above data types in a real world example, till now we saw what is data type and how to declare it. Let’s see the practical use of some data types:

We will see why JavaScript is a dynamic language from the below example, we have used variables to store the number and string as well.

<html>

<head>

</head>

<body>

<p id="SharpExample">

<script>

var name = "sharplesson";

var val = 5;

var  val1 = "star";

var num = 3;

var result = name + "&nbsp;has&nbsp;got&nbsp;" + val +"&nbsp;"+ val1 + "&nbsp;or&nbsp;" +num ;

document.getElementById("SharpExample").innerHTML = result;

</script>

</p>

</body>

</html>

When you will run the above code, you will see the result:

sharplesson has got 5 star or 3

You can clearly see that, like other language we need not to use different data types for strings or number. We have just used ‘var’ and assisgned values in double and without quotes.

Note: we have used (&nbsp;) to put a space between the words and numbers.

Conclusion

We have just covered JavaScript Data Types and shown the use with a simple example, try it with more examples. We have not used, all JavaScript data types in an example, because they need proper discussion. We will cover it in more detail to help you further.

As of now, we have shown you the types of data and an idea about how to use it?


What next? Next we will talk about JavaScript Variables.


If you liked our content on JavaScript data types, then you can give us a like, share it with friends or tweet for us. Any effort of yours will let us write a quality tutorial.