Skip to content
Related Articles

Related Articles

JavaScript Course | Data Types in JavaScript

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Apr, 2019
View Discussion
Improve Article
Save Article

Previous article: JavaScript Course | Variables in JavaScript

Datatypes in JavaScript

There are majorly two types of languages. First, one is Statically typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C, C++, Java.

// Java(Statically typed)
// variable x is of type int and it
// will not store any other type.
int x = 5;
// type string and will only accept string values
string y = 'abc'

Other, Dynamically typed languages: These languages can receive different data types over time. For example- Ruby, Python, JavaScript etc.

// Javascript(Dynamically typed)
var x = 5; // can store an integer
var name = 'string'; // can also store a string.

JavaScript is dynamically typed (also called loosely typed) scripting language. That is, in javascript variables can receive different data types over time. Datatypes are basically typed of data that can be used and manipulated in a program. A variable in JavaScript can contain any data. This means that a variable at one time can be a number and at another be a string.

The latest ECMAScript(ES6) standard defines seven data types: Out of which six data types are Primitive(predefined).

  • Numbers: 5, 6.5, 7 etc.
  • String: “Hello GeeksforGeeks” etc.
  • Boolean: Represent a logical entity and can have two values: true or false.
  • Null: This type has only one value : null.
  • Undefined: A variable that has not been assigned a value is undefined.
  • Object: It is the most important data-type and forms the building blocks for modern JavaScript. We will learn about these data types in details in further articles.

A number
The number type in javascript contains both integer and floating point numbers. Besides these numbers, we also have some ‘special-numbers’ in javascript that are: ‘Infinity’, ‘-Infinity’ and ‘NaN’. The Infinity basically represents the mathematical ‘?’. The ‘NaN’ denotes a computational error.

let num = 2; // integer 
let num2 = 1.3; // floating point number
let num3 = Infinity; // Infinity
let num4 = 'something here too'/2; // NaN

A String in javascript is basically a series of characters that are surrounded by quotes. There are three types of quotes in Javascript, which are:

let str = "Hello There";
let str2 = 'Single quotes works fine';
let phrase = `can embed ${str}`;

There’s no difference between ‘single’ and “double” quotes in javascript. Backticks provide extra functionality as with the help of them we can embed variables inside them.

let name = "Mukul";

// embed a variable
alert( `Hello, ${name}!` ); // Hello, Mukul!

A Boolean
The boolean type has only two values: true and false.

This data type is used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.

 let isCoding = true; // yes
 let isOld = false; // no

A null
The special null value does not belong to any of the default data types. It forms a separate type of its own which contains only the null value:

let age = null;

The ‘null’ data type basically defines a special value which represents ‘nothing’, ’empty’ or ‘value unknown’.

Just like null, Undefined makes its own type. The meaning of undefined is ‘value is not assigned’.

let x;
console.log(x); // undefined

Objects are not primitive in nature and a bit complex to understand. Everything in javascript is basically an object, and that is the reason why it becomes very important to have a good understanding of what they are. Objects are used to store keyed collections of various data and more complex entities.
We can create objects in multiple ways. One is by making use of figure brackets {…} with an optional list of properties. The properties of an object are in the form of ‘key: value’ pair. Another way is to make use of the ‘new’ keyword.
An empty Object can be created using given below syntax.

let person = new Object(); // "object constructor" syntax
let person = {};  // "object literal" syntax

Both these method are correct, though it’s totally your call what to choose. We can also put properties inside an Object like:

// an object
let person = {     
  name: "Mukul"// by key "name" store value "Mukul"
  age: 22        // by key "age" store value 22

Next article: JavaScript Course | Operators in JavaScript

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!