Found 9150 Articles for Object Oriented Programming

Can we re-throw errors in JavaScript? Explain.

AmitDiwan
Updated on 17-Jul-2020 07:48:06

217 Views

Exception can be rethrown after they have been caught by using the throw after catching the exception.Following is the code to re-throw errors in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Re-throw errors in JavaScript CHECK Enter a number bigger than 40 to re throw error;    let BtnEle = document.querySelector(".Btn");    let resEle = ... Read More

How to check if a document is ready in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:45:07

720 Views

Following is the code to check if a document is ready in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: blueviolet;    } Check if a document is ready Check Click on the above button to see document state    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    resEle.innerHTML = document.readyState + "";    BtnEle.addEventListener("click", () => {       if (document.readyState === "complete") {          resEle.innerHTML = "The page has finished loading completely";       } else {          resEle.innerHTML = "The page is still loading";       }    }); OutputOn clicking the ‘Check’ button −

How to reduce arrays in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:40:50

146 Views

Following is the code to reduce arrays in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: blueviolet;    }    button {       padding: 8px;    } Reduce arrays javascript [1,3,5,6,9,22,15] Sum Click on the above button to sum the elements of the array    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let sampleEle = document.querySelector(".sample");    let arr = [1, 3, 5, 6, 9, 22, 15];    sampleEle.innerHTML = arr;    BtnEle.addEventListener("click", () => {       resEle.innerHTML =       "Sum = " +       arr.reduce((sum, prev) => {          return sum + prev;       });    }); OutputOn clicking the ‘Sum’ button −

The generator.throw() method in JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:42:47

176 Views

The generator.throw() method is used to pass an error to the yield. The generator resumes the execution after throw has been called by throwing an error and returning object with properties done and value.Following is the code for generator.throw() in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } generator.throw() method ... Read More

Creating ‘Copy to Clipboard’ feature on a web page with JavaScript

AmitDiwan
Updated on 17-Jul-2020 08:42:09

190 Views

Following is the code for creating ‘copy to clipboard’ feature on a web page with JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    input,    button {       padding: 8px;    } Creating Copy to Clipboard Copy Text Click on the above button to copy text from the textbox    let resEle = document.querySelector(".result");   ... Read More

Continuing a loop in functional programming JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:34:13

127 Views

Following is the code for continuing a loop in functional programming JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Continuing a loop in functional programming JavaScript Click here Click on the above button to iterate fruitArr and skip mango and peach or not    let resEle = document.querySelector(".result");    let fruitArr = ["apple", "mango", "peach", "papaya", "watermelon"];    document.querySelector(".Btn").addEventListener("click", () => {       fruitArr.some(function (fruit) {          if (fruit === "mango" || fruit === "peach") {             return false;          }          resEle.innerHTML += fruit + "";       });    }); OutputOn clicking the ‘Click here’ button −

Breaking a loop in functional programming JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:35:27

156 Views

Following is the code for breaking a loop in functional programming −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Breaking a loop in functional programming JavaScript Click here Click on the above button to iterate fruitArr array and exit if peach is found    let resEle = document.querySelector(".result");    let fruitArr = ["apple", "mango", "peach", "papaya", "watermelon"];    document.querySelector(".Btn").addEventListener("click", () => {       fruitArr.some(function (fruit) {          if (fruit === "peach") {             return true;          }          resEle.innerHTML += fruit + "";       });    }); OutputOn clicking the ‘Click here’ button −

Explain import “as” and Export “as” constructs in JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:32:33

206 Views

Import as allows us to import a named module under different name.Export as allows us to export a named module under different name.Following is the code for import as and export as construct in Javascript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Import as and Export as in JavaScript CLICK HERE Click on the above button to execute the imported ... Read More

How to get all unique values in a JavaScript array?

AmitDiwan
Updated on 17-Jul-2020 07:31:41

433 Views

Following is the code for getting all unique values in a JavaScript array −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } Get all unique values in a array Click here Click on the above button to remove the duplicate values from the above array    let resultEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let arr = [2, 3, 4, 2, 3, 4, "A", "A", "B", "B"];    sampleEle.innerHTML = "arr = " + arr;    document.querySelector(".Btn").addEventListener("click", () => {       let set1 = new Set(arr);       resultEle.innerHTML = "arr = " + [...set1] + "";    }); OutputOn clicking the ‘Click here’ button −

What are Partial functions in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:27:35

3K+ Views

Partial functions allow takes a function as an argument and along with it takes arguments of other types too. It then uses some of the arguments passed and returns a function that will take the remaining arguments. The function returned when invoked will call the parent function with the original and its own set of arguments.Following is the code for partial functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500; ... Read More

Advertisements