DEV Community

Santhosh Kumar
Santhosh Kumar

Posted on

JAVASCRIPT: DATA TYPES

This post was originally published on my blog, find original post here

Every variable has a data type that determines type of value it stores, a variable of type number can store an integer or floating-point numbers.

JavaScript is dynamically typed language which means a variable can be number type for a moment and at another be a string type.

Number

number type represents both integer and fractional (floating point numbers).

let num = 79; // integer
let num = 65.45; // floating-point

We can perform arithmetic operations like +, -, *, /.

let num = 45 + 2 * 6; // 57

For very big number or very small number, we can use e (exponent)

let speedOfLight = 2.998e8;

Infinity represents mathematical infinity, this is a special numeric value.

let infinity = 1 / 0; 
console.log(infinity); // Infinity

NaN is also a special numeric value to represent the incorrect or invalid mathematical operation.

let nan = "str" / 79;

We can get the data type of a variable using typeof.

console.log(typeof "peace"); //string

String

string is used to represent text. They are written by eclosing them their content in quotes.

let hello = "Hello World";
let sq = 'sinle quote';

Both single quote and double quote can be used.

Backticks represents string template, we can embed variable and expression in a string by enclosing them in ${ variable or expression }
expression inside ${....} is evaluated and becomes a part of the string.

let name = "Kevin";
console.log(`My name is ${ name } `); // My name is kevin
console.log(`Age is ${17 + 1} `); // Age is 18

Boolean

Boolean type has two values only: true and false.

It is used to store yes/ no values.

let isGreater = true;

null

To represent nothingness, empty, we can use null

let answer = null;

undefined

if a value is not assigned to a variable or variable is not declared , it will have undefined .

Meaning of undefined is "value is not assigned".

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

Top comments (0)