Creating JavaScript Constructor Using the New Operator

AmitDiwan
Updated on 16-Jul-2020 07:07:23

158 Views

Following is the code for creating a JavaScript constructor using the ‘new’ operator −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } JavaScript constructor using new CLICK HERE Click on the above button to create JavaScript constructor using new operator    let resEle = document.querySelector(".result");    function Human(firstName, lastName, age){       this.firstName = firstName;       this.lastName = lastName;       this.age ... Read More

Add Property Method to JavaScript Constructor

AmitDiwan
Updated on 16-Jul-2020 07:05:10

233 Views

Following is the code to add a property, method to a JavaScript constructor −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       } Add property, method to javaScript constructor CLICK HERE Click on the above button to add property, method to constructor    let resEle = document.querySelector(".result");    function Human(firstName, lastName){       this.firstName = firstName;       this.lastName = lastName;    } ... Read More

Stop a Function During Its Execution in JavaScript

AmitDiwan
Updated on 16-Jul-2020 07:04:12

5K+ Views

To stop a function during its execution, use the concept of −document.getElementById().addEventListener().Example Live Demo Document Call the function Stop the function execution    document.getElementById("call").addEventListener("click", callFunction);    document.getElementById("halt").addEventListener("click", haltFunction);    var timeValue = null;    function callFunction() {       timeValue = setInterval(function() {          console.log("The call() is being executed....");       }, 1000);    }    function haltFunction() {       clearInterval(timeValue);    } To run the above program, save the file name anyName.html(index.html) and right click on the file and select ... Read More

Add, Access and Delete JavaScript Object Properties

AmitDiwan
Updated on 16-Jul-2020 07:02:23

161 Views

Following is the code to add, access and delete JavaScript object properties −Example Live Demo

Range Overflow and Range Underflow Properties in JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:59:54

736 Views

Range Underflow − If the element value is less than that of the specified in min attribute, it sets to true.Range Overflow − If the element value is more than that of the specified in max attribute, it sets to true.Following is the code for Range overflow and range underflow property in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Range Overflow and Range Underflow property ... Read More

Convert JSON Text to JavaScript JSON Object

AmitDiwan
Updated on 16-Jul-2020 06:57:01

867 Views

The JSON parse() method is used for converting a JSON text to a JavaScript object.Following is the code for converting JSON text to JavaScript JSON object −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 18px;       font-weight: 500;       color: red;    } JSON parse() Method {"name":"Rohan", "sports":["Cricket", "Football"], "country":"India"} CLICK HERE Click on the above button to parse the above JSON string    let ... Read More

Add oninput Attribute to HTML Element with JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:54:17

604 Views

For this, use the concept of document.getElementById() along with addEventListener(). Following is the JavaScript code −Example Live Demo Document Enter the value:    document.getElementById('enterValue').addEventListener("input", function () {       console.log('you have entered the value');    }); To run the above program, save the file nameanyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.OutputWhen user enters a value −

Create JSON Object in JavaScript with Example

AmitDiwan
Updated on 16-Jul-2020 06:54:14

475 Views

Following is the code to create a JSON object in JavaScript.Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } JSON object in JavaScript JSON OBJECT CLICK HERE Click on the above button to create a JSON object and display it    let resEle = document.querySelector(".result");    let obj = {       firstName:'Rohan',       lastName: 'Sharma',       Age:14,    }    document.querySelector(".Btn").addEventListener("click", () => {       for(i in obj){          resEle.innerHTML += 'key = '+i+' value = ' + obj[i] + '';       }    }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

Get Number from User Input and Display in Console with JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:52:55

3K+ Views

You can use # to get the value when user clicks the button using document.querySelector(“”); Following is the JavaScript code −Example Live Demo Document Example: choose a number between 1 to 100 Give a number: Click Me    function example() {       let btn = document.querySelector("#choose");       let randomNumber = Math.ceil(Math.random() * 100);       btn.onclick = function() {          let givenNumber = document.querySelector("#inputNumber").value;          let valueInt = parseInt(givenNumber, 100);   ... Read More

Create a Constant Array in JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:51:41

2K+ Views

To create a const array in JavaScript we need to write const before the array name. The individual array elements can be reassigned but not the whole array.Following is the code to create a constant array in JavaScript.Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 20px;       font-weight: 500;    } Const array in JavaScript CLICK HERE Click on the above button to change array values and reassign the ... Read More

Advertisements