Data Type in JavaScript

Understand data types of javascript in depth in a simple way

Data Type in JavaScript

JavaScript types

javascript provide different data type to hold a different type of value. The set of types in JavaScript consists of two types of data primitive data type and nonprimitive data type.

  1. Primitive data type
  2. Non-primitive data type

Untitled-2022-09-07-0812.png

JavaScript has the primitive data types:

  • All types except objects define immutable values (that is, values that can't be changed).

FOR Example : There are different types of data that we can use in a JavaScript program.

let data = 5;  //data is a number
let b = "Hello";  //data is a number
let data = true; //data is boolean
let data = null;  //data is a null
  • To get the current type of the value that the variable stores, you use the typeof operator:

let data  = 5;  
consol.log(typeof(data)); //data is a number

let data  = "Hello";  
consol.log(typeof(data)); //data is a string

let data  = true; 
consol.log(typeof(data)); //data is boolean

let data  = null;  
consol.log(typeof(data)); //data is a null

Output :

"number" //5 is an integer data.
"string" // "Hello" is an string .
"boolean"//true is an boolean 
"null" // null is an null
  • There are 7 basic primitive data types in JavaScript. They are:
TablesDescriptionExample
1. Stringrepresents sequence of charactersdata="hello";
2. Numberrepresents numeric valueslet data =100;
3. Booleanrepresents boolean value either false or truelet a = true,false
4. Undefinedrepresents undefined valuelet a;
5. nullrepresents null i.e. no value at alllet a = null;
6. BigIntan integer with arbitrary precision900719925124740999n , 1n etc.
7. SymbolSymbol is unique and immutable.let value = Symbol('hello');

Let's see each data type one by one.

Javascript String :

String is used to store text. In JavaScript, strings are enclosed by quotes:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: `Hello` FOR Example :
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
  • Single quotes and double quotes are practically the same and you can use either of them.
  • Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

Javascript Number :

  • Number represents integer and floating numbers (decimals and exponentials). FOR Example :
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
  • A number type can also be +Infinity, -Infinity, and NaN (not a number). FOR Example :
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN

Output :

Infinity
-Infinity
NaN

JavaScript Boolean :

The boolean datatype variable holds only one value, which can be true or false, in lowercase.

Consider the following example:

Declares two variables that hold boolean values. FOR Example :

let dataChecked = true;
let valueCounted = false;

JavaScript Undefined :

The undefined data type represents value that is not assigned.

If a variable is declared but the value is not assigned, then the value of that variable will be undefined.

FOR Example :

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

Output :

undefined

JavaScript null :

null is a special value that represents empty or unknown value.

javascript defines that null is an empty object pointer.

null is not the same as NULL or Null.

FOR Example :

const number = null;
//The number variable is empty.

JavaScript Bigint :

In JavaScript, Number type can only represent numbers less than (253 - 1) and more than -(253 - 1). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is created by appending n to the end of an integer.

BigInt was introduced in the newer version of JavaScript and is not supported by many browsers including Safari.

JavaScript symbol :

his data type was introduced in a newer version of JavaScript (from ES2015).

A value having the data type Symbol can be referred to as a symbol value. Symbol is an immutable primitive value that is unique.

for example :

// two symbols with the same description

let value1 = Symbol('js');
let value2 = Symbol('js');

Though value1 and value2 both contain 'js', they are different as they are of the Symbol type

JavaScript has the non-primitive data types:

In javascript, non-primitive data types can hold collections of values and more complex entities.

Let’s discuss each one of them in detail.

TablesDescription
Objectrepresents instance through which we can access members
Arrayrepresents group of similar values
Functionrepresents boolean value either false or true
RegExrepresents regular expression
Daterepresents undefined value

JavaScript Object

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

avaScript objects are written with curly braces {}. The properties are stored in key value pairs. Property values can be values of any type, including other objects

For Example :

var product = {name: "Watch", price: 50.20, quantiy: 20, description: "It is product description"};

continue reading......

Did you find this article valuable?

Support priyanka chaudhari by becoming a sponsor. Any amount is appreciated!