Category: JavaScript Important Keywords

https://zain.sweetdishy.com/wp-content/uploads/2026/01/js-2.png

  • JavaScript – var Keyword

    The var keyword in JavaScript is used to declare variables. Before ES6, there was only var keyword to declare a variable. In ES6, let and const keywords were introduced as a better way to declare variables. It is advised to use let instead of var to declare a variable. If you are targeting older version of browser, you can use var.

    In JavaScript, variables are the data storage container. You can use the variables to store any type of data. For example, string, number, boolean, object, etc.

    A variable declared with var keyword has function scope. Whereas a variable declared with let has block scope.

    Syntax

    Follow the syntax below to define the variable using the ‘var’ keyword.

    var identifier;var identifier = value;

    Here, the identifier can be a valid variable name. In the JavaScript – Variables chapter, we have already discussed the rules to define valid identifiers.

    You may assign or not value to the variable while declaring it using the JavaScript ‘var’ keyword.

    Example

    In the below code, we have defined the 3 variables using the ‘var’ keyword. In the num1 variable, we have stored the numeric value. In the str1 variable, we have stored the string value; in the bool variable, we have stored the boolean value.

    <html><body><div id ="output1"></div><div id ="output2"></div><div id ="output3"></div><script>var num1 =30;var str1 ="Hello World!";var bool =true;
          document.getElementById('output1').innerHTML = num1;
          document.getElementById('output2').innerHTML = str1;
          document.getElementById('output3').innerHTML = bool;</script></body></html>

    Output

    30
    Hello World!
    true
    

    JavaScript Variable Scopes

    The scope of the variable defined using the JavaScript ‘var’ keyword has a function scope or global scope.

    JavaScript Function Scope

    When you define the variable inside the function using the var keyword, it can be accessible inside the function anywhere. Even if you define the variable using the var keyword inside the particular block inside the function, it can be accessible anywhere.

    Let’s understand the function scope with the example below.

    Example

    In the below code, we have defined the variable ‘x’ inside the function. We have also defined the variable ‘y’ inside the block, which is inside the function.

    After that, we access the variables x and y inside the function.

    <html><body><div id ="demo1"></div><div id ="demo2"></div><script>functiondemo(){var x =10;{var y =20;// x and y both can be accessible here}
             document.getElementById('demo1').innerHTML ="X == "+ x;
             document.getElementById('demo2').innerHTML ="Y == "+ y;}demo();</script></body></html>

    Output

    X == 10
    Y == 20
    

    If you define the variable using the let keyword inside the block, which is inside the function, and try to access it outside the block, it will throw an error.

    JavaScript Global Scope

    It becomes the global variable if you define the variable using the ‘var’ keyword outside the function or a particular block.

    After that, you can access the variable anywhere inside the code using the window object.

    Example

    We have defined the ‘num1’ global variable in the code below.

    After that, we access the ‘num1’ variable inside the function using and without using the window object.

    <html><body><div id ="output"></div><script>const output = document.getElementById('output');var num1 =10;functionsum(num2){
             output.innerHTML +="num1 + num2 = "+(num1 + num2)+"<br>";
             output.innerHTML +="window.num1 + num2 = "+(window.num1 + num2);}sum(20);</script></body></html>

    Output

    num1 + num2 = 30
    window.num1 + num2 = 30
    

    JavaScript Variable Hoisting

    The variable defined using the ‘var’ keyword is hoisted at the top of its scope. It means JavaScript adds the declaration of the variable at the top of its scope.

    So you can access the variable before the declaration of the variable.

    Example

    In the below code, we have initialized the variable ‘a’ and printed its value before its declaration.

    The code runs without error, as the variable defined using the var keyword is hoisted at the top of its scope.

    <html><body><div id ="output">Value of the variable a is:</div><script>functioncheckHoisting(){
             a =98;
             document.getElementById('output').innerHTML += a;var a;}// You can't access the variable a here.checkHoisting();</script></body></html>

    Output

    Value of the variable a is: 98
    

    The above JavaScript code is similar to the below code.

    var a;
    a =98;
    document.getElementById('output').innerHTML += a;

    Redeclaration of variable defined using the ‘var’ keyword

    You can redeclare the variables using the ‘var’ keyword, and the code will run without any error.

    If you have initialized the last duplicate variable with the latest value, it will contain that value. But if you don’t initialize the last duplicate variable, the variable will contain its previous value without losing it.

    Example

    In the code below, we have defined the variable ‘a’ two times and initialized it with different values. You can observe in the output that variable ‘a’ contains the last value.

    We have defined the variable ‘a’ a third time and haven’t initialized it. So, it contains the value of the 2nd ‘a’ variable.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');var a =10;var a =20;
          output.innerHTML +="The value of the a is: "+ a +"<br>";var a;
          output.innerHTML +="The value of the a is: "+ a;</script></body></html>

    Output

    The value of the a is: 20
    The value of the a is: 20
    

    If you define the duplicate variables in the different scopes, it can have value according to where you access the variable.

    Example

    In the code below, we have defined the ‘num’ variable outside and inside the function. When you access it inside or outside the function, it prints the different values.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');var num =10;functionprintNum(){var num =20;
             output.innerHTML +="Num inside the function is: "+ num +"<br>";}printNum();
          output.innerHTML +="Num outside the function is: "+ num +"<br>";</script></body></html>

    Output

    Num inside the function is: 20
    Num outside the function is: 10
    

    Declaring Many Variables in a Single Statement

    If you want to declare multiple variables using the JavaScript ‘var’ keyword, you can declare all variables in a single statement. It makes code readable.

    Example

    In the below code, we have declared the variables ‘a’ and ‘b’ in the single statement and initialized them with different values after declaring.

    <html><body><div id ="output1"></div><div id ="output2"></div><script>var a, b;
          a =70;
          b =80;
          document.getElementById('output1').innerHTML ="The value of a is: "+ a;
          document.getElementById('output2').innerHTML ="The value of b is: "+ b;</script></body></html>

    Output

    The value of a is: 70
    The value of b is: 80
    

    However, you can also assign the values to the variables while declaring the multiple variables in a single statement.

    Using the var keyword with loops

    When you use the JavaScript ‘var’ keyword to define the iterator variable of for loop, you can also access it outside the for loop.

    Example

    In the below code, we have defined the variable ‘p’ inside the for loop. We access the variable p inside and outside the loop.

    <html><body><div id ="output"></div><script>const output = document.getElementById('output');for(var p =0; p <5; p++){
             output.innerHTML +="The value of p is: "+ p +"<br>";}
          output.innerHTML +="The value of p outside the loop is: "+ p;</script></body></html>

    Output

    The value of p is: 0
    The value of p is: 1
    The value of p is: 2
    The value of p is: 3
    The value of p is: 4
    The value of p outside the loop is: 5
    

    Declaration with Destructuring

    In JavaScript, you can declare the variables using the ‘var’ keyword while destructuring the array or objects.

    Example

    In the below code, the ‘arr’ array contains 3 variables.

    After that, we define variables using the ‘var’ keyword and destructure the array.

    <html><body><div id ="output"></div><script>const output = document.getElementById('output');var arr =[4,5,6];var[a, b, c]= arr;
          output.innerHTML ="a = "+ a +", b = "+ b +", c = "+ c;</script></body></html>

    Output

    a = 4, b = 5, c = 6
    

    You can also use the ‘var’ keyword to define the variables and store the objects or function expressions.

  • JavaScript – new Keyword

    The new keyword in JavaScript is used to create an instance of the object that has constructor function. Using new keyword, we can create the instances of a user-defined as well as built-in object types. We can create instances of the classes, prototype, or constructor functions.

    When a JavaScript function is called with new keyword, the function is used as constructor. The new keyword will do the following:

    • Creates a blank/ empty JavaScript object.
    • Sets its prototype for inheritance.
    • Binds this variable internally with newly created object.
    • Executes the constructor function using new crated object whenever this is used.
    • Returns newly created object, unless the contractor function returns a non-null object reference.

    The new keyword is also used to create an instance of the built-in JavaScript objects like String, Boolean, Number, etc.

    Syntax

    The syntax to use the new keyword in JavaScript is as follows

    newConstructor(arguments);

    Parameters

    • Constructor − It is the name of the constructor function or class name.
    • arguments −It can be multiple arguments to initialize the object properties with them.

    Return Value

    • It returns the newly created object if the constructor function returns nothing or a primitive value.
    • It returns the value that is returned by constructor function if constructor returns a non-primitive value.

    Using ‘new’ keyword with Function Constructor

    To use a function as a constructor, we should call the function with new keyword placing it before the function name.

    We can define multiple objects using function constructor. The function constructor works as the object’s prototype.

    Follow the syntax below to use the ‘new’ keyword and constructor function to define the object.

    newFuncName(arguments);

    Example

    We have defined the Watch() constructor function in the code below.

    The Watch() constructor function initializes the brand, price, and type properties.

    After that, we have created two new instances of the Watch() function and printed them in the output.

    <html><body><div id ="output"></div><script>functionWatch(brand, price, type){this.brand = brand;this.price = price;this.type = type;}const titan =newWatch('titen',4000,'analog');const sonata =newWatch('sonata',3000,'digital');
       document.getElementById('output').innerHTML +="The titan object is: "+JSON.stringify(titan)+"<br>"+"The sonata object is: "+JSON.stringify(sonata);</script></body></html>

    Output

    The titan object is: {"brand":"titen","price":4000,"type":"analog"}
    The sonata object is: {"brand":"sonata","price":3000,"type":"digital"}
    

    Example

    In the below code, we have defined the Laptop() constructor function, initializing various properties related to the laptop. Also, we have defined the getLaptop() function, which prints the laptop details.

    After that, we created the two instances of the Laptop() object and used the getLaptop() method with both. In this way, you can share single methods with different objects.

    <html><body><div id ="output"></div><script>const output = document.getElementById('output');functionLaptop(brand, model, processor){this.brand = brand;this.model = model;this.processor = processor;this.getLaptop=function(){
    	     output.innerHTML +=this.brand +", "+this.model +", "+this.processor +"<br>";}}constHP=newLaptop('HP',"VIKING","i7");const Dell =newLaptop('Dell',"Inspiron","i5");HP.getLaptop();
       Dell.getLaptop();</script></body></html>

    Output

    HP, VIKING, i7
    Dell, Inspiron, i5
    

    Using ‘new’ keyword with Class

    You can also use the new keyword to define the instance of a class. The class also provides the blueprint for the object.

    Before ES6, the constructor function was used to define the template for the object. After ES6, classes are used to define the template for the object.

    Example

    In the below example, we have defined the ‘WindowClass class. In the ‘WindowClass’ we have added the constructor to initialize the properties. We have also added the getDetails() method in the class.

    After that, we used the ‘new’ keyword followed by the class name to define an object of the WindowClass.

    Next, we invoke the getDetails() method on the instance of the class.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo')classWindowClass{constructor(color, size, type){this.color = color;this.size = size;this.type = type;}getDetails=function(){
                output.innerHTML ="Color: "+this.color +"<br>"+"Size: "+this.size +"<br>"+"Type: "+this.type;}}// Creating the instance of WindowClass classconst window1 =newWindowClass('blue','small','wooden');// Calling function object
          window1.getDetails();</script></body></html>

    Output

    Color: blue
    Size: small
    Type: wooden
    

    Using ‘new’ keyword with built-in object

    You can also use the ‘new’ keyword to define the instance of the built-in object having constructor functions.

    Follow the syntax below to create an instance of the built-in object Number.

    const num =newNumber(10);

    In the above syntax, we have passed the numeric value as an argument of the Number() constructor.

    Example

    In the code below, we have used the Number() and String() constructors with a new keyword to define the numeric and string objects.

    After that, we used the ‘typeof’ operator to check the type of num and str variables. In the output, you can see that the num and str variable type is an object.

    <html><body><div id ="output"></div><script>const num =newNumber(10);const str =newString("Hello");
          document.getElementById("output").innerHTML ="num = "+ num +", typeof num "+typeof num +"<br>"+"str = "+ str +", typeof str "+typeof str;</script></body></html>

    Output

    num = 10, typeof num object
    str = Hello, typeof str object
  • JavaScript – void Keyword

    The void keyword in JavaScript is used as an operator that evaluates a given expression and returns undefined. The void is an important keyword in JavaScript. The meaning of the void is null or empty.

    The void keyword can be used as a unary operator that appears before its single operand, which may be of any type. This operator specifies an expression to be evaluated without returning a value or returning the undefined.

    Syntax

    The syntax to use the void keyword in JavaScript −

    void operand;

    In the above syntax, the operand can be any expression.

    Return Value

    It returns the undefined.

    Example

    In the below code, we used the 10 with the ‘void’ keyword. In the output, you can observe that it returns undefined.

    <html><body><div id ="output">The value of the ans variable is:</div><script>let ans =void10;
          document.getElementById("output").innerHTML += ans;</script></body></html>

    Output

    The value of the ans variable is: undefined
    

    Importance of Precedence of void Keyword

    Generally, the JavaScript ‘void’ keyword is used to return the primitive undefined value, but if you don’t take precedence in the consideration, it can return a different value.

    Let’s understand it via the example below.

    Example

    In the below code, the void operator takes precedence over the strict equality operator in the first expression. So, it first evaluates ‘void 10’ to undefined and compares it with 20. So, it returns false.

    The second expression evaluates ’10 === 20′ first to false and evaluates ‘void false’ to undefined.

    <html><body><div id ="output1"></div><div id ="output2"></div><script>let res1 =void10===20;let res2 =void(10===20);
          document.getElementById("output1").innerHTML += res1;
          document.getElementById("output2").innerHTML += res2;</script></body></html>

    Output

    false
    undefined
    

    What is javascript:void(0)?

    Let’s break down the javascript:void(0) into two parts and understand both separately.

    javascript:

    You can use the ‘javascript:’ to create a pseudo URL. Using ‘javascript:’ you can create the interactive URL.

    You need to write the expression after ‘javascript:’, and when users click the anchor text, it executes the JavaScript code.

    Let’s understand it via the example below.

    Example

    In the below code, we have created the link text and used the JavaScript URL as a href. When you click the anchor text, it writes it in the HTML document.

    <html><body><a href ="javascript:document.write('Anchor text is clicked!')"> Click me!</a></body></html>

    In this way, you can use the ‘javascript:uri’ to create the pseudo URL.

    void(0)

    The void(0) evaluates the JavaScript expression, but it returns undefined. So, when you want to execute the expression without performing any action, you can use the void(0).

    javascript: void(0)

    When you don’t want to navigate users to another web page when they click on the link, you can use the ‘javascript:void(0)’ as a href value of the anchor tag.

    Let’s understand it via the example below.

    Example

    It won’t do anything Whenever you click the anchor text in the code below.

    <html><body><a href ="javascript:void(0)"> Click me!</a></body></html>

    You can also use the ‘javascript:void(0)’ when you want to update the web page using the URI but don’t want to navigate to another web page.

    Example

    In the below code, when you click the anchor text, it will change the background color of the body, rather than navigating to another web page.

    <html><body><a href ="javascript:void(0);" 
       	  onclick ="document.body.style.backgroundColor = 'blue'"> 
       	  Change Background Color 
       </a></body></html>

    The void Keyword with Functions

    When you use the void keyword with JavaScript functions, it returns undefined. After that, if you try to execute the function, it will throw an error, as the ‘void’ operator considers the function as its operand and evaluates it as undefined.

    Example

    In the below code, we have defined the test() function and used the ‘void’ keyword with it.

    Also, used the trycatch block to catch the error.

    In the try block, we execute the function, and in the output, you can observe that control goes into the catch block.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');try{voidfunctiontest(){
             output.innerHTML +="The test function is executed!";}test();}catch(error){
          output.innerHTML +="The test function is undefined!";}</script></body></html>

    Output

    The test function is undefined!
    

    The void Keyword with Immediately Invoked Function

    When you use the ‘void’ keyword with the JavaScript immediately invoked function, it invokes the function expression first and evaluates it as undefined.

    Example

    In the below code, we have defined the immediately invoked function with the void keyword. You can observe that it invokes the function first and returns undefined.

    <html><body><div id ="demo"></div><script>const output = document.getElementById("demo");voidfunction(){
             output.innerHTML ="Unknown function is executed!";}();</script></body></html>

    Output

    Unknown function is executed!
    

    Also, you can use the ‘void’ keyword with the arrow function to return undefined from the arrow function. The JavaScript:void() URI can also be used to prevent the web page reloading by default, like the preventDefault() method.

  • JavaScript – this Keyword

    What is ‘this’ keyword?

    In JavaScript, the ‘this‘ keyword contains the reference to the object. It represents the context of the function or current code. It is used to access the properties and methods of the current object.

    When this keyword is used inside a function, the this will refer to the object that the function is called with.

    In JavaScript, functions are also objects. So, you can use the ‘this’ keyword with functions also.

    Which object does the ‘this’ refer to?

    The object referred by the ‘this‘ keyword depends on how you have used the ‘this’ keyword.

    For example,

    • The ‘this’ keyword refers to the window object in the global scope.
    • When you use the ‘this’ keyword inside the function, it also represents the ‘window’ object.
    • The ‘this’ keyword refers to an undefined in the strict mode in the function.
    • The ‘this’ keyword refers to the object when you use it in the object method.
    • In the event handler, the ‘this‘ keyword refers to the element on which the event is executed.
    • The ‘this’ keyword in methods like call(), apply(), and bind() can refer to different objects.

    Syntax

    Follow the syntax below to use the ‘this’ keyword in JavaScript &minus

    this.property
    ORthis.method();

    You can access the properties and execute the object’s methods using the ‘this’ keyword.

    JavaScript ‘this’ in the Global Scope

    When you use the ‘this’ keyword in the global scope, it represents the global (window) object. You can access the global variables using the ‘this’ keyword in the global scope.

    Example

    In the below code, we have defined the ‘num’ variable and printNum() function in the global scope. After that, we used the ‘this’ keyword to access the global variable and functions.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');var num =10;functionprintNum(){
             output.innerHTML +="Inside the function: "+ num +"<br>";}this.printNum();
          output.innerHTML +="Outside the function: "+this.num +"<br>";</script></body></html>

    Output

    Inside the function: 10
    Outside the function: 10
    

    JavaScript ‘this’ in a Function

    When you use the ‘this’ keyword in the function, it represents the global scope or ‘window’ object.

    Example

    In the below code, we use the ‘this’ keyword inside the function. You can observe that we access the global variables using the ‘this’ keyword inside the function.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');var message ="Hello World!";functionprintMessage(){var message ="Hi! How are you?";
             output.innerHTML ="The messsage is: "+this.message;}printMessage();</script></body></html>

    Output

    The messsage is: Hello World!
    

    ‘this’ in a Function in Strict Mode

    When you use the ‘this’ keyword inside the function in the strict mode, it doesn’t refer to any object. The value of the ‘this’ keyword becomes undefined.

    Example

    In the below code, we use the ‘this’ keyword inside the function in the strict mode. It prints undefined.

    <html><body><div id ="demo"></div><script>let output = document.getElementById('demo');var message ="Hello World!";functiontest(){"use strict";
             output.innerHTML ="The value of this in the strict mode is: "+this;}test();</script></body></html>

    Output

    The value of this in the strict mode is: undefined
    

    ‘this’ in a Constructor Function

    When you use the function as a constructor function to create an object, the ‘this’ keyword refers to the object.

    Example

    We have defined the Animal() constructor function in the code below. We used the ‘this’ keyword inside the constructor function to initialize the properties of the object.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');functionAnimal(){this.name ='Lion';this.age =3;this.color ='Brown';}const newAnimal =newAnimal();
          output.innerHTML ="Animal Name: "+ newAnimal.name +"<br>";
          output.innerHTML +="Animal Age: "+ newAnimal.age +"<br>";
          output.innerHTML +="Animal Color: "+ newAnimal.color;</script></body></html>

    Output

    Animal Name: Lion
    Animal Age: 3
    Animal Color: Brown
    

    ‘this’ in an Arrow Function

    When you use the ‘this’ keyword in the arrow function, it refers to the scope of its parent object.

    For example, when you define the arrow function inside the object method and use the ‘this’ keyword inside that, it presents the object. The ‘this’ keyword refers to the global object if you define the arrow function inside another function.

    Example

    In the below code, we have defined the arrow function inside the getDetails() method of the object. When we print the value of the ‘this’ keyword, it prints the object.

    <html><body><div id ="output1">Value of'this' inside the getDetails() method:</div><div id ="output2">Value of'this' inside the getInnerDetails() method:</div><script>const wall ={
    		 size:"10",
    		 color:"blue",getDetails(){
    		    document.getElementById('output1').innerHTML +=JSON.stringify(this);constgetInnerDetails=()=>{
    			   document.getElementById('output2').innerHTML +=JSON.stringify(this);}getInnerDetails();}}
          wall.getDetails();</script></body></html>

    Output

    Value of 'this' inside the getDetails() method: {"size":"10","color":"blue"}
    Value of 'this' inside the getInnerDetails() method: {"size":"10","color":"blue"}
    

    ‘this’ in the Object Method

    When you use the ‘this’ keyword inside the object method, it represents the object itself.

    Example

    In the below code, we have defined the ‘fruit; object. The object contains the printFruitDetails() method, and in the method, we used the ‘this’ keyword to access the properties of the object.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');const fruit ={
             name:"Apple",
             color:"red",printFruitDetails(){
                output.innerHTML +="Furit Name = "+this.name +"<br>";
                output.innerHTML +="Fruit Color = "+this.color;}}
          fruit.printFruitDetails();</script></body></html>

    Output

    Furit Name = Apple
    Fruit Color = red
    

    ‘this’ in a Child Function of an Object Method

    When you define the function inside the object method and use the ‘this’ keyword inside the function, it represents the global object rather than an object.

    Example:

    In the below code, we have defined the person object. The person object contains the printDetails() method. In the printDetails() method, we have defined the printThis() function.

    In the printThis() function, we print the value of the ‘this’ keyword, and it prints the global object.

    <html><body><div id ="output">Inside the printThis()function, Value of'this'=</div><script>const person ={
          name:"Salman",
          isBusinessman:false,printDetails(){functionprintThis(){
                document.getElementById('output').innerHTML +=this;}printThis();}}
       person.printDetails();</script></body></html>

    Output

    Inside the printThis() function, Value of 'this' = [object Window]
    

    JavaScript ‘this’ in Event Handlers

    Using the ‘this’ keyword with event handlers refers to the HTML element on which the event is executed.

    Example

    In the below code, we have added the onClick event handler into the <div> element. When users click the div element, we hide the div element using the ‘display’ property.

    <html><head><style>
          div {
            height:200px;
            width:700px;
            background-color: red;}</style></head><body><p>Click the DIV below to remove it.</p><div onclick ="this.style.display = 'none'"></div></body></html>

    Explicit Function Binding in JavaScript

    In JavaScript, call(), apply(), or bind() methods are used for the explicit binding.

    The explicit binding allows you to borrow the method of the particular object. Using these methods, you can explicitly define the context of the ‘this’ keyword.

    Let’s understand explicit binding using the examples below.

    Example: Using the call() method

    In the below code, the lion object contains the color and age property. It also contains the printDetails() method and prints the details using the ‘this’ keyword.

    The tiger object contains only color and age properties. We used the call() method to call the printDetails() method of the lion object with the context of the tiger object. So, the method prints the details of the tiger in the output.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');const lion ={
        color:"Yellow",
        age:10,printDetails(){
          output.innerHTML +=`<p>Color: ${this.color}</p>`;
          output.innerHTML +=`<p>Age: ${this.age}</p>`;}}const tiger ={
        color:"Orange",
        age:15,}
      lion.printDetails.call(tiger);</script></body></html>

    Output

    Color: Orange
    
    Age: 15
    

    Example: Using the bind() method

    The below code also contains the lion and tiger objects. After that, we used the bind() method to bind the printDetails() method of the lion object into the tiger object.

    After that, we use the tigerDetails() method to print the details of the tiger object.

    <html><body><div id ="demo"></div><script>const output = document.getElementById('demo');const lion ={
        color:"Yellow",
        age:10,printDetails(){
          output.innerHTML +=`<p>Color: ${this.color}</p>`;
          output.innerHTML +=`<p>Age: ${this.age}</p>`;}}const tiger ={
        color:"Orange",
        age:15,}const tigerDetails = lion.printDetails.bind(tiger);tigerDetails();</script></body></html>

    Output

    Color: Orange
    
    Age: 15
    

    JavaScript ‘this’ Precedence

    You should use the below precedence order to determine the context of the ‘this’ keyword.

    • 1. bind() method
    • 2. call and apply() methods
    • 3. Object method
    • 4. Global scope