Interview Preparation: JavaScript

MD ARIF HOSSAIN
3 min readMay 13, 2020

--

Image from Google

What is Set?

Set objects are collections of unique values. Duplicate values are ignored, as the collection must have all unique values. The values can be primitive types or object references.

Also, NaN and undefined can also be stored in a Set

What is WeakMap in ES6?

The WeakMap is same as Map where it is a collection of key/value pairs. But in WeakMap , the keys must be objects and the values can be arbitrary values. The object references in the keys are held weakly, meaning that they are a target of garbage collection (GC) if there is no other reference to the object anymore. The WeakMap API is the same as the Map API.

What will be the output of the following code?

var output = (function(x){
delete x;
return x;
})(0);

console.log(output);

The output would be 0. The delete operator is used to delete properties from an object. Here x is not an object but a local variable. delete operators don't affect local variables.

What is Generator Function?

A generator is a function that produces a sequence of results instead of a single value. It has a function* or function *x syntax.

function* generator(i) {
yield i
yield i * 2
}
const gen = generator(2)console.log(gen.next().value) // 2
console.log(gen.next().value) // 4

What will be the output of the code below?

var x = { foo : 1};
var output = (function(){
delete x.foo;
return x.foo;
})();

console.log(output);

The output would be undefined. The delete operator is used to delete the property of an object. Here, x is an object which has the property foo, and as it is a self-invoking function, we will delete the foo property from object x. After doing so, when we try to reference a deleted property foo, the result isundefined.

What will be the output of the code below?

var z = 1, y = z = typeof y;
console.log(y);

The output would be undefined. According to the associativity rule, operators with the same precedence are processed based on the associativity property of the operator. Here, the associativity of the assignment operator is Right to Left, so typeof y will evaluate first , which is undefined. It will be assigned to z, and then y would be assigned the value of z and then z would be assigned the value 1.

typeof null

In JavaScript, typeof null returns ‘object’! It incorrectly suggests that null is an object. It is a bug in JS which causes much confusion. We also have to careful while checking if a data is object or not because null might give us a false answer. typeof bar === 'object' will return true if bar is set to null.

What are all the new changes in Object literal notations in ES6?

ES6 allows declaring object literals by providing shorthand syntax for initializing properties from variables and defining function methods. It also enables the ability to have computed property keys in an object literal definition.

var a = 'Khan', b = 5, c = {}//shorthand: const obj = {a:a, b:b, c:c}
const obj = {a, b, c}
//shorthand method
const obj = {
add(x, y) { return x + y }
}
//equls const obj = { 'Khan': 'Pro' }
const name = 'Khan'
const obj = {
[name]: 'Pro'
}

Write a JavaScript code to reverse a string

function reverseWords(str){
return str.split(' ').reverse();
}

Reverse a string without default function

const reverse = str => {
let result = ''
for(let i = str.length-1; i>=0; i--){
result += str[i]
}
return result
}
console.log( reverse('Hello, Khan') ) // nahK ,olleH

Hope you get the questions.

--

--