10 JavaScript Interview Questions

1. Truthy value, Falsy value

Example :

let number = 10;if(number) {console.log(“Data is true”);}else{console.log(“Data is false”)}

Some extra Example

2. Null vs Undefined

Some examples :

let name;console.log(name); // undefined;
function name(){console.log(“your name”)}let youName = name();console.log(yourName); // your name , undefined
const person = {name: “mahfuj”,age: 21,}console.log(person.salary); // undefined

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

Example :

let a = 10;let b =”10”;
if(a == b){console.log(“true”);}esle{console.log(“false”)}// it is forced to match a and b. Because both have the same value. And it returns “true”
if(a === b){console.log(“true”);}esle{console.log(“false”)}// it returns “false”. Because both have the same value but not the same data type. One is a number and one is a string data type.

4. this key

Example :

class Person {constructor(name, age) {this.name = name;this.age = age;}deposit = function () {console.log(`My name is ${this.name}. I am ${this.age} year old`);};}const person1 = new Person(‘Mofasser Hossain’, 20);console.log(person1.deposit());

5. Closure

const firstName = ‘Mofasser’;function fullName() {const lastName = ‘Hossain’;return function () {console.log(firstName + lastName);};}const name = fullName();console.dir(name);// first name is global scope// second name is closure

6. Find Factorial number

Example :

If the number = 6;function factorial(n) {let factorial = 1;for (let i = 1; i <= n; i++) {factorial x= i;}return factorial;}console.log(factorial(6));// ans 720;

7. Find Factorial number recursive way

Example :

function factorial(n) {// default valueif (n == 0 || n == 1) {return 1;}else {return n x factorial(n — 1);}}console.log(factorial(4));
// n => 4// => 4 x factorial(3);// => 4 x 3 x factorial(2);// => 4 x 3 x 2 x factorial(1);// => 4 x 3 x 2 x 1x factorial(0);// => 4 x 3 x 2 x 1x 1;// factorial 4 = 24;

8. Count the number of words in a string

Solution

let speech =‘I am a Student. I am learning the JS programming Language. Now I am at Basic JS.’;let countNum = 0;for (i = 0; i < speech.length; i++) {let char = speech[i];if (char == ‘ ‘ && speech[i — 1] != ‘ ‘) {countNum++;}}countNum++;console.log(countNum);

9. Remove Duplicate number from an array

Solution

let array = [3, 32, 45, 76, 100, 1, 3, 32, 100, 1, 45];let newArray = [];for (let i = 0; i < array.length; i++) {let element = array[i];let newIndex = newArray.indexOf(element);if (newIndex == -1) {newArray.push(element);}}console.log(newArray);

10. Find the large number in the array

Solution

function getLargeNumber(num) {let larger = num[0];for (let i = 0; i < num.length; i++) {let element = num[i];if (element > larger) {larger = element;}}return larger;}let arry = [3, 32, 45, 76, 100, 1];let result = ‘Lager Number is ‘ + getLargeNumber(arry);console.log(result);

Web Designer and Front-end Developer