Skip to main content

Command Palette

Search for a command to run...

Data Types in JavaScript

Published
2 min read
Data Types in JavaScript

Data types describe the characteristics of data stored in a variable. Data types can be divided into two:

  1. Primitive Data types

  2. Non-primitive Data types

Primitive Data Types

Primitive data types are immutable or non-modifiable data types. Once a primitive data type is created we cannot modify it. Primitive data types in JavaScript include:-

  • Number

Number datatype can stores both integers and decimal values. Using Number data type we can do all the arithmetic operations.

let age = 21;
var quantity= 12;
const gravity = 9.81  // we use const for non-changing values
  • Strings

Strings data type are used to store textual data. To declare a string, we need a variable name, assignment operator, and a value under a single quote, double quote, or backtick quote.

let userName = "Khyati";
let city = 'Mumbai';
let language = 'JavaScript';
let space = ' ';           // an empty space string
  • Booleans

The Boolean data type can store only two values i.e. true and false.This type is commonly used to store yes or no types values.

var yes = true;
var no= false;
  • Null

null data type is special type of placeholder in JavaScript. Null value represents the intentional absence of any object value.

var vaule = null;
  • Undefined

The undefined data type is special type of placeholder in JavaScript, undefined means “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined.

var  userName;
console.log(userName);  // shows "undefined"

Non-primitive Data Types

Non-primitive data types are modifiable or mutable. We can modify the value of non-primitive data types even after it gets created. Non-primitive data types in JavaScript includes:-

  • Arrays

Arrays data type are used to store multiple values at a time in single variable. An array is a list of values store within a variable, and can access these values by referring to their index number.

let numbers = [1, 2, 3];
let fruits=['apple', 'banana', 'mango'];
  • Objects

Objects are the complex data type in JavaScript. Object tries to map real like things in programing. Object stores the data in form of key value pair. Where key is string and value can be anything.

let car={
name:'Honda City',
model: 'VMT' ,
color: 'Black',
}
  • Functions

Functions data type are like mini programs in JavaScript. Functions contains a block of code which gets executed when functions is invoked.

function add(){
var numOne=5;
var numTwo=6;
var sum =numOne + numTwo;
return sum ();
}