JavaScript Code


JavaScript Data Types


Data Types

Data types are useful as to how a value should be treated. For example we want JavaScript to handle how two numbers are added, and how two strings are added. This could be possible with the concept of data types.

In JavaScript, we have the following data types.

  • number
  • string
  • boolean
  • object
  • undefined
x = 84;             //number
x = "apple";        //string
x = true;           //boolean
x = {a: 10, b: 20}; //object
x = undefined;      //undefined

Numbers

number data type is used to hold numeric values. These numeric values can be with a decimal, or without a decimal.

let n = 25;
let pi = 3.14;

Strings

String is a sequence of characters. Single quotes are double quotes can be used to enclose the sequence of characters, and define a string literal.

let name = "Apple";
let car = 'TATA XUV 700';

Boolean

Boolean can have a value of either true or false. true is ON, false if OFF in terms of logic gates.

let name = "Apple";
let car = 'TATA XUV 700';

Object

An object is a collection of name:value pairs.

let myPhone = {name: "Apple", storage: 256; ram: 6};

undefined

undefined is the only value for the data type undefined. When a variable is not defined, then the variable is of type undefined.

let idk = undefined;