Home Blog Work

Javascript ES6 Interview Questions

Published on December 6, 2021

const vs Object.freeze()

Const creates immutable binding for variable.

const car = 'Mazda CX-5';

car = 'Toyota Bz4' // ERROR "car" is read-only


const car = {
	make: 'Mazda'
car.make = 'Toyota' 

console.log(car); // {make: 'Toyota'}

This won’t throw any error. To Free the whole Object we would use.

const car = { make: 'Mazda' }

car.make = 'Toyota' // This won't throw any error but won't change the value.
console.log(car) // {make: 'Mazda'}

Generator function

Generator functions generates an object over time.

function *CarsDB() {
	yield ({ 'id': '1', 'make': 'Mazda'})
	yield ({ 'id': '2', 'make': 'Toyota'})
    return null;

const cars = CarsDB();

console.log( //{id: '1', make: 'Mazda'}

console.log( //{id: '2', make: 'Toyota'}

Generator functions will always have * before function name and yield will pause the execution until the next() function call.

How to create unnamed Class expression

const Calc = class {
    constructor (num1, num2) {
    	this.num1 = num1;
    	this.num2 = num2;
    add() {
    	return this.num1 * this.num2;
console.log(new Product(5,8).add());