10 JavaScript Question You Need To Know

Shakibshalim
3 min readMay 8, 2021

Truthy and falsy in javascript: JavaScript sets a value to one of primitive data types like Undefined, Null, Boolean, Number, String, and Symbol. Everything else in the Object includes an array also. We generally know the value truthy or falsy.

* These values are always falsy —

false

0

‘’ or “”

null

undefined

NaN

* Everything else in truthy —

‘0’

‘false’

[]

{}

function(){}

Null vs Undefined: There two types null and undefined. You can simply identify null and undefined by using some code. Null is an empty value or non exist and undefined means variable been declared but not defined. Let me explain in example:

Example for null:

let number = null;

console.log(number);

the result shows: null;

Example for undefined:

let number = undefined;

console.log(number);

the result shows: undefined;

Double equal vs triple equal: == and === equal is mostly same but there are significantly difference == means loose quality and === means strong quality. if you have a string and use triple equal with match other string it will match but if you use double equal you can match string to a number and any other. You can also match string to string.

Example:

let number = 0;

let string = 0;

console.log(number === number) // true

console.log(string === string) // true

let number = 0;

let string = ‘0’;

console.log(number == string) // true

console.log(string == string) // true

Difference between call, bind and apply: Many people think call and apply are the same. But many reasons you need to know for applying this method. There are different case have different method if you want to use call method you can use directly. You can use call and apply when the time of function execution and bind method you use when times of binding.

Understand this keyword: This keyword in JavaScript is a core concept of the language that's why beginners have confused about it. Every execution context references an object and that object references this keyword and then this keyword refers to the object called the function.

Example:

let color = ‘yellow’;function colors() {const color = ‘red’;console.log(color);  // redconsole.log(‘this.color’, this.color);  // this.color yellow}colors();

setTimeout and setInterval uses: setTimeout is mostly used when you set a sidebar or any pop-up notification and others. When you write anything like hello, bye, etc you show them one by one like you set timeout hello for 3 sec after 3 sec hello gone and bye come. setInterval is a useful feature when you set 1 to 100 for a set interval then it's run like 1, 2, 3,4, and it runs away. Ok in summary setTimeout means your time is out and that's gone. Like exam timeout. setInterval is like a clock 1 2 3 4.

Example:

//setTimeoutlet greeting = setTimeout(function() {alert (‘Hello!’);}, 2000)//setIntervalvar count = 0;var interval = setInterval(counter, 1000);function counter() {console.log(++count);

Find the largest element of an array: It's very simple to find the largest element of an array just use math.max () and you will get the largest element.

Example:

console.log(Math.max(1,2,3,4,5,6));

Remove duplicate item from an array: if you have any duplicate item in an array you can easily remove them by using a simple code.

Example:

let names = [‘akash’, ‘kumar’, ‘sojib’, ‘rohim’, ‘kabir’];let duplicate = […new Set(names)];console.log(duplicate);

Count the number of words in a string: You can count string by using the split method. first, declare a function then return it as a length then console to check it easily.

Example:

function count(string) {return string.split( “ ”).length;}console.log(string(“hello”));

Calculate Factorial of a number: First explain what is factorial think you have a number of 2 what is the factorial of number 2? answer if you have 0 the factorial is 1, 2! = 2, 3!= 6, 4! = 24, 5! = 120. 5! = 5 * 4* 3* 2* 1.

Example:

function factorial (num) {if (num === 0 || num === 1)return 1;for (var i = num -1; i> =1; i- -){num * = i;}return num;}factorial(4);

--

--