DEV Community

Sasireka
Sasireka

Posted on

Datatypes

1. Datatypes in JavaScript

There are two types of datatypes in JavaScript.

  • Primitive Datatype(immutable, single values)

  • Non-Primitive Datatype(mutable, complex structures)

Primitive Datatypes:

String - Value should be enclosed in single quotes('') or double quotes("").
let color = "Yellow";
let lastName = "Johnson";

Number - Integer and floating-point numbers, including special values like Infinity and NaN (Not-a-Number).
let length = 16;
let weight = 7.5;

Boolean - Only have the two Values : true or false.
let x = true;
let y = false;

Null - An intentional absence value for the particular variable.
let x = null;
let y = null;

Undefined - The variable is declared but not have any values for that variable.
let x;
let y;

BigInt - Used to represent the largest numbers.
let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345)

Non-Primitive Datatypes:

Object - A collection of key-value pairs of data.
const person = {
name : "Priya",
age : 22,
gender : "Female"
}

Array - The Array data type is a specialized form of an object used to store ordered lists of values. Arrays can store elements of different data types and are accessed by numerical indices. For example:
const colors = [“red”, “green”, “blue”];

Function - Functions are also objects in JavaScript. They can be assigned to variables, passed as arguments, or returned from other functions. For example:
function mark(a,b){
let mark1 = a;
let mark2 = b;
console.log(mark1+mark2)
}
mark(80,95)

Top comments (0)