Just like any other programming language JavaScript also have data types. Data types are types which we can assign to variables.
As told earlier in other languages, we declare them when declaring a variable. But in JavaScript, we declare variables using var. Two other variable declarations were also introduced in ES6 – let and const.
But none of them tells whether a variable is a Number, string or Boolean. Whatever we assign to the variable, its type becomes that.
In JavaScript, the data types are broadly classified as Primitives or Objects. We will discuss Primitives here.
In JavaScript, there are six primitive data types:
A boolean represents only one of two values: true, or false. Think of a boolean as an on/off or a yes/no switch.
var boo1 = true; var boo2 = false;
Traditional programming languages like C, C++ and Java generally have many number types like integer, float, double. But there is only one type of Number in JavaScript. The Number type is a double-precision 64-bit binary format. Numbers can be written with or without a decimal point. A number can also be +Infinity, -Infinity, and NaN (not a number). To check the value in +Infinity, -Infinity we can use constants Number.MAX_VALUE and Number.MIN_VALUE.
var num1 = 32; var num2 = +Infinity; console.log(num1); //32 console.log(num2); //Infinity console.log(Number.MAX_VALUE); //1.7976931348623157e+308 console.log(Number.MIN_VALUE); //5e-324 console.log("abc"/2); //NaN
Strings are used for storing text. Strings must be inside of either double(") or single(') quotes. Most recently in ES6 a new quotes was introduced and it’s with tilde(`).
In JavaScript, Strings are immutable (they cannot be changed).
var str1 = 'hello, it is me'; var str2 = "hello, it's me"; var str3 = `hello, it’s me`;
Null has one value: null. It is explicitly nothing. It is useful in JS programming to sometime assign a variable null value.
var nothing = null;
A variable that has no value is undefined. When we first declare a variable as in the statements below, the value of undefined is assigned by the JS compiler.
var testVar; console.log(testVar); // undefined testVar = 3; console.log(testVar); // 3
Symbols are the new primitive data-type introduced in ES6. Symbols are unique an immutable data-type. They are tokens that can be used as unique ids.
Two Symbols are never the same, even if they are declared as same. Consider the below example.
let symbol1 = Symbol(); let symbol2 = Symbol(); console.log(symbol1 === symbol2); //Output - false
I have learned many things from this article. It is beneficial for me. Thank you!
Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..
This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.
Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.
You have shared great information with me i appreciate your work!
Build various types of web applications,command-line application,etc....
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *