AmitDiwan

AmitDiwan

8,390 Articles Published

Articles by AmitDiwan

Page 506 of 839

How to convert a node list to an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 392 Views

Following is the code to convert a node list to an array in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       display: inline-block;       font-size: 20px;       font-weight: 500;       color: white;       background-color: blue;       padding:10px;       width: 100px;       height: 100px;    } Convert a node list to an array 1 2 3 4 CLICK HERE Click on the above button to change colors of all the above div    let sampEle = document.querySelectorAll('.sample');    let BtnEle = document.querySelector('.Btn');    BtnEle.addEventListener('click',()=>{       Array.from(sampEle).forEach(item=>{          item.style.backgroundColor = 'red';       })    }) OutputOn clicking the ‘CLICK HERE’ button −

Read More

Can we check if a property is in an object with JavaScript?

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 142 Views

Following is the code to check if a property is in an object 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;    } Checking a property in an object CLICK HERE Click on the above button to check if firstName property is present in obj object    let resEle = document.querySelector(".result");    let obj = { firstName: "Rohan", lastName: "Sharma", ...

Read More

Pseudo mandatory parameters in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 187 Views

Following is the code to implement pseudo mandatory parameters in functions 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;    } Optional function parameters CLICK HERE Click on the above button to call the add() and multiply() function    let resEle = document.querySelector(".result");    function add(a = 0, b = 0) {       return a + b;    }    function multiply(a, b) {       a = a || 0;       b = b || 0;       return a + b;    }    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML = "Sum of the numbers = " + add() + "";       resEle.innerHTML +=       "Multiplication of the numbers = " + multiply() + "";    }); OutputOn clicking the ‘CLICK HERE’ button −

Read More

Fat vs concise arrow functions in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 288 Views

The consice arrow is more stream lined form of fat arrow functions for one-line functions. If the function body only has one line of code, then there isn’t any need of curly braces {} for function body as conscie arrow functions have implicit return. Also, if there is only one parameter then that can be written without the parenthesis () but if there is no parameter then the parenthesis is necessary.SyntaxFat arrow function −let add = (a, b) =>{return a+b;}Consice arrow function:let add = (a, b)=>a+b;If one parameter only −let add = a=>a+22;Following is the code for fat vs concise ...

Read More

Fat arrow functions in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 362 Views

The fat arrow function syntax looks like the following −(param1, param2, ..)=>{ }It doesn’t have the function keyword and only have the => between the parameters and the function body.Following is the code implementing fat arrow functions 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;    } Fat arrow functions CLICK HERE Click on the above button to call the add() fat ...

Read More

Passing parameters to callback functions JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 522 Views

Following is the code for passing parameters to callback functions 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;    } Passing parameters to callback functions CLICK HERE Click on the above button to pass parameter to callback function add2 and call it    let resEle = document.querySelector(".result");    function add2(a) {       return a + 2;    }    function multiply9(fn, num) {       return fn(num) * 9;    }    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML = "The number returned = " + multiply9(add2, 8);    }); OutputOn clicking the ‘CLICK HERE’ button −

Read More

No. of ways to empty an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 239 Views

To empty an array in JavaScript, there are four ways −Setting to new Array − In this we set our array variable to a new empty array.Using length property − In this we set the length property of our array to 0.Using pop − In this we continuously pop the array elements till the length reaches to 0.Using splice − In this we put the start index as 0 and the no of elements to removed as array.length-1.Following is the code to display no. of ways to empty an array in JavaScript −Example Live Demo Document ...

Read More

How to implement asynchronous loop in JavaScript?

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 222 Views

Following is the code to implement asynchronous loop 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;    } Asynchronous loop in JavaScript CLICK HERE Click on the above button to start our asynchronous loop    let resEle = document.querySelector(".result");    async function waitTime(time) {       return new Promise((resolve) => {          setTimeout(resolve, time);       });    }    async function someFunction() {       for (let i = 0; i < 10; i++) {          await waitTime(2000);          resEle.innerHTML += "i = " + i + "";       }    }    document.querySelector(".Btn").addEventListener("click", () => {       someFunction();    }); OutputOn clicking the ‘CLICK HERE’ button the loop will print 1 to 10 −

Read More

JavaScript program to decrement a date by 1 day

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 295 Views

Following is the code to decrement a date by 1 day in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,.sample {       font-size: 20px;       font-weight: 500;       color: rebeccapurple;    } JavaScript program to decrement a date by 1 day Decrement Click on the above button to decrement the date by one day    let resEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let date = new Date();    sampleEle.innerHTML = date;    document.querySelector(".Btn").addEventListener("click", () => {       date.setDate(date.getDate() - 1);       resEle.innerHTML = "New Date = " + date;    }); OutputThe above code will produce the following output −On clicking the ‘Decrement’ button −

Read More

How to find keys of a hash in JavaScript?

AmitDiwan
AmitDiwan
Updated on 17-Jul-2020 459 Views

Following is the code for finding keys of a hash in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Find keys of a hash in JavaScript DISPLAY Click on the above button to display the hash keys    let resEle = document.querySelector(".result");    let obj = {       firstName: "Rohan",       lastName: "Sharma",       age: 22,    };    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML += "object keys are " + Object.keys(obj);    }); OutputThe above code will produce the following output −On clicking the ‘DISPLAY’ button −

Read More
Showing 5051–5060 of 8,390 articles
« Prev 1 504 505 506 507 508 839 Next »
Advertisements