Some Important JavaScript Questions

Md Piash
The Startup
Published in
5 min readMay 13, 2020

1. Remove duplicate members from an array?

There are multiple ways to remove duplicates from an array.
The simplest approach is to use the Set object which lets us store unique values of any type. In other words, Set will automatically remove duplicates for us.

const names = [‘Abrar’, ‘Piash’, ‘Asif’, ‘Rakib’, ‘Abrar’, ‘Rakib’];
let unique = […new Set(names)];
console.log(unique); // [ ‘Abrar’, ‘Piash’, ‘Asif’, ‘Rakib’ ]

Another option is to use filter().

const names = [‘Abrar’, ‘Piash’, ‘Asif’, ‘Rakib’, ‘Abrar’, ‘Rakib’];
let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
console.log(x(names)); // [ ‘Abrar’, ‘Piash’, ‘Asif’, ‘Rakib’ ]

2. What is a “closure” in JavaScript?

A closure is a feature in JavaScript where an inner function has access to the outer function’s variables a scope chain.
The closure has three scope chains:

it has access to its own scope — variables defined between its curly brackets
it has access to the outer function’s variables
it has access to the global variables

Here is an example:

var globalVar = “xyz”;

(function outerFunc(outerArg) {

var outerVar = ‘a’;

(function innerFunc(innerArg) {

var innerVar = ‘b’;

console.log(

“outerArg = “ + outerArg + “\n” +

“innerArg = “ + innerArg + “\n” +

“outerVar = “ + outerVar + “\n” +

“innerVar = “ + innerVar + “\n” +

“globalVar = “ + globalVar);

})(456);

})(123);

outerArg = 123
innerArg = 456
uterVar = a
innerVar = b
globalVar = xyz

In the above example, variables from innerFunc, outerFunc, and the global namespace are all in scope in the innerFunc. The above code will therefore produce the following output:

3.Verify a word as palindrome?

Palindrome is a word, phrase or sequence that reads the same backwards as forwards. Example: madam,wow.

function isPalindrome(str){

var i, len = str.length;

for(i =0; i<len/2; i++){

if (str[i]!== str[len -1 -i])

return false;

}

return true;

}

console.log( isPalindrome(‘madam’)); // true

console.log( isPalindrome(‘toyota’)); //false

4.What is Generator function?

Generators are a special class of functions that simplify the task of writing iterators. A generator is a function that produces a sequence of results instead of a single value, i.e you generate ​a series of values.

Example:

function * generatorFunction() {

console.log(‘This will be executed first.’);

yield ‘Hello, ‘;

console.log(‘I will be printed after the pause’);

yield ‘World!’;

}

const generatorObject = generatorFunction();
console.log(generatorObject.next().value);
console.log(generatorObject.next().value);
console.log(generatorObject.next().value);

// This will be executed first.
// Hello,
// I will be printed after the pause
// World!
// undefined

5. What is function hoisting in JavaScript?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.

6. What are some of the features of ES6?

Some of the ES6 features are given bellow:

Support for constants also known as “immutable variables”
Block-Scope support for both variables, constants, functions|
Arrow functions
Extended Parameter Handling
Template Literals and Extended Literals
Enhanced Regular Expression
Destructuring Assignment
Modules, Classes, Iterators, Generators
Enhanced Object Properties
Support for Map/Set & WeakMap/WeakSet
Promises, Meta-Programming ,Internationalization and Localization

7. How can you reliably test if a value is equal to NaN?

The NaN property represents a value that is “not a number”. This special value results from an operation that could not be performed either because one of the operands was non-numeric or because the result of the operation is non-numeric.

While this seems straightforward enough, there are a couple of somewhat surprising characteristics of NaN that can result in hair-pulling bugs if one is not aware of them.

For one thing, although NaN means “not a number”and its type is believe it or not Number:

console.log(typeof NaN === “number”); // logs “true”

Additionally, NaN compared to anything — even itself! — is false:

console.log(NaN === NaN); // logs “false”

A semi-reliable way to test whether a number is equal to NaN is with the built-in function isNaN(), but even using isNaN() is an imperfect solution.

A better solution would either be to use value !== value, which would only produce true if the value is equal to NaN. Also, ES6 offers a new Number.isNaN() function, which is a different and more reliable than the old global isNaN() function.

8.What is WeakSet?

WeakSet in JavaScript is used to store a collection of objects. It adapts the same properties of that of a set it does not store duplicates. The major difference of a WeakSet with the set is that a WeakSet is a collection of objects and not values of some particular type.

Syntax: new WeakSet(object).

9.Find missing number from unsorted array of integers.

We can use the sum of a linear series of n numbers = n*(n+1)/2
like:

function missingNumber(arr){

var n = arr.length+1,

sum = 0,

expectedSum = n* (n+1)/2;

for(var i = 0, len = arr.length; i < len; i++){

sum += arr[i];

}

return expectedSum — sum;

}

console.log(missingNumber([5, 2, 6, 1, 3]));

10.Permutation in JavaScript

A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For example, suppose we have a set of three letters: A, B, and C. We might ask how many ways we can arrange 2 letters from that set. Each possible arrangement would be an example of a permutation.

Example:

var permArr = [],

usedChars = [];

function permute(input) {

var i, ch;

for (i = 0; i < input.length; i++) {

ch = input.splice(i, 1)[0];

usedChars.push(ch);

if (input.length == 0) {

permArr.push(usedChars.slice());

}

permute(input);

input.splice(i, 0, ch);

usedChars.pop();

}

return permArr

};

console.log(permute([5, 3, 7, 1]));

//Outputs:
//[ 3, 7, 5, 1 ], [ 3, 7, 1, 5 ],
//[ 3, 1, 5, 7 ], [ 3, 1, 7, 5 ],
//[ 7, 5, 3, 1 ], [ 7, 5, 1, 3 ],
//[ 7, 3, 5, 1 ], [ 7, 3, 1, 5 ],
//[ 7, 1, 5, 3 ], [ 7, 1, 3, 5 ],
//[ 1, 5, 3, 7 ], [ 1, 5, 7, 3 ],
//[ 1, 3, 5, 7 ], [ 1, 3, 7, 5 ],
//[ 1, 7, 5, 3 ], [ 1, 7, 3, 5 ]

--

--