Data Types and Variables in JavaScript

A.S.M.Hasan Sarker
2 min readMay 5, 2021

There are different types of data in JavaScript. They are :
1.Number
2.String
3.Boolean

4.Object
a.Function
b.Array
c.Date
d.RegExp

5.Symbol (new in ES2015)
6.Undefined
7.Null

These data types are described below:

Numbers: There are two built-in numeric types in ES6. They are: Number and BigInt

Number: The Number type is a 64-bit binary format(numbers between -(2⁵³ − 1) and 2⁵³ − 1).

Some number related functions:

1. parseInt(): We can convert a string to a number using the parseInt() function. We can control the base in this conversion using a second optional argument.

Example:

console.log(parseInt(‘123’, 10)); // 123
console.log(parseInt(‘010’, 10)); // 10

If we want to convert a binary number to an integer, we need to change the base:

console.log(parseInt(‘11’, 2)); // 3

Similarly, we can parse floating-point numbers using the built-in parseFloat() function, and parseFloat() always uses base 10.

console.log(parseFloat(42)); // 42

2.NaN(Not a Number): It’s a particular type of value. If we want to convert any text into a number, it will return a NaN value.

Example: console.log(parseInt(“Hello”, 10)); //NaN

It will also return a NaN value, if we add a number with a NaN value.
Example: console.log(NaN + 5); // NaN

3.Number.isNaN(): Using this function, we can check anything is a number or not. It returns a boolean value. Boolean value means True or False.

Some examples are given here.

Number.isNaN(NaN); // true
Number.isNaN(‘hello’); // false
Number.isNaN(‘1’); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN([1]) // false
Number.isNaN([1,2]) // false

4.Infinity: It’s a particular type of value.
Example: console.log(1 / 0); // Infinity

5. -Infinity: It’s also a particular type of value.
Example: console.log(-1 / 0); // Infinity

6.isFinite(): To check is it infinity or not, we use isFinite() function.
Example:

isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

Strings:
Strings in JavaScript are sequences of Unicode characters.

7.string.Length: To find the length of a string, access its length property:
Example: console.log(“Hello”.length); //5

8.string.charAt(): Find a character using index number charAt(index_number) function:
‘hello’.charAt(0); // “h”

9.string.replace(): Replace a string using replace() function:
‘hello, world’.replace(‘world’, ‘mars’); // “hello, mars”

10.string.toUpperCase(): change string uppercase from lowercase using toUpperCase():
‘hello’.toUpperCase(); // “HELLO”

11.Variables:
New variables in JavaScript are declared using one of three keywords: var, let and const.

Now we discuss the difference between these three keyword.
i. var: Before ES6, var is used to declare a variable. But there are some limitations to use the var keyword. Using var, we can redeclare the variable as well as reassign the value.
Example: var a = 25;
var a = 20; // No error.

ii. let: Using the let keyword, we can not redeclare the variable, but we can reassign the value in that variable.
Example: let a = 25;
let a = 20; // Error
a = 20; // No error

iii. const: Using const keyword we can not redeclear the variable as well as we can not reassign value in that variable.
Example : const a = 25;
const a = 20; // error
a = 20; // error

--

--