Javascript Important Questions #1

Javascript Important Questions #1

JavaScript Interview Series

·

3 min read

1.) What are the different data types present in JavaScript?

-> One can easily identify the data types in JavaScript using the typeof operator which returns the data type of the operand. Data types are divided into two types:-

a) Primitive Data Types - These data types only store the data of only one type. These include String, Number, BigInt, Boolean, undefined, null, and symbol.

1. String - Series of characters put under single or double quotes.

2. Number - JavaScript Number includes numbers with or without decimals.

3. BigInt - When a number exceeds the limit of the "Number" data type it usually comes under the BigInt category.

4. Boolean - A conditional data type that either returns true or false.

5. undefined - Undefined as per its name specifies a data type that has been declared or defined but not assigned.

6. null - Represents non-existing, empty, or blank values.

7. Symbol - It is a unique and immutable data type and may be used as an identifier for object properties. It was introduced in ES6.

typeof "John Doe" //returns "string"
typeof 12.4 //returns "number"
typeof false //returns "boolean"
typeof 1234543423871627487819365823710236284910212n //returns "bigint"
typeof undefined //returns "undefined"
typeof null //returns an "object"
typeof Symbol('hi') //returns "Symbol"

b) Non-Primitive data types - Primitive data types are limited to storing only a single value. For storing multiple and complex values non-primitive data types are used.

  1. Objects - Javascript objects are non-primitive data types that allow us to store multiple collections of data. Each member of the object acts as a key-value pair separated by commas enclosed under curly braces.

     const person = {
     name:"John Doe",
     age:21,
     email:"abc@gmail.com",
     }
    
    1. Arrays - An array is a variable that can hold more than one value in it. Data is stored as an ordered list.

       const arr1 = [10,'hello',true];
      
      1. RegEx- A regular expression is a pattern of characters that are generally used for pattern-matching functionalities on text.

Any data type which is not of primitive data type, when put with typeof operator returns as of Object type in JavaScript.

2.) undefined vs null vs not defined

  1. undefined - The undefined data type, means a variable has been declared but its value has not been assigned to it. When such variables are created, JavaScript automatically assigns the 'undefined' value to the variable. It is not an assignment value as it is not assigned by the user. When arithmetic operations are performed while using a not assigned variable, it returns NaN.

     var length = 10;
     var breadth; //undefined value
     var area = length*breadth;
     console.log(area); //NaN
    
  2. null - Primitive data type which represents an empty or blank value. It is not set by JavaScript unlike undefined. When the typeof operator is used for null, it returns an empty object. Null is treated as a bug in JavaScript, which if tried to solve, will ruin the entire codebase. To identify null, when performing arithmetic operations, it first converts the variable to 0.

var length = 10;
var breadth = null;
var area = length*breadth;
console.log(area); //ZERO
  1. not defined - The value of not defined is the same as that of undefined, but in that, it indicates that the variable or a function does not exist. "not defined" error is generally caused by accessing the variable out of the scope or by some typos. Undefined and not defined usually confuse us, but they both are two different things. An undefined indicates that a variable has been declared but not given a value, whereas in not defined the variable does not exist.

Conclusively, we got to understand the various data types present in JavaScript, i.e., primitive and non-primitive, and further about their subtypes. Then we get to know about one of the most asked questions, undefined vs null vs not defined. A quick overview of the above sections can help in last-minute recapitulation of such important topics.