FrontendDeveloper.in

JavaScript Interview Questions

  • Question 361

    What is the output of prepend additive operator on falsy values

    If you prepend the additive(+) operator on falsy values(null, undefined, NaN, false, ""), the falsy value converts to a number value zero. Let's display them on browser console as below,

    console.log(+null); // 0
    console.log(+undefined); // NaN
    console.log(+false); // 0
    console.log(+NaN); // NaN
    console.log(+""); // 0
    
  • Question 362

    How do you create self string using special characters

    The self string can be formed with the combination of []()!+ characters. You need to remember the below conventions to achieve this pattern.

    1. Since Arrays are truthful values, negating the arrays will produce false: ![] === false
    2. As per JavaScript coercion rules, the addition of arrays together will toString them: [] + [] === ""
    3. Prepend an array with + operator will convert an array to false, the negation will make it true and finally converting the result will produce value '1': +(!(+[])) === 1

    By applying the above rules, we can derive below conditions

    (![] + [] === "false" + !+[]) === 1;
    

    Now the character pattern would be created as below,

    s               e               l               f
     ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^
    
     (![] + [])[3] + (![] + [])[4] + (![] + [])[2] + (![] + [])[0]
     ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^^^^^^^^^^^^^
    (![] + [])[+!+[]+!+[]+!+[]] +
    (![] + [])[+!+[]+!+[]+!+[]+!+[]] +
    (![] + [])[+!+[]+!+[]] +
    (![] + [])[+[]]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    (![]+[])[+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]]+(![]+[])[+[]]
    
  • Question 363

    How do you remove falsy values from an array

    You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and "") from the array.

    const myArray = [false, null, 1, 5, undefined];
    myArray.filter(Boolean); // [1, 5] // is same as myArray.filter(x => x);
    
  • Question 364

    How do you get unique values of an array

    You can get unique values of an array with the combination of Set and rest expression/spread(...) syntax.

    console.log([...new Set([1, 2, 4, 4, 3])]); // [1, 2, 4, 3]
    
  • Question 365

    What is destructuring aliases

    Sometimes you would like to have a destructured variable with a different name than the property name. In that case, you'll use a : newName to specify a name for the variable. This process is called destructuring aliases.

    const obj = { x: 1 };
    // Grabs obj.x as { otherName }
    const { x: otherName } = obj;
    
  • Question 366

    How do you map the array values without using map method

    You can map the array values without using the map method by just using the from method of Array. Let's map city names from Countries array,

    const countries = [
    { name: "India", capital: "Delhi" },
    { name: "US", capital: "Washington" },
    { name: "Russia", capital: "Moscow" },
    { name: "Singapore", capital: "Singapore" },
    { name: "China", capital: "Beijing" },
    { name: "France", capital: "Paris" },
    ];
    
    const cityNames = Array.from(countries, ({ capital }) => capital);
    console.log(cityNames); // ['Delhi, 'Washington', 'Moscow', 'Singapore', 'Beijing', 'Paris']
    
  • Question 367

    How do you empty an array

    You can empty an array quickly by setting the array length to zero.

    let cities = ["Singapore", "Delhi", "London"];
    cities.length = 0; // cities becomes []
    
  • Question 369

    What is the easiest way to convert an array to an object

    You can convert an array to an object with the same data using spread(...) operator.

    var fruits = ["banana", "apple", "orange", "watermelon"];
    var fruitsObject = { ...fruits };
    console.log(fruitsObject); // {0: "banana", 1: "apple", 2: "orange", 3: "watermelon"}
    
  • Question 370

    How do you create an array with some data

    You can create an array with some data or an array with the same values using fill method.

    var newArray = new Array(5).fill("0");
    console.log(newArray); // ["0", "0", "0", "0", "0"]
    
  • Question 371

    What are the placeholders from console object

    Below are the list of placeholders available from console object,

    1. %o — It takes an object,
    2. %s — It takes a string,
    3. %d — It is used for a decimal or integer These placeholders can be represented in the console.log as below
    const user = { name: "John", id: 1, city: "Delhi" };
    console.log(
    "Hello %s, your details %o are available in the object form",
    "John",
    user
    ); // Hello John, your details {name: "John", id: 1, city: "Delhi"} are available in object
    
  • Question 372

    Is it possible to add CSS to console messages

    Yes, you can apply CSS styles to console messages similar to html text on the web page.

    console.log(
    "%c The text has blue color, with large font and red background",
    "color: blue; font-size: x-large; background: red"
    );
    

    The text will be displayed as below, Screenshot

    Note: All CSS styles can be applied to console messages.

  • Question 373

    What is the purpose of dir method of console object

    The console.dir() is used to display an interactive list of the properties of the specified JavaScript object as JSON.

    const user = { name: "John", id: 1, city: "Delhi" };
    console.dir(user);
    

    The user object displayed in JSON representation Screenshot

  • Question 374

    Is it possible to debug HTML elements in console

    Yes, it is possible to get and debug HTML elements in the console just like inspecting elements.

    const element = document.getElementsByTagName("body")[0];
    console.log(element);
    

    It prints the HTML element in the console,

    Screenshot

  • Question 375

    How do you display data in a tabular format using console object

    The console.table() is used to display data in the console in a tabular format to visualize complex arrays or objects.

    const users = [
    { name: "John", id: 1, city: "Delhi" },
    { name: "Max", id: 2, city: "London" },
    { name: "Rod", id: 3, city: "Paris" },
    ];
    console.table(users);
    

    The data visualized in a table format,

    Screenshot Not: Remember that console.table() is not supported in IE.

Get LinkedIn Premium at Rs 399