top
upGrad KnowledgeHut SkillFest Sale!

Search

JavaScript Tutorial

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:BooleanNumberStringNullUndefinedSymbol (Introduced in ES6)JavaScript BooleanA 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;NumberTraditional 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); //NaNStringStrings 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`;NullNull has one value: null. It is explicitly nothing. It is useful in JS programming to sometime assign a variable null value.var nothing = null;UndefinedA 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); // 3SymbolSymbols 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
logo

JavaScript Tutorial

Primitive Data Types

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:

  • Boolean
  • Number
  • String
  • Null
  • Undefined
  • Symbol (Introduced in ES6)

JavaScript Boolean

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;

Number

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

String

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

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;

Undefined

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

Symbol

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

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.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More