FrontendDeveloper.in

JavaScript Interview Questions

  • Question 136

    How do you add a key value pair in javascript

    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";
    
  • Question 137

    Is the !-- notation represents a special operator

    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.

  • Question 138

    How do you assign default values to variables

    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'.

  • Question 139

    How do you define multiline strings

    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.

  • Question 140

    What is an app shell model

    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.

  • Question 141

    Can we define properties for functions

    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
    };
    
  • Question 142

    What is the way to find the number of parameters expected by a function

    You can use function.length syntax to find the number of parameters expected by a function. Let's take an example of sum function to calculate the sum of numbers,

    function sum(num1, num2, num3, num4) {
    return num1 + num2 + num3 + num4;
    }
    sum.length; // 4 is the number of parameters expected.
    
  • Question 143

    What is a polyfill

    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.
  • Question 144

    What are break and continue statements

    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 + "
    ";
    }
    
  • Question 145

    What are js labels

    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"
    
  • Question 146

    What are the benefits of keeping declarations at the top

    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
  • Question 147

    What are the benefits of initializing variables

    It is recommended to initialize variables because of the below benefits,

    1. It gives cleaner code
    2. It provides a single place to initialize variables
    3. Avoid undefined values in the code
  • Question 148

    What are the recommendations to create new object

    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 () {};
    
  • Question 149

    How do you define JSON arrays

    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"}
    ]
    
  • Question 150

    How do you generate random integers

    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)

Get LinkedIn Premium at Rs 399