JavaScript interview questions

Puja DebnathPuja Debnath
2 min read
  1. console.log("5" > "11") =>true
    console.log("5" > 11) =>false
    console.log(" " > -1) =>true
    console.log("you " > 1) =>false
    Ans: for string letter by letter comparison takes place, when values of different types are compared, and they get converted into numbers, here empty string will be converted into 0 , and "you" becomes NaN, which is neither greater than nor less than, or any num

  2. const Person = { name:"puja" , age"23" , name:"broooo" }
    Ans:-if a var is overridden then the memory will be dedicated to the last value

  3. const Person1 = {name:"puja", age:23 },const Person2 = {name:"puja", age:23}
    consoe.log(Person1 == person2) =>false
    consoe.log(Person1 == person2) =>false
    Ans: they both have different memory allocation(=== check for the same value )

  4. convert a multidimensional array into a single array
    Ans: there are 3 ways ------flat , reduce, spread

const answer = (numbers) =>{
return numbers.reduce(acc, curr) =>{
return acc.concat(Array.isArray(curr) ? answer(num) : num ) ,
},[] ) }
console.log(numbers.flat(Infinity))

  1. console.log(1+2+"3" +4+5) => 3345
    consol.elog(1+2+3+"1") =>61
    Ans: When string and integer together concat happen, otherwise add

  2. Accessing a block scope variable before its declaration results in a Reference error. This is known as the "temporal dead zone." Even though the variable exists in the scope, accessing it before its declaration or initialization results in an error.

  3. for(var I = 0 ; I<3 ; i++){setTimeout(() =>consoele.log(i) , 100 )
    since var is global var the time the first loop will be completed I value will be 3 sp 3 will be rint everytime

0
Subscribe to my newsletter

Read articles from Puja Debnath directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Puja Debnath
Puja Debnath