JavaScript Learning Notes: Arrays


                    const user = {
                        username: "hitesh",
                        price: 999,
                    
                        welcomeMessage: function() {
                            console.log(`${this.username} , welcome to website`);
                            console.log(this);
                        }
                    
                    }
                

Here this refers to Current context




                   console.log(this); //In node env return {}
                   console.log(this); //In Browser return Window{}
                
thisnot works inisde 01_functions/code>

Arrow Function


                    const chai =  () => {
                        let username = "hitesh"
                        console.log(this);
                    }
                

Console Output:
{}


  • Implicit Arrow Functionconst addTwo = (num1, num2) => num1 + num2
  • In arrow functions, if the function body is just an expression, you can omit the return keyword and braces, and the expression will be implicitly returned.
  • 
                        const addTwo = (num1, num2) =>  num1 + num2
    
                        const addTwo = (num1, num2) => ( num1 + num2 )
                    

    Both will return same result.

  • If you use curly brackets {} in an arrow function, you must include the return keyword to return a value. Without it, the function won't return anything, which could lead to unexpected results in your code.


  • when you want to return an object literal directly, you need to use parentheses () around the object literal to distinguish it from a block of code.
  • 
                        const addTwo = (num1, num2) => ({username: "hitesh"})
    
                    

    Here, the parentheses () around { username: "hitesh" } are used to indicate that this is an object literal and not a block of code.