Javascript Interview 101: What are the different data types is JS?

Javascript Interview 101: What are the different data types is JS?

·

2 min read

Javascript has seven types that are divided into two categories: primitive and no-primitive.

The primitive includes numbers, string, boolean, undefined, null, and non-primitive includes object and symbol.

The 'symbol' data type is new in JS. It has been included in the ES6 version.

JavaScript Numbers

Numbers represent integer and floating numbers (decimal 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

Javascript String

String is used to store text. In JS, string are surrounded by quotes.

Example: "hello", 'hello', `hello`.

Javascript Boolean

Boolean represents two values: true or false.

const isChecked = true;
const isOpen = false;

Javascript Undefined

The undefined data type represents value that is not assigned. If a variable is declared but the value is not assigned, the the value of that variable will be undefined.

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

Javascript null

Null is a special value that represents empty or unknow value

const number = null;

Note: null is not the same as NULL or Null

Javascript Symbol.

Symbol is an immutable primitive value that is unique.

const name1 = Symbol('Adam');
const name2 = Symbol('Adam')'

name1 and name2 both contain 'Adam' but they are different as they are of the Symbol type.

Javascript Object

An Object is a complex data type that allow we to store data follow by key-value. For example:

const person = {
    name: 'Adam',
    age: 10,
    address: 'Vietnam'
};

What is the difference between primitive and non-primitive?

  1. Primitive is defined by value.

  2. Non-primitive defined by reference.