Menu Close

JavaScript Data Types ( With Examples )

In this article, we are going to learn all about JavaScript data types with the help of examples. Data types in JavaScript basically is identify the type of the variable or value.

JavaScript Data Types

A data type is a type of data that tells the compiler or interpreter, How the programmer wants to use the data in the code. Some common data types are available in JavaScript such as number, string, null, etc.

JavaScript Data Types Flowchart

JavaScript Data Types

Example:


var x = 12;
var name = 'programmingfunda';
var  y = {name: 'programmingfunda', language: 'JavaScript'};

As you can see in the above example we have declared three different variables which are store different values with different data types.

  • x is integer data type.
  • name is string data type.
  • y is object.

JavaScript Data Types Table

Here eight basic data types in JavaScript.

Data TypeDescriptionExample
stringRepresent textual data‘Programming’
numberAn integer or floating-point number12, 12.90, 10020.20 etc
BigIntAn integer with arbitrary precision900719925124740999n etc.
BooleanAny of two values: true or falsetrue or false
undefinedA variable whose value not definedlet x
nullDenotes a null valuelet x = null;
SymbolData type whose instances are unique and immutablelet x = Symbol(“Hello”);
Objectkey-pair pairs of collection of datalet students = {name: ‘Programming Funda’};

In a programming language, Data types are the most important concept. To perform an operation on variables. It is important to know something about the data types.

In below example we are trying to add 40 to programmingfunda.

Example:


var x = 40 + 'programmingfunda';
console.log(x); // 40programmingfunda

When you will execute above example JavaScript treat whole statement like below.


var x = '40' + 'programmingfunda';

Note: When we adding a number and string then JavaScript treat number as string.

JavaScript evaluate expression from left to right.

Example:


var x = 12 + 18 + 'programmingfunda';

Output:

30programmingfunda

JavaScript type are dynamically

JavaScript variable are dynamic, that means same JavaScript variable can hold different data types.

Example:


var x;
x = 'programming'; // string type 
x = 34; // number type

JavaScript String

String is collection of characters surrounded by single or double quotes.

Example:


var x = 'Programming Funda'; // single quotes
var y = "JavaScript"; // double quotes
var result = `${x} is best for ${y}`; // Backticks

Note:- Backticks is used when you need to store variable value inside the string.

JavaScript Numbers

A JavaScript variable can hold number ( Decimal and Exponential ).

Example:


var x1, x2;
x1 = 12;
x2 = 30.0;
console.log(x1);
console.log(x2);

JavaScript BigInt

In JavaScript, the number type can only represent the number less than (253 – 1) and more than (253 – 1). However, If you need to use a large amount of the number, Then you can use BigInt.

BigInt is created in JavaScript by adding n to the end if the number.

Example:


let x = 984783754287467647n;

// Ading two big integers
let result = x + 2n;
console.log(result); // 984783754287467649n

JavaScript Booleans

Boolean can have only two values true or false.

Example:


var x1, x2, x3;
x1 = 12;
x2 = 12;
x3 = 34;
console.log((x1 == x2)); // return true
console.log((x1 == x3)); // return false

JavaScript undefined

undefined data type represents the value that does not define. If a variable declared but a value is not assigned, Then the value of that variable will be undefined.

Example:


let name;
console.log(name); // output will be undefined.

JavaScript null

JavaScript null is a special value that represent the empty and unknown value.

Example:


let x = null;
console.log(x);

JavaScript Object

JavaScript object are written with curly bracket { }. An object is a complex data type that allows us to store large collections of data. Data in an object store in the form of key-value pair.

Example:


var languages = ['JavaScript', 'Python', 'C++', 'PHP']
console.log(languages);

JavaScript arrays are written with a square bracket [ ]. All items in the array are separated by commas. The type of a JavaScript array is also an object.

Example:


var obj;
obj = { firstname: "programming", lastname: "funda", age: 2};
console.log(obj);

JavaScript Symbol

Symbol data type was introduced in a newer version of JavaScript. A value having data type Symbol can be referred Symbol data type. The symbol is an immutable primitive value that is unique.

Example:


let x = Symbol('hello');
let y = Symbol('hello');

JavaScript typeof Operator

In JavaScript, typeof operator is used to find the data types of variables. typeof operator return the type of variable and expression.

Example:


let x = 'hello'; 
let y = 12;
let arr = [1,2,3,4,5]
let obj = { name: "Programming Funda", type: "Coding Platform" };
let sym = Symbol('Hello');
let un;
let n = null;

console.log(typeof x); // string
console.log(typeof y); // number
console.log(typeof arr); // object
console.log(typeof obj); // object
console.log(typeof sym); // object
console.log(typeof un); // undefined
console.log(typeof n); // null

Summary

In this JavaScript data types guide, you have demonstrated all about following:

  • Different-2 data types in JavaScript with the help of the example.
  • Use of JavaScript typeof operator to find the data type of any JavaScript variable.

I hope, having read this article, you will not have any confusion regarding JavaScript data types. If you really like this article, then please share and keep visiting for further JavaScript tutorials.

Previous Article
Next Article

Was this post helpful?

Related Posts