Access Array Returned by a Function in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:23:22

858 Views

Following is the code for accessing an array returned by a function in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Accessing an array returned by a function in JavaScript CLICK HERE Click on the above button to return an array from retTable() function and access it    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function retTable(num) {       let tempNum = [];       for (i = 1; i {       let tableArr = retTable(5);       resEle.innerHTML = "tableArr = " + tableArr;    }); OutputOn clicking the ‘CLICK HERE’ button −

Strict Equality vs Loose Equality in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:21:21

1K+ Views

The loose equality operator ‘==’ allows us to compare two or more operands by converting their value to a common type first and then checking for the equality between them.strict equality operator ‘===’ allows us to compare two or more operands by checking the equality between the values as well as their types . It returns true only if the values and the type both match with the other operand.Following is the code for loose equality vs strict equality in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, ... Read More

Loose Equality in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:18:34

289 Views

The loose equality operator ‘==’ allows us to compare two or more operands by converting their value to a common type first and then checking for the equality between them.Following is the code to implement loose equality in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Loose equality in JavaScript. CLICK HERE Click on the above button see some ... Read More

Renaming Imports and Exports in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:14:23

237 Views

Following is the code for renaming imports and exports in JavaScript −Note − You need to run a localhost server to run this example.Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Renaming imports and exports in JavaScript CLICK HERE Click on the above button to execute the imported function script.jsimport {test, tellTime as showTime} from "./sample.js"; let resultEle = ... Read More

Loading JavaScript Modules Dynamically

AmitDiwan
Updated on 20-Jul-2020 08:12:34

235 Views

Following is the code for loading JavaScript modules dynamically −Note − You need to run a localhost server to run this example.Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;    } Loading JavaScript modules dynamically IMPORT Click on the above button to import module script.jsimport test from './sample.js'; document.querySelector('.Btn').addEventListener('click',()=>{    test(); })sample.jslet resultEle = document.querySelector(".result"); export default function testImport(){    resultEle.innerHTML = 'Module testImport has been imported'; }OutputOn clicking the ‘IMPORT’ button −

Keyed Collections in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:10:32

699 Views

Keyed collections are data collections that are ordered by key not index. They are associative in nature. Map and set objects are keyed collections and are iterable in the order of insertion.Following is the code for keyed collections in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Keyed collections in JavaScript CLICK HERE Click on the above button to ... Read More

Indexed Collections in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:08:10

4K+ Views

Arrays are the indexed collection in JavaScript as they allow us to access an element using its index.Following is the code to implement indexed collections in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Indexed collections in JavaScript CLICK HERE Click on the above button to create and display the array    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    BtnEle.addEventListener("click", () => {       let arr = [1, 2, 3, 4, 5];       arr.forEach((item, index) => {          resEle.innerHTML += `arr[${index}] = ${item} `;       });    }); OutputOn clicking the ‘CLICK HERE’ button −

Conditional Statements in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:06:19

2K+ Views

There are three types of conditional statements in JavaScript −If statement − The if statement is used to execute code inside the if block only if the specific condition is met.If else statement − The If….Else statement is used to check only two conditions and execute different codes for each of them.If else if else statement − The if…else if…else statement is used for checking more than two conditions.Following is the code to implement conditional statements in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;   ... Read More

Modifying Prototypes in JavaScript

AmitDiwan
Updated on 20-Jul-2020 08:04:03

207 Views

Following is the code for modifying prototypes in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Modifying prototypes in JavaScript CLICK HERE Click on the above button to see the original and modified prototype function    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function Person(name, age, occupation) {       this.name = name;     ... Read More

Set JavaScript Object Values Dynamically

AmitDiwan
Updated on 20-Jul-2020 08:01:20

794 Views

Following is the code for setting JavaScript object values dynamically −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Set Javascript object values dynamically CLICK HERE Click on the above button to set student object values dynamically    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let student = {       name: "Rohan",       age: 22,       displayInfo() {          return "Name = " + this.name + " : Age = " + this.age + "";       },    };    BtnEle.addEventListener("click", () => {       resEle.innerHTML = student.displayInfo();       resEle.innerHTML += "After changing properties ";       student.name = "Shawn";       student.age = 19;       resEle.innerHTML += student.displayInfo();    }); OutputOn clicking the ‘CLICK HERE’ button −

Advertisements