Skip to main content

Command Palette

Search for a command to run...

JS Interview Prep:

Published
4 min readView as Markdown
  • Expressions vs Statements:
    Statements do things such as : var x; function name(){}
    expressions calculate/produce values such as: 2 + 3
    program is a sequence of statements .

    we can skip statement part but not expression:
    such as: in if-else can skip else part but in ternary oprtr else part also produce value so cant skip.

  • Data Types:
    * Primitive 6 types:
    1. numbers
    2.string
    3.boolean
    4.null
    5.undefined
    6.symbol

  • Object:
    All other types are object in JS.

  • Primitive types are also have object behaviour such as:
    string.length can calculate length of strings.
    But the difference is Primitive types are compared by value and obj is compared by reference.

  • Autoboxing:

    if we give “salman”.length , output will be : 6, first string will be convert into obj then calculate length then give output.
    ex:
    console.log("hello".toUpperCase()); // "HELLO"
    Behind the scenes:
    console.log(new String("hello").toUpperCase()); // "HELLO"

  • Coercion:

    data types changes dynamically in JS:
    ex: “3” - 1 = 2 // converted into no

    “5” + “2” = 7 // string converted into numbers
    these are examples of implicit coercion.

  • Ex of explicit coercion:
    console.log(Number(“123”)) //string to number
    console.log(String(123)) //number to string
    console.log(Boolean(0)) // false

// Difference b/w null & undefined :
*null is assigned value, means vrble has value but null.
*undefined is unassigned value, vrble didnot assign any value.

NOTE: type of null in JS is object but in some cases it doesnot work.
ex: function countLength(obj){
if(typeof obj === object) return object.keys(obj).length;
}
console.log(countLength(null)); // throws an error cant convert undefined or null.
but if change in above cndtn as obj ≠== null then it will work.

//NaN & Infinity
→Infinity is highest possible value. typeof infinity is number.
→-Infinity is lowest possbile value.

used to put boundary line values
→Not a Number indicates the error occured, it represents error value.
→exception is NaN is not equal to itself, i.e NaN === NaN , false , means equal oprtr doesnot work on NaN.
→isNaN() is to check something is NaN or not, returns true or false.

* Counter Points to remember:

when typecasting to number:
String ko number me convert krnegh toh :
- ““ , empty string 0 me convert hoga
- alphabet, return NaN
undefined → NaN
null → 0

* SCOPE:
scope is enclosing context in which vrble is associated with a value and is accessible.

var vs let vs const:
var : global scope
let & const: block scope

understanding of scope:
ex1:

O/P:
abc
changed
NOTE: name is a module scope/global scope so is accessible in whole code. why bcoz name is not modified inside function scope.

O/P:
modified
modified

Interview Question:

Ans: 30 20 10

NOTE:

Behaviour of scope :
i. inner function can access outer variables but not vice-versa.( answer of first console.log() ).
ii. each fn scope creates its own local copy of vrble, shadowing outer ones (in case of same name variables. mtlb jis scope me variable create hua h usi ke value ko consider krega. aur agr us scope me varibale declare nhi h toh nearest parent scope me jo value hai varible ka usko consider krega. agr parent me declare nhi hua hai toh grand parent me wha bhi nhi toh global scope me jo h usi ko consider karega.)
iii. global variable remains accessible unless shadowed by inner ones.

Ex2:

O/P: undefined 20 10
\=> 2 important points to rember:

  1. *why undefined?
    coz of hoisting, inside first scope var a is declared but using it before declaration so undefined. if var a is not defined in first() scope then it will consider a = 10 as global scope.

  2. if “var a” or koi bhi vrble kahin bhi declare ho gya kisi outer funtion me toh inner function uske value ko consider karega(inner function me var a defined nhi hai iss case me).
    mtlb agr code aise first() ke inside:
    function second() { console.log(a); // ? } var a = 20;
    toh 20 hi print krta.

→var is function scoped.
→let & const if block scoped.