
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6710 Articles for Javascript

451 Views
To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year= document.getElementById("theCurrentYear").innerHTML = new Date().getFullYear(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

2K+ Views
For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo103.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" {"name": "John", "subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }Read More

169 Views
The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = {first: second => second + 1000} var object2= Object.assign({}, object); console.log("The result="); console.log(object2.first(1000));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo102.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo102.js The result= 2000Read More

307 Views
Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the concept of remove(). Following is the code −Example Live Demo Document START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); ... Read More

824 Views
For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Live Demo Document My Name is John My h6 value must be here... My h6 value must be here... My h6 value must be here... const allSpan = document.querySelectorAll('.my-main-div-class span'), repaceAboveText = document.getElementsByClassName('uniqueId')[0].textContent; for (var newElement of allSpan){ newElement.textContent=repaceAboveText } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

1K+ Views
In this article, we will learn to remove leading zeroes from numbers in JavaScript using Regex. When working with numeric strings in JavaScript, it’s common to encounter unwanted leading zeroes. These zeroes can cause issues when parsing numbers, formatting data, or displaying user-friendly outputs. Why Remove Leading Zeroes in JavaScript? Leading zeroes can lead to inconsistencies in numeric string representation. For instance − When parsing numbers, leading zeroes might result in incorrect interpretations. Displaying numbers with leading zeroes can confuse users or look unprofessional. Clean strings are ... Read More

434 Views
Let’s say the following are our array of objects −var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C'] }]We need to add the following in the already created array of objects −{firstName: "Bob", listOfSubject: ['JavaScript']};Examplevar studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']}]; updateThisObject = {firstName: "Bob", listOfSubject: ['JavaScript']}; function forLoopExample(studentObjects, updateObj){ for(var index = 0;index < studentObjects.length; index++) { if(updateObj.listOfSubject.join("") === studentObjects[index].listOfSubject.join("")) { studentObjects[index] = updateObj; ... Read More

648 Views
Let’s say the following are our objects −var first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000}You can use the concept of hasOwnProperty() to add properties from one object to another. Following is the code −Examplevar first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000} function addPropertiesWithoutOverwritting(first, second) { for (var key2 in second) { if (second.hasOwnProperty(key2) && !first.hasOwnProperty(key2)) { first[key2] = second[key2]; } } return first; } console.log(addPropertiesWithoutOverwritting(first, second))To run ... Read More

119 Views
For this, use filter() along with map(). Let’s say the following is our array −const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]We will assign a new value to the name “Bob”. Following is the code −Exampleconst studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ] var changeName = "Bob"; studentDetails.filter((obj) => obj.Name === changeName).map((obj) => obj.Name = "Carol"); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo98.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More

146 Views
Use the concept of freeze() from JavaScript to disallow adding new properties to an object, altering of object properties, etc.Following is the code wherein we are changing the value but the previous value still remains since we cannot alter properties using freeze() −Exampleconst canNotChangeTheFieldValueAfterFreeze = {value1 : 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); canNotChangeTheFieldValueAfterFreeze.value = 100; console.log("After changing the field value1 from 10 to 100 ="+canNotChangeTheFieldValueAfterFreeze.value1);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo97.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo97.js After changing the field value1 from 10 ... Read More