Found 6710 Articles for Javascript

JavaScript program to decrement a date by 1 day

AmitDiwan
Updated on 17-Jul-2020 09:11:04

263 Views

Following is the code to decrement a date by 1 day 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: rebeccapurple;    } JavaScript program to decrement a date by 1 day Decrement Click on the above button to decrement the date by one day    let resEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let date = new Date();    sampleEle.innerHTML = date;    document.querySelector(".Btn").addEventListener("click", () => {       date.setDate(date.getDate() - 1);       resEle.innerHTML = "New Date = " + date;    }); OutputThe above code will produce the following output −On clicking the ‘Decrement’ button −

How to find keys of a hash in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 09:09:10

422 Views

Following is the code for finding keys of a hash in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Find keys of a hash in JavaScript DISPLAY Click on the above button to display the hash keys    let resEle = document.querySelector(".result");    let obj = {       firstName: "Rohan",       lastName: "Sharma",       age: 22,    };    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML += "object keys are " + Object.keys(obj);    }); OutputThe above code will produce the following output −On clicking the ‘DISPLAY’ button −

JavaScript regex program to display name to be only numbers, letters and underscore.

Alshifa Hasnain
Updated on 27-Feb-2025 19:27:31

552 Views

In this article, we will learn the regex program to display names to be only numbers, letters, and underscores In Java. Validating user input is essential for maintaining data integrity and security in web applications.  JavaScript Regex for Name Validation Regular expressions regex is probably more powerful in defining patterns to search for in the text validations. A typical validation is confirming a name consists of only letters (A-Z, a-z), digits (0-9), and underscore (_). To ensure that a name contains only letters we can use the following regex − /^\w+$/ ^: Start of the string ... Read More

How can we validate decimal numbers in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 09:02:49

522 Views

Following is the code to validate decimal numbers in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Validate decimal numbers in JavaScript CHECK Click the above button to check if a valid decimal number is entered or not    let resEle = document.querySelector(".result");    document.querySelector(".Btn").addEventListener("click", () => {       var str = document.querySelector(".num").value;       var regex = /^[-+]?[0-9]+\.[0-9]+$/;   ... Read More

How can I remove a child node in HTML using JavaScript?

AmitDiwan
Updated on 17-Jul-2020 08:58:54

200 Views

Following is the code to remove a child node in HTML using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Removing a child node in HTML using JavaScript Cow Lion Tiger Buffalo CLICK HERE Click the above button to remove the first list element from the above list    let resEle = document.querySelector(".result");    let animalList = document.querySelector(".animal");    document.querySelector(".Btn").addEventListener("click", () => {       animalList.removeChild(animalList.childNodes[0]);       animalList = animalList;    }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

How to load a JavaScript file Dynamically?

AmitDiwan
Updated on 17-Jul-2020 08:56:27

638 Views

Following is the code to load a JavaScript file dynamically −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Load a JavaScript file Dynamically CLICK HERE Click the above button to load external JavaScript    let resEle = document.querySelector(".result");    document.querySelector(".Btn").addEventListener("click", () => {       var jsRef = document.createElement("script");       jsRef.setAttribute("src", "sample.js");       document.getElementsByTagName("head")[0].appendChild(jsRef);    }); script.jsresEle.innerHTML='JavaScript has been loaded';OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

Named arguments in JavaScript.

AmitDiwan
Updated on 17-Jul-2020 08:53:14

235 Views

Following is the code for using named arguments in JavaScript functions −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample, .result {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: rebeccapurple;    } Named arguments in JavaScript functions CLICK HERE Click on the above button to call a function and pass above object to it as argument    let sampleEle ... Read More

JavaScript program to get a variable to count up/down on keyboard press.

Disha Verma
Updated on 13-Mar-2025 13:06:03

519 Views

When working with JavaScript, handling keyboard events and creating a program that gets a variable to count up/down on keyboard press can be an essential skill. Keyboard events such as keydown and keyup in JavaScript are actions that occur when a user interacts with the keyboard. This article explains how to create a JavaScript program that increments or decrements a variable based on specific keyboard key presses. This feature can be helpful in interactive websites, like games or tools that check user input. Table of Content You can create a JavaScript program to get a variable to count up ... Read More

Can we have a return statement in a JavaScript switch statement?

AmitDiwan
Updated on 17-Jul-2020 08:47:22

21K+ Views

The JavaScript switch statement can contain return statements if it is present inside a function. The function will return the value in the switch statement and the code after the switch statement will not be executed.Following is the code to have return statements in JavaScript switch statement −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Return statement in JavaScript switch Enter day 1-7 CHECK ... Read More

How to group array of objects by Id in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 08:43:33

2K+ Views

Following is the code to group array of objects by id 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;    } group the array of objects by Id in JavaScript { name: 'Rohan', age: 18 }, { name: 'Mohan', age: 20 }, { name: 'Shawn', age: 18 }, { name: 'Michael', age: 18 }, { name: 'David', age: 20 } ... Read More

Advertisements