Return statement in next line


    let x = function() {
        return
        { msg: "hi" }
    }
    console.log(x()); // undefined

    /* encounters empty return. Hence returns nothing */
        

setTimeout


    console.log("1")
    setTimeout(function(){console.log("2")}, 1000)
    setTimeout(function(){console.log("3")}, 0)
    console.log("4")

    /* 1 4 3 2 */

    function x() {
        setTimeout(function(){console.log(i)}, 1000);
        var i = 1;
    }
    x();
    /* 1 */
        

Set Timeout 2


    let status = "hi";

    setTimeout(() => {
      status = "hello";
      console.log("1: ", status);       // hello
    }, 0);

    console.log("2: ", status);         // hi


    /* output */
    2: hi
    1: hello
        

Set Timeout 3


    function func() {
        let i;
        for(i = 0; i < 3; i++)
            setTimeout(() => {
              console.log(i);
            }, i*100);
    }

    func();                            // 3 3 3
        

Set Timeout 3


    var x = 5, y = 1;
    var obj = {x: 10};

    with(obj) {
        console.log(x, y);       // 10 1
    }

        

Map and ParseInt

Explain the output.


    ['1', '5', '11'].map(parseInt)          /* [1, NaN, 3] */
        
  • Function Signature of parseInt: can take up to two arguments: parseInt(string, radix)
    • string: the string to parse
    • radix: the base of the mathematical numeral systems used
  • Behavior of map: calls the provided function with three arguments
    • current array element
    • index of that element
    • the entire array
  • Application to Each Element
    • parseInt('1', 0) is called
      • In JavaScript, a radix of 0 (or omitted) means the function decides the radix based on the input string.
      • Since '1' doesn't start with '0x' or '0', it's treated as a decimal (base 10)
      • so it returns 1.
    • parseInt('5', 1) is called
      • A radix of 1 is not valid (the radix must be between 2 and 36)
      • so parseInt returns NaN.
    • parseInt('11', 2) is called
      • the function interprets the string '11' as a binary number (base 2)
      • which converts to decimal 3.

Different ways of calling a function

Direct Invocation


    function show() {
        console.log('show function');
    }

    show();
        

Using call and apply.


    function show() {
        console.log('show function');
    }

    show.call({});
    show.apply({});
        

Using setTimeout


    function show() {
        console.log('show function');
    }

    setTimeout(show, 0)
        

Using new keyword


    function show() {
        console.log('show function');
    }

    new show();
        

Calling a function without using Parentheses

Using New keyword

The parenthesis are only required when we want to pass some default values.

    function show() {
        console.log('show function');
    }

    new show;
        

Using tagged template literal


    function show() {
        console.log('show function');
    }

    show``;
        

Using getter property in object


    const obj = {
        show: 1,
        get show() {
            console.log('show function');
        }
    }

    obj.show;