FrontendDeveloper.in

JavaScript Interview Questions

  • Question 316

    What are default values in destructuring assignment

    A variable can be assigned a default value when the value unpacked from the array or object is undefined during destructuring assignment. It helps to avoid setting default values separately for each assignment. Let's take an example for both arrays and object use cases,

    Arrays destructuring:

    var x, y, z;
    
    [x = 2, y = 4, z = 6] = [10];
    console.log(x); // 10
    console.log(y); // 4
    console.log(z); // 6
    

    Objects destructuring:

    var { x = 2, y = 4, z = 6 } = { x: 10 };
    
    console.log(x); // 10
    console.log(y); // 4
    console.log(z); // 6
    
  • Question 317

    How do you swap variables in destructuring assignment

    If you don't use destructuring assignment, swapping two values requires a temporary variable. Whereas using a destructuring feature, two variable values can be swapped in one destructuring expression. Let's swap two number variables in array destructuring assignment,

    var x = 10,
    y = 20;
    
    [x, y] = [y, x];
    console.log(x); // 20
    console.log(y); // 10
    
  • Question 318

    What are enhanced object literals

    Object literals make it easy to quickly create objects with properties inside the curly braces. For example, it provides shorter syntax for common object property definition as below.

    //ES6
    var x = 10,
    y = 20;
    obj = { x, y };
    console.log(obj); // {x: 10, y:20}
    //ES5
    var x = 10,
    y = 20;
    obj = { x: x, y: y };
    console.log(obj); // {x: 10, y:20}
    
  • Question 319

    What are dynamic imports

    The dynamic imports using import() function syntax allows us to load modules on demand by using promises or the async/await syntax. Currently this feature is in stage4 proposal. The main advantage of dynamic imports is reduction of our bundle's sizes, the size/payload response of our requests and overall improvements in the user experience. The syntax of dynamic imports would be as below,

    import("./Module").then((Module) => Module.method());
    
  • Question 320

    What are the use cases for dynamic imports

    Below are some of the use cases of using dynamic imports over static imports,

    1. Import a module on-demand or conditionally. For example, if you want to load a polyfill on legacy browser
    if (isLegacyBrowser()) {
    import(···)
    .then(···);
    }
    
    1. Compute the module specifier at runtime. For example, you can use it for internationalization.
    import(`messages_${getLocale()}.js`).then(···);
    
    1. Import a module from within a regular script instead a module.
  • Question 321

    What are typed arrays

    Typed arrays are array-like objects from ECMAScript 6 API for handling binary data. JavaScript provides 12 Typed array types,

    1. Int8Array: An array of 8-bit signed integers
    2. Uint8Array: An array of 8-bit unsigned integers
    3. Uint8ClampedArray: An array of 8-bit unsigned integers clamped to 0-255
    4. Int16Array: An array of 16-bit signed integers
    5. Uint16Array: An array of 16-bit unsigned integers
    6. Int32Array: An array of 32-bit signed integers
    7. Uint32Array: An array of 32-bit unsigned integers
    8. BigInt64Array: An array of 64-bit signed BigInts
    9. BigUint64Array: An array of 64-bit unsigned BigInts
    10. Float16Array: An array of 16-bit floating point numbers
    11. Float32Array: An array of 32-bit floating point numbers
    12. Float64Array: An array of 64-bit floating point numbers

    For example, you can create an array of 8-bit signed integers as below

    const a = new Int8Array();
    // You can pre-allocate n bytes
    const bytes = 1024;
    const a = new Int8Array(bytes);
    
  • Question 323

    What is collation

    Collation is used for sorting a set of strings and searching within a set of strings. It is parameterized by locale and aware of Unicode. Let's take comparison and sorting features,

    1. Comparison:
    var list = ["ä", "a", "z"]; // In German,  "ä" sorts with "a" Whereas in Swedish, "ä" sorts after "z"
    var l10nDE = new Intl.Collator("de");
    var l10nSV = new Intl.Collator("sv");
    console.log(l10nDE.compare("ä", "z") === -1); // true
    console.log(l10nSV.compare("ä", "z") === +1); // true
    
    1. Sorting:
    var list = ["ä", "a", "z"]; // In German,  "ä" sorts with "a" Whereas in Swedish, "ä" sorts after "z"
    var l10nDE = new Intl.Collator("de");
    var l10nSV = new Intl.Collator("sv");
    console.log(list.sort(l10nDE.compare)); // [ "a", "ä", "z" ]
    console.log(list.sort(l10nSV.compare)); // [ "a", "z", "ä" ]
    
  • Question 324

    What is for...of statement

    The for...of statement creates a loop iterating over iterable objects or elements such as built-in String, Array, Array-like objects (like arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. The basic usage of for...of statement on arrays would be as below,

    let arrayIterable = [10, 20, 30, 40, 50];
    
    for (let value of arrayIterable) {
    value++;
    console.log(value); // 11 21 31 41 51
    }
    
  • Question 325

    What is the output of below spread operator array

    [..."John Resig"];
    

    The output of the array is ['J', 'o', 'h', 'n', ' ', 'R', 'e', 's', 'i', 'g']

    Explanation: The string is an iterable type and the spread operator within an array maps every character of an iterable to one element. Hence, each character of a string becomes an element within an Array.

  • Question 326

    Is PostMessage secure

    Yes, postMessages can be considered very secure as long as the programmer/developer is careful about checking the origin and source of an arriving message. But if you try to send/receive a message without verifying its source will create cross-site scripting attacks.

  • Question 327

    What are the problems with postmessage target origin as wildcard

    The second argument of postMessage method specifies which origin is allowed to receive the message. If you use the wildcard “*” as an argument then any origin is allowed to receive the message. In this case, there is no way for the sender window to know if the target window is at the target origin when sending the message. If the target window has been navigated to another origin, the other origin would receive the data. Hence, this may lead to XSS vulnerabilities.

    targetWindow.postMessage(message, "*");
    
  • Question 328

    How do you avoid receiving postMessages from attackers

    Since the listener listens for any message, an attacker can trick the application by sending a message from the attacker’s origin, which gives an impression that the receiver received the message from the actual sender’s window. You can avoid this issue by validating the origin of the message on the receiver's end using the “message.origin” attribute.

    For example, let's check the sender's origin http://www.some-sender.com on receiver side www.some-receiver.com,

    //Listener on http://www.some-receiver.com/
    window.addEventListener("message", function(message){
    if(/^http://www\.some-sender\.com$/.test(message.origin)){
    console.log('You received the data from valid sender', message.data);
    }
    });
    
  • Question 329

    Can I avoid using postMessages completely

    You cannot avoid using postMessages completely(or 100%). Even though your application doesn’t use postMessage considering the risks, a lot of third party scripts use postMessage to communicate with the third party service. So your application might be using postMessage without your knowledge.

  • Question 330

    Is postMessages synchronous

    The postMessages are synchronous in IE8 browser but they are asynchronous in IE9 and all other modern browsers (i.e, IE9+, Firefox, Chrome, Safari).Due to this asynchronous behaviour, we use a callback mechanism when the postMessage is returned.

Get LinkedIn Premium at Rs 399