DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Rafi-Genon
Rafi-Genon

Posted on • Updated on

Top 10 JavaScript Interview Questions And Answers.

1. Truthy & Falsy Values

When a value is counted in the context of Boolean then which values show true we consider them as truthy and which show false we consider them as falsy value there are some example of falsy value (false, 0, -0, "", undefined, NaN)

2. Null Vs Undefined

When we define a variable but don't assign any value in it then javascript shows us undefined on the other hand, Null is assigned null means empty and explicitly means nothing

var abc;
console.log(abc);
// undefined

var xyz = null;
console.log(xyz);
// null
Enter fullscreen mode Exit fullscreen mode

3. Double Equals (==) vs Triple Equals (===)

Double Equals (==) check only value. It converts the type of value to match each other. on the other hand, Triple Equals (===) match both value and type.

const num = 1234 
const stringNum = '1234'  

console.log(num == stringNum) //true
console.log(num === stringNum)  //false
Enter fullscreen mode Exit fullscreen mode

4. Have a look at the difference between bind, call, apply

apply(): method call a function and pass all the arguments in a array
call(): method passes all the arguments with comma
bind(): returns a new function, allowing you to pass in an array and any number of arguments.

5. How JS code executes

Everything in JavaScript happens inside an "Execution Context”. When a code stared execution the execution context is created.
first of all Creation Phase or Memory Creation & after Code Execution Phase. In Creation Phase, JS create global variable for all diclared variable & after Execution Phase JS follow Call stack system

6. What is an API

Full form of API is Application Programming Interface. API allow two application to communicate each other. API delivers our request to the provider & provider response back to us.

7. Reverse a string

To reverse a string we will use three method these are split(), reverse(),join()

function reverseString(str) {
    // Step 1. Use the split() method to return a new array
    var splitString = str.split(""); // var splitString = "hello".split("");
    // ["h", "e", "l", "l", "o"]

    // Step 2. Use the reverse() method to reverse the new created array
    var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
    // ["o", "l", "l", "e", "h"]

    // Step 3. Use the join() method to join all elements of the array into a string
    var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
    // "olleh"

    //Step 4. Return the reversed string
    return joinArray; // "olleh"
}

reverseString("hello");
Enter fullscreen mode Exit fullscreen mode

8. Count the number of words in a string

var speech = "I am Rafi"
var count =0;
for(var i = 0; i< speech.length; i++){
    var char = speech[i];
    if(char === " "){
   count ++
  }
}
// expected count is 3
Enter fullscreen mode Exit fullscreen mode

9. is comming soon

Top comments (0)

Create an Account!

πŸ‘€ Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.