10 JavaScript Interview Questions
1. Truthy value, Falsy value
Truthy value is something like that is evaluated to true. When you apply conditions in your code you need to know what is truthy and falsy in javascript and which values. In javascript, there is some default truthy and falsy condition available.
Example :
let number = 10;if(number) {console.log(“Data is true”);}else{console.log(“Data is false”)}
// answer true value.
Some extra Example
Truthy value =>
- Any number except zero . // 10
- A string with text. // “hello”
- A string with white space . // “ ”
- Empty objects and empty arrays. // {} , []
Falsy value =>
- Number zero (0) // 0
- Empty string. // “”
- Undefine .
- Null
- NaN
2. Null vs Undefined
The undefined property indicates that you declare a variable but not assigned its value or not declared at all. And null property indicates that your declared variable or object property does not exist. You can get undefined in many ways.
Some examples :
#Variable without assigned value.
let name;console.log(name); // undefined;
#The function doesn’t return anything
function name(){console.log(“your name”)}let youName = name();console.log(yourName); // your name , undefined
#An object does not have the property that you expect.
const person = {name: “mahfuj”,age: 21,}console.log(person.salary); // undefined
3. Double equal(==) vs Triple equal(===)
When you apply a condition with double equal(==) it only checks the value. If the value is matched, it returns true. When checking the condition with Triple equal(===) it not only checks the value but also checks the data type. If both are matched it returns true unless it returns false.
Example :
let a = 10;let b =”10”;
// double equal
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”
// Triple equal
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
If a method or function contains “this” keyword, then “this” will represent the object from which the method or function will be called.
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
The closure is a function with some remember values. A Closure gives you access to an outer function scope from an inner function.
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
The factorial function says to multiply all whole numbers from our chosen number down to 1. Like if our number is 3 factorial the answer will look like this — 3x2x1 = 6;
We can calculate it easily by for loop.
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
Recursion is that the function will call inside the function. It's called a call-back function. Factorial number main structure is if number is 2, factorial number will be 2 x (2–1) = 2; if the number is n = n x (n -1); recursive function needs a default value to stop the recursion loop.otherwise, it will never stop.
Example :
function factorial(n) {// default valueif (n == 0 || n == 1) {return 1;}else {return n x factorial(n — 1);}}console.log(factorial(4));
This recursive function works like this =>
// 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);