FrontendDeveloper.in

JavaScript Interview Questions

Filter by topic:
  • There are two possible solutions to add new properties to an object.

    Let's take a simple object to explain these solutions.

    var object = {
    key1: value1,
    key2: value2,
    };
    
    1. Using dot notation: This solution is useful when you know the name of the property
    object.key3 = "value3";
    
    1. Using square bracket notation: This solution is useful when the name of the property is dynamically determined or the key's name is non-JS like "user-name"
    obj["key3"] = "value3";
    
  • No,that's not a special operator. But it is a combination of 2 standard operators one after the other,

    1. A logical not (!)
    2. A prefix decrement (--)

    At first, the value decremented by one and then tested to see if it is equal to zero or not for determining the truthy/falsy value.

  • You can use the logical or operator || in an assignment expression to provide a default value. The syntax looks like as below,

    var a = b || c;
    

    As per the above expression, variable 'a 'will get the value of 'c' only if 'b' is falsy (if is null, false, undefined, 0, empty string, or NaN), otherwise 'a' will get the value of 'b'.

  • You can define multiline string literals using the '\n' character followed by line terminator('').

    var str = "This is a \n very lengthy \n sentence!";
    console.log(str);
    

    But if you have a space after the '\n' character, there will be indentation inconsistencies.

  • An application shell (or app shell) architecture is one way to build a Progressive Web App that reliably and instantly loads on your users' screens, similar to what you see in native applications. It is useful for getting some initial HTML to the screen fast without a network.

  • Yes, we can define properties for functions because functions are also objects.

    fn = function (x) {
    //Function code goes here
    };
    
    fn.name = "John";
    
    fn.profile = function (y) {
    //Profile code goes here
    };
    
  • A polyfill is a piece of JS code used to provide modern functionality on older browsers that do not natively support it. For example, Silverlight plugin polyfill can be used to mimic the functionality of an HTML Canvas element on Microsoft Internet Explorer 7.

    There are two main polyfill libraries available,

    1. Core.js: It is a modular javascript library used for cutting-edge ECMAScript features.
    2. Polyfill.io: It provides polyfills that are required for browser needs.
  • The break statement is used to "jump out" of a loop. i.e, It breaks the loop and continues executing the code after the loop.

    for (i = 0; i < 10; i++) {
    if (i === 5) {
    break;
    }
    text += "Number: " + i + "
    ";
    }
    

    The continue statement is used to "jump over" one iteration in the loop. i.e, It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

    for (i = 0; i < 10; i++) {
    if (i === 5) {
    continue;
    }
    text += "Number: " + i + "
    ";
    }
    
  • The label statement allows us to name loops and blocks in JavaScript. We can then use these labels to refer back to the code later. For example, the below code with labels avoids printing the numbers when they are same,

    var i, j;
    
    loop1: for (i = 0; i < 3; i++) {
    loop2: for (j = 0; j < 3; j++) {
    if (i === j) {
    continue loop1;
    }
    console.log("i = " + i + ", j = " + j);
    }
    }
    
    // Output is:
    //   "i = 1, j = 0"
    //   "i = 2, j = 0"
    //   "i = 2, j = 1"
    
  • It is recommended to keep all declarations at the top of each script or function. The benefits of doing this are,

    1. Gives cleaner code
    2. It provides a single place to look for local variables
    3. Easy to avoid unwanted global variables
    4. It reduces the possibility of unwanted re-declarations
  • It is recommended to avoid creating new objects using new Object(). Instead you can initialize values based on it's type to create the objects.

    1. Assign {} instead of new Object()
    2. Assign "" instead of new String()
    3. Assign 0 instead of new Number()
    4. Assign false instead of new Boolean()
    5. Assign [] instead of new Array()
    6. Assign /()/ instead of new RegExp()
    7. Assign function (){} instead of new Function()

    You can define them as an example,

    var v1 = {};
    var v2 = "";
    var v3 = 0;
    var v4 = false;
    var v5 = [];
    var v6 = /()/;
    var v7 = function () {};
    
  • JSON arrays are written inside square brackets and arrays contain javascript objects. For example, the JSON array of users would be as below,

    "users":[
    {"firstName":"John", "lastName":"Abrahm"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Shane", "lastName":"Warn"}
    ]
    
  • You can use Math.random() with Math.floor() to return random integers. For example, if you want generate random integers between 1 to 10, the multiplication factor should be 10,

    Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10
    Math.floor(Math.random() * 100) + 1; // returns a random integer from 1 to 100
    

    Note: Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)