Skip to main content

Command Palette

Search for a command to run...

Data Types In JavaScript

Updated
โ€ข5 min readโ€ขView as Markdown
Data Types In JavaScript

Welcome guys!!! to my first ever blog on hashnode. Hope you like the content . In this blog , We are discussed about the JavaScript data type .

A a developer we have to learn Programming language and as we know every developer learn his first programming language , In that we also learn about the Data Types . So basically we are familiar with data types . In this blog we take a look on how data types used in JavaScript with explanation and examples . So guys Let's Start.

When we write your program we need to store our data as input inside our variable, we need to know that which type of data we stored in the program. Basically Data types specify that what kind of data can be stored in the program and how it can be manipulated within a program.

There are six basic data types in JavaScript : Number, String, and Boolean , Object, Array,and function . We have Null andUndefined as special data types.

To get the type of variable we use typeof operator. Let's See a demo for this

var  count = 50; 
console.log(typeof(count)); 

count = true;   
console.log(typeof(count)); 

 count = "hello there";   
console.log(typeof(count));

Output :-

"number"
"boolean"
"string"

1. The String Data Type

In JavaScript, a string is a sequence of zero or more characters. Strings are created using a single quote(โ€˜) or a double quote (โ€œ) surrounding one or more characters . for example:

 var a = 'Hi there!';  // using single quotes
 var b = "Hi there!";  // using double quotes
//we can include quotes inside the string as long as they don't match the enclosing quotes.
 var str = 'This\' is also a string'; // use \ to escape the single quote (')

2. The Number Data Type

The number data type have positive and negative numbers with or without decimal place. JavaScript used IEEE-754( Institute of Electrical and Electronics Engineers) format to represent both integer and floating-point numbers. for example:

var a = 25;         // integer
var b = 80.5;       // floating-point number
var c = 4.25e+6;    // exponential notation, same as 4.25e6 or 4250000

In JavaScript NaN represents a special Not-a-Number value. It represents an invalid or an undefined mathematical operation, like taking the square root of -2 or dividing 0 by 0, etc. for example:

alert("Some text" / 3);       // Output: NaN
alert("Some text" / 2 + 10);  // Output: NaN
alert(Math.sqrt(-1));         // Output: NaN

3. The Boolean Data Type

In JavaScript if we have to store Yes And No so we use Boolean data type, which can hold only two values: true or false. let's see example for that

var isReading = true;   // yes, I'm reading
var isSleeping = false; // no, I'm not sleeping

One more this is that Boolean values also come as a result of comparisons in a program. Let's see an example which compares two variables and shows the result .

var a = 2, b = 5, c = 10;

alert(b > a) // Output: true
alert(b > c) // Output: false

Here we have have cover Primitive data type . Now moving forward to next one .

4. The Undefined Data Type

The undefined data type can have only one the special value undefined. If we declared a variable , but did not assigned a value, then It shows the value undefined . for example:

var a;
var b = "Hello World!"

console.log(a) // Output: undefined
console.log(b) // Output: Hello World!

5. The Null Data Type

In this we discuss another special data type which can have only one value the null value. A Null value have no value in it . but it does not equal to an empty string ("") or 0, simply it is nothing.

If we assigned a Null value to a variable then it goes empty and lose its current value . for example:

var a = null;
console.log(a); // Output: null

var b = "Hello World!"
console.log(b); // Output: Hello World!

b = null;
console.log(b) // Output: null

6. The Object Data Type

The object is a complex data type that allows to store collections of data.

An object contains properties as a key-value pair. Property key (which consider as name) is always a string, but the value is any data type. ( like - Numbers, Strings, Booleans, function, and, arrays ) for example:

var emptyObject = {};
var person = {"name": "Ayush", "surname": "Kumar", "age": "23"};

// For better reading
var car = {
    "modal": "Mahindra SUV",
    "color": "Gray",
    "doors": 5
}

7 . The Array Data Type

In the last we have take a look on Array data types , If we want to store 100 variable in our program so we have to make 100 variable for that , which is no a good practice so we used array to store data in it . An array used for storing multiple values in one variable. Each value in an array has a numerical position, known as its index, and contains data of data type like - Strings, Numbers, Booleans, Objects, Functions, and other arrays also. Array index starts from 0, so that the first array element is array[0] not array[1].

famous quotes in programming world:-

Real Programmers count from 0 ๐Ÿ˜Š๐Ÿ˜Š

The simplest way to create an array is by specifying the array elements as a comma-separated list enclosed by square brackets, as shown in the example below:

var colors = ["Red", "Yellow", "Green", "Orange"];
var cities = ["Mumbai", "Delhi", "Bangalore"];

console.log(colors[0]);   // Output: Red
console.log(cities[2]);   // Output: Bangalore

so here we have finished all Data types in JavaScript in a efficient way . So that's all for this blog . I hope that you enjoyed reading this blog . I would really appreciate any feedback or future topic requests from you guys.

Thanks You for reading this