Found 10483 Articles for Web Development

Adding methods to Javascript Prototypes

AmitDiwan
Updated on 18-Jul-2020 07:54:11

107 Views

Following is the code for adding methods to JavaScript prototypes −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Adding methods to Javascript Prototypes Click Here Click on the above button to call the methods of person1 and person2 object    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function personConstructor(fName, lName, birthYear) {       this.fName ... Read More

Function prototypes in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:52:35

244 Views

Functions created in JavaScript always have the prototype property added by the JavaScript engine to them. The prototype property is an object which contains the constructor property by default. The function protoype can be accessed by −functionName.prototypeWhen objects are creating using the function constructor, this prototype property can be used to share methods or properties between the objects created by that function constructor.Following is the code for function prototypes in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       ... Read More

Anonymous Wrapper Functions in JavaScript

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

811 Views

Anonymous functions are used to wrap code snippets, JavaScript libraries, functions etc to control their visibility and the namespace so that there shouldn’t be any conflict with other libraries code. IIFE (Immediately Invoked Function Expressions) are used for this purpose.Following is the code to implement Anonymous Wrapper 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;    } Anonymous wrapper functions in JavaScript 0    let resEle = document.querySelector(".result");    (function () {       resEle.innerHTML =       "This piece of code is automatically executed as it is inside an IIFE";    })(); Output

Sum of nested object values in Array using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:53:21

993 Views

Following is the code to sum nested object values in array using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Sum of nested object values in Array CLICK HERE Click on the above button to sum the nested object values of json array    let json = {       storeData: [       { ... Read More

Implement Private properties using closures in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:48:37

145 Views

Following is the code to implement private properties using closures 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;    } Private properties, using closures 0 CLICK HERE Click on the above button to increment the above counter using closures    let BtnEle = document.querySelector(".Btn");    let resEle = document.querySelector(".result");    function test() {       let a = 0;       return function incrementA() {          a++;          return a;       };    }    let storeVal = test();    BtnEle.addEventListener("click", (event) => {       resEle.innerHTML = storeVal();    }); OutputOn clicking the ‘CLICK HERE’ button the counter will increase on each click −

Using ‘{ }' in JavaScript imports?

AmitDiwan
Updated on 18-Jul-2020 07:47:41

148 Views

Following is the code using {} in javaScript imports −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Using '{ }' in javascript imports CLICK HERE Click on the above button to execute the imported function script.jsimport {test, tellTime as showTime} from "./sample.js"; let resultEle = document.querySelector('.result'); document.querySelector('.Btn').addEventListener('click', ()=>{    resultEle.innerHTML+=test();    resultEle.innerHTML+=showTime(); })sample.jsfunction testImport() {    return "Module ... Read More

When you should not use JavaScript Arrow Functions?

AmitDiwan
Updated on 18-Jul-2020 07:44:25

182 Views

The arrow functions should not be used as an object method because an arrow function does not have its own this. It takes this value of the enclosing lexical scope which is the window object instead of the object itself. This can cause problems as we would now be setting and accessing the window object properties instead of the intended object.Following is the code showing when should you not use JavaScript arrow functions −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {   ... Read More

Formatted Strings Using Template Strings in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:41:47

284 Views

Following is the code for formatted strings using template strings 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: blueviolet;    }    .sample {       color: red;    } Formatted Strings Using Template Strings JavaScript `The person name is ${personObj.name}. His age and rollno are ${personObj.age} and ${personObj.rollno} respectively` CLICK HERE Click on the above ... Read More

How to import an Object with Sub Objects and Arrays in JavaScript?

AmitDiwan
Updated on 18-Jul-2020 07:43:56

593 Views

Following is the code for importing an object with Sub objects and arrays in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result{       font-size: 18px;       font-weight: 500;    } Importing an Object with Sub Objects and Arrays let sampleEle = document.querySelector(".sample"); let obj = JSON.parse(sampleEle.innerHTML); script.jsimport obj from "./sample.js"; let resultEle = document.querySelector(".result"); for (let i in obj) {    resultEle.innerHTML += "Property = " + i + " : Value = " + obj[i] + ""; }sample.jsexport default{    firstName: "Rohan",    lastName: "Sharma",    school: {       name: "St Marks",       address: "USA",    },    sports: ["cricket", "football"], };OutputThe above code will produce the following output −

Spread operator for arrays in JavaScript

Disha Verma
Updated on 17-Mar-2025 12:34:29

308 Views

The spread (…) operator of JavaScript is used to expand an array or any other iterable into individual elements. It was introduced in ECMAScript 2015 (ES6). The spread operator is a powerful feature of JavaScript that allows one to perform operations for complex and lengthy code in a simple single line of code. The spread operator is denoted by the (...) (triple dot) symbol. You can simply create a copy of any existing array with the spread operator. The basic syntax of the spread operator is mentioned below - const newArray = [...existingArray]; Usage on Arrays The spread operator has ... Read More

Advertisements