FrontendDeveloper.in

JavaScript Interview Questions

  • Question 196

    What is the purpose of the seal method

    The Object.seal() method is used to seal an object, by preventing new properties from being added to it and marking all existing properties as non-configurable. But values of present properties can still be changed as long as they are writable. The next level of immutability would be the Object.freeze() method. Let's see the below example to understand more about seal() method

    const object = {
    property: "Welcome JS world",
    };
    Object.seal(object);
    object.property = "Welcome to object world";
    console.log(Object.isSealed(object)); // true
    delete object.property; // You cannot delete when sealed
    console.log(object.property); //Welcome to object world
    
  • Question 198

    What are the differences between the freeze and seal methods

    If an object is frozen using the Object.freeze() method then its properties become immutable and no changes can be made in them whereas if an object is sealed using the Object.seal() method then the changes can be made in the existing properties of the object.

  • Question 199

    How do you determine if an object is sealed or not

    The Object.isSealed() method is used to determine if an object is sealed or not. An object is sealed if all of the below conditions hold true

    1. If it is not extensible.
    2. If all of its properties are non-configurable.
    3. If it is not removable (but not necessarily non-writable). Let's see it in the action
    const object = {
    property: "Hello, Good morning",
    };
    
    Object.seal(object); // Using seal() method to seal the object
    
    console.log(Object.isSealed(object)); // checking whether the object is sealed or not
    
  • Question 200

    How do you get enumerable key and value pairs

    The Object.entries() method is used to return an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop. Let's see the functionality of object.entries() method in an example,

    const object = {
    a: "Good morning",
    b: 100,
    };
    
    for (let [key, value] of Object.entries(object)) {
    console.log(`${key}: ${value}`); // a: 'Good morning'
    // b: 100
    }
    

    Note: The order is not guaranteed as object defined.

  • Question 202

    How can you get the list of keys of any object

    You can use the Object.keys() method which is used to return an array of a given object's own property names, in the same order as we get with a normal loop. For example, you can get the keys of a user object,

    const user = {
    name: "John",
    gender: "male",
    age: 40,
    };
    
    console.log(Object.keys(user)); //['name', 'gender', 'age']
    
  • Question 203

    How do you create an object with a prototype

    The Object.create() method is used to create a new object with the specified prototype object and properties. i.e, It uses an existing object as the prototype of the newly created object. It returns a new object with the specified prototype object and properties.

    const user = {
    name: "John",
    printInfo: function () {
    console.log(`My name is ${this.name}.`);
    },
    };
    
    const admin = Object.create(user);
    
    admin.name = "Nick"; // Remember that "name" is a property set on "admin" but not on "user" object
    
    admin.printInfo(); // My name is Nick
    
  • Question 204

    What is a WeakSet

    A WeakSet is used to store a collection of weakly(weak references) held objects. The syntax would be as follows,

    new WeakSet([iterable]);
    

    Let's see the below example to explain it's behavior,

    var ws = new WeakSet();
    var user = {};
    ws.add(user);
    ws.has(user); // true
    ws.delete(user); // removes user from the set
    ws.has(user); // false, user has been removed
    
  • Question 205

    What are the differences between WeakSet and Set

    The main difference is that references to objects in Set are strong while references to objects in WeakSet are weak. i.e, An object in WeakSet can be garbage collected if there is no other reference to it. Other differences are:

    1. Set can store any value whereas WeakSet can store only collections of objects
    2. WeakSet does not have size property unlike Set
    3. WeakSet does not have methods such as clear, keys, values, entries, forEach.
    4. WeakSet is not iterable.
  • Question 206

    List down the collection of methods available on WeakSet

    Below are the list of methods available on WeakSet,

    1. add(value): A new object is appended with the given value
    2. delete(value): Deletes the value from the collection.
    3. has(value): It returns true if the value is present in the collection, otherwise it returns false.

    Let's see the functionality of all the above methods in an example,

    var weakSetObject = new WeakSet();
    var firstObject = {};
    var secondObject = {};
    // add(value)
    weakSetObject.add(firstObject);
    weakSetObject.add(secondObject);
    console.log(weakSetObject.has(firstObject)); //true
    weakSetObject.delete(secondObject);
    
  • Question 207

    What is a WeakMap

    A WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. In this case, keys must be objects and the values can be arbitrary values. The syntax looks like the following:

    new WeakMap([iterable]);
    

    Let's see the below example to explain it's behavior,

    var ws = new WeakMap();
    var user = {};
    ws.set(user);
    ws.has(user); // true
    ws.delete(user); // removes user from the map
    ws.has(user); // false, user has been removed
    
  • Question 208

    What are the differences between WeakMap and Map

    The main difference is that references to key objects in Map are strong while references to key objects in WeakMap are weak. i.e, A key object in WeakMap can be garbage collected if there is no other reference to it. Other differences are,

    1. Map can store any key type whereas WeakMap can store only collections of key objects
    2. WeakMap does not have size property unlike Map
    3. WeakMap does not have methods such as clear, keys, values, entries, forEach.
    4. WeakMap is not iterable.
  • Question 209

    List down the collection of methods available on WeakMap

    Below are the list of methods available on WeakMap,

    1. set(key, value): Sets the value for the key in the WeakMap object. Returns the WeakMap object.
    2. delete(key): Removes any value associated to the key.
    3. has(key): Returns a Boolean asserting whether a value has been associated to the key in the WeakMap object or not.
    4. get(key): Returns the value associated to the key, or undefined if there is none. Let's see the functionality of all the above methods in an example,
    var weakMapObject = new WeakMap();
    var firstObject = {};
    var secondObject = {};
    // set(key, value)
    weakMapObject.set(firstObject, "John");
    weakMapObject.set(secondObject, 100);
    console.log(weakMapObject.has(firstObject)); //true
    console.log(weakMapObject.get(firstObject)); // John
    weakMapObject.delete(secondObject);
    
  • Question 210

    What is the purpose of uneval

    The uneval() is an builtin function which is used to create a string representation of the source code of an Object. It is a top-level function and is not associated with any object. Let's see the below example to know more about it's functionality,

    var a = 1;
    uneval(a); // returns a String containing 1
    uneval(function user() {}); // returns "(function user(){})"
    

    The uneval() function has been deprecated. It is recommended to use toString() for functions and JSON.stringify() for other cases.

    function user() {}
    console.log(user.toString()); // returns "(function user(){})"
    
Get LinkedIn Premium at Rs 399