Rounding and Truncating Numbers in JavaScript

AmitDiwan
Updated on 18-Jul-2020 08:06:32

4K+ Views

There are two functions in JavaScript for rounding and truncating numbers: Math.round() and Math.trunc() respectively −Math.round() − = It rounds the decimal number to the nearest integer value.Math.trunc() − = It simply removes the fractional part of the decimal number and converts it to a whole number.Following is the code for rounding and truncating numbers 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; ... Read More

Parse Array of Objects Inside Another Object Using Maps or foreach

AmitDiwan
Updated on 18-Jul-2020 08:05:09

140 Views

Following is the code to parse array of objects inside another object using maps or forEach −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Parse array of objects inside object CLICK HERE Click on the above button to parse the json object    let json = {       storeData: [          {       ... Read More

Does JavaScript Support Block Scope

AmitDiwan
Updated on 18-Jul-2020 08:03:27

227 Views

JavaScript supports block scope only for variables that were declared using let or const keyword. Variables declared using var support function scope but not block scope.Following is the code for displaying block scope 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;    } Block scope JavaScript Click Here Click on the above button to create variables with var and let ... Read More

Parsing Array of Objects Inside an Object using Maps or ForEach in JavaScript

AmitDiwan
Updated on 18-Jul-2020 08:01:40

770 Views

Following is the code to parse array of objects inside an object using maps or forEach 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;    } Parse array of objects inside object CLICK HERE Click on the above button to parse the json object    let json = {       storeData: [          {   ... Read More

The New Operator in JavaScript

AmitDiwan
Updated on 18-Jul-2020 08:01:26

407 Views

The new operator is used for creating a user-defined object type instance of one of the builtin object types that has a constructor function.Following is the code for new operator 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;    } New Operator in JavaScript Click Here Click on the above button to create the object person1 from Person constructor ... Read More

Referring JavaScript Function from Within Itself

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

141 Views

Following is the code to refer JavaScript function from within itself −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Referring JavaScript function from within itself Click Here Click on the above button to call the printHello() function    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function printHello(num) {       for (let i = 0; i < num; i++) {          resEle.innerHTML += "Hello World ";          printHello(--num);       }    }    BtnEle.addEventListener("click", () => {       printHello(3);    }); OutputOn clicking the ‘Click Here’ button −

Bind Object's Method to Click Handler in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:58:14

242 Views

Following is the code for binding an object’s method to a click handler 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;    } Binding an object's method to a click handler in JavaScript Click Here Click on the above button to know how many times you have clicked the button    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let testObj = {       a: 0,       timesClicked() {          this.a += 1;          resEle.innerHTML = "Times clicked = " + this.a;       },    };    BtnEle.addEventListener("click", () => {       testObj.timesClicked();    }); OutputOn clicking the ‘Click Here’ button −

Share a Method Between JavaScript Objects in an Array

AmitDiwan
Updated on 18-Jul-2020 07:58:02

140 Views

Following is the code to share a method between objects in an array −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Share a method between objects in an array CLICK HERE Click on the above button to call welcome() method of all person objects    let resEle = document.querySelector(".result");    function Person(firstName, lastName) {       this.firstName = firstName; ... Read More

Modify Built-in Object Prototypes in JavaScript

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

167 Views

Following is the code for modifying built-in object 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;    } Modify built-in object prototypes Click Here Click on the above button to call the alert method    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    window.alert = function (msg) {       resEle.innerHTML = "Custom Alert function has been triggered";       resEle.innerHTML += "Alert Message = " + msg;    };    BtnEle.addEventListener("click", () => {       alert("Hello World");    }); OutputOn clicking the ‘Click Here’ button −

De-structuring an Object Without One Key

AmitDiwan
Updated on 18-Jul-2020 07:55:36

155 Views

Following is the code to de-structure an object without one key −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;    } De structuring an object without one key. {"firstName":"Rohit", "lastName":"Sharma", "age":22} CLICK HERE Click on the above button to destructure the above object    let resEle = document.querySelector(".result");    let obj ... Read More

Advertisements