Found 6710 Articles for Javascript

JavaScript Convert a string to boolean

AmitDiwan
Updated on 07-May-2020 14:08:17

261 Views

To convert a string to boolean in JavaScript, the code is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    } Converting string to boolean CLICK HERE Click the above button to convert the string to boolean value    let sampleEle = document.querySelector(".sample");    let inputEle = document.querySelector(".str");    document.querySelector(".Btn").addEventListener("click", () => {       sampleEle.innerHTML += "Using == " + (inputEle.value == "true") + "";       sampleEle.innerHTML += "Using === " + (inputEle.value === "true") + "";    }); OutputOn writing something in the input field other than string ‘true’ and clicking the button −

JavaScript Const

AmitDiwan
Updated on 07-May-2020 14:04:50

285 Views

The JavaScript const declarations create variables that cannot be reassigned to some other value or redeclared later. It was introduced in ES2015.Following is the code for JavaScript const declaration −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    } Const example CLICK HERE Click the above button to try changing const value    let sampleEle = document.querySelector(".sample");    const a = 33;    sampleEle.innerHTML = "a = " + a + "";    document.querySelector(".Btn").addEventListener("click", () => {       try {          a = 44;       } catch (err) {          sampleEle.innerHTML += "Error : " + err;       }    }); OutputOn clicking the ‘CLICK HERE’ button to change constant value −

JavaScript compile() Method

Disha Verma
Updated on 13-Mar-2025 13:07:42

559 Views

The compile() method in JavaScript is a lesser-known method that has been associated with two main contexts: compiling regular expressions and validating JavaScript syntax. This method has been deprecated since JavaScript version 1.5. But developers must know about this. What is the compile() Method? The compile() method allows developers to update an existing regular expression (RegExp) object with a new pattern and flags. This is helpful when you need to change a regular expression without making a new object. Syntax and Parameters Here is the basic syntax for this: RegExpObject.compile(pattern, flags) Let's understand the parameters used with ... Read More

JavaScript clearTimeout() & clearInterval() Method

Shriansh Kumar
Updated on 30-Jul-2024 16:32:42

2K+ Views

In JavaScript, the clearTimeout() and clearInterval() methods are used to clear timers. When we set a timer using setTimeout() or setInterval() methods, the browser allocates memory to track it. Therefore, we use these methods to release that memory and avoid unnecessary function calls. It is best practice to clear timers when they are no longer needed. In this article, we will learn how the clearTimeout() and clearInterval() methods help to clear timers. JavaScript clearTimeout() method The clearTimeout() method in JavaScript clears the timeout that has been previously set by the setTimeout() function. It accepts a single integer value as a ... Read More

JavaScript Chart.js

AmitDiwan
Updated on 07-May-2020 13:50:15

494 Views

Chart.js is an open source JavaScript library. Using Chart.js, add animated, interactive graphs on your website.Following is the code for Chart.js library in JavaScript −Example Live Demo

JavaScript Callbacks

Alshifa Hasnain
Updated on 26-Feb-2025 19:37:22

750 Views

In JavaScript, since functions are objects we can pass them as parameters to another function. These functions can then be called inside another function and the passed function is referred to as a callback function. Why Use Callbacks? The following are the advantages of using JavaScript Callbacks − Encapsulation: Separates logic for better modularity. Reusability: Allows different callback functions for different processing needs. Asynchronous Execution: Commonly used in event handling, API requests, and file operations. Using a Callback Function Below is an example where an add() ... Read More

JavaScript Auto-filling one field same as other

AmitDiwan
Updated on 07-May-2020 13:38:41

371 Views

To auto-filling one field same as other, the JavaScript script is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    } JavaScript Auto-filling one field same as other Enter your primary address Address Pin Code Secondary Address same as primary Same Address Enter your secondary address Address Pin Code    let checkEle = document.querySelector(".check");    let addressEle = document.querySelectorAll(".address");    let pinCodeEle = document.querySelectorAll(".pincode");    checkEle.addEventListener("change", (event) => {       if (event.target.checked) {          console.log(addressEle[0].value);          addressEle[1].value = addressEle[0].value;          pinCodeEle[1].value = pinCodeEle[0].value;       }    }); OutputOn filling the primary address and clicking the “Same Address” checkbox −

JavaScript Symbol.hasInstance Property

AmitDiwan
Updated on 07-May-2020 13:35:12

107 Views

The Symbol.hasInstance property is used to check if a constructor recognizes the object as its instance.Following is the code for Symbol.hasInstance property −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    div {       font-size: 20px;       font-weight: 500;    } JavaScript Symbol.hasInstance Property CLICK HERE Click on the above button to see if the above symbols are instance of array and custom() respectively    let fillEle = document.querySelector(".sample");    let ele = [1, 2, ... Read More

JavaScript Symbol.for() function

AmitDiwan
Updated on 07-May-2020 13:31:07

106 Views

The Symbol.for() function searches runtime-wide symbol registery for a given symbol. If the symbol is found then it is returned otherwise a new symbol is created in the global symbol registery and simply returned.Following is the code for symbol.for() functionExample Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    div {       font-size: 20px;       font-weight: 500;    } JavaScript Symbol.for() function CLICK HERE Click on the above button to get the symbols.    let ... Read More

JavaScript symbol.@@toPrimitive() function

AmitDiwan
Updated on 07-May-2020 13:27:48

122 Views

The symbol.@@toPrimitive() function converts a symbol object to a primitive value.SyntaxSymbol()[Symbol.toPrimitive](hint)The hint value specifies the type eg − number, string, etc and is an optional argument.Following is the code for symbol.@@toPrimitive() function −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    div {       font-size: 20px;       font-weight: 500;    } JavaScript symbol.@@toPrimitive() function CLICK HERE Click on the above button to add the object with a number.    let fillEle = document.querySelector(".sample");   ... Read More

Advertisements