
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

187 Views
Let’s say the following is our string with whitespace −var fullName=" John Smith ";Use replace() and set Regex in it to remove whitespaces.Examplefunction removeSpacesAtTheBeginningAndTheEnd(name) { return name.toString().replace(/^\s+|\s+$/g,''); } var fullName=" John Smith "; var valueAfterRemovingSpace=removeSpacesAtTheBeginningAndTheEnd(fullName) console.log(valueAfterRemovingSpace);To run the above program, you need to use the following command −node fileName.js.Here my file name is demo208.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo208.js John Smith

455 Views
Let’s say the following is our array −var numbers = [10, 3, 40, 50, 20, 30, 100]We need to search two numbers from the above array elements, whose sum is 80.For this, use simple for loop with if condition.Examplefunction specificPairsOfSumOfTwoNumbers(numbers, totalValue) { var storeTwoNumbersObject = {} for(var currentNumber of numbers) { if(storeTwoNumbersObject[currentNumber]) { return { firstNumber: totalValue-currentNumber, secondNumber:currentNumber} } ... Read More

4K+ Views
Let’s say the following is our JSON array −var details = [ { "customerDetails": [ { "customerName": "John Smith", "customerCountryName": "US" } ] }, { "customerDetails": [ { "customerName": "David Miller", "customerCountryName": "AUS" } ] }, { ... Read More

2K+ Views
To make addEventListener() repeatedly work on button clicks, use the following code −Example Live Demo Document Click Me var eventValue = function (event) { document.body.appendChild(document.createElement('div')) .textContent = event.type; } var pressed = document.querySelector("#pressButtonDemo"); pressed.addEventListener("click", eventValue); 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 −When you click the button “Click Me” then you will get ... Read More

164 Views
Let’s say the following is our array with elements preceded by a + sign −var studentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ];To remove the + sign, the code is as follows −ExamplestudentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ]; console.log("The actual array="); console.log(studentNames); studentNames = studentNames.map(function (value) { return value.replace('+', ''); }); console.log("After removing the + symbol, The result is="); console.log(studentNames);To run the above program, you need to use the following command −node fileName.js.Here my file ... Read More

1K+ Views
Form handling is a fundamental aspect of web development. A Submit button sends the form data for processing, while a Clear button allows users to reset or empty the form inputs. This functionality enhances user experience and ensures proper data entry.In this tutorial, we'll learn how to create a form with a Submit button for handling submissions and a Clear button for resetting inputs using JavaScript. We'll also handle the actions triggered by these buttons effectively. Creating a submit button for form submission Below is an example of creating a submit button for form submission and another button to ... Read More

4K+ Views
Let’s say the following is our button −Sum We are calling the function addTheValue(10) with parameter 10 on button click. On clicking the button, we are adding the value 10 as in the below code −Example Live Demo Document Adding 10 each time whenever you click the Sum Button...... 10 Sum function addTheValue(secondValue) { var fValue = document.getElementById("firstValue"); firstValue.innerHTML = parseInt(fValue.innerHTML) + parseInt(secondValue); } To run the above program, save the file ... Read More

1K+ Views
Let’s say the following is our dropdown (select) − John Mike Sam David Carol Following is the code to get the length of list options in JavaScript −Example Live Demo Document John Mike Sam David Carol var lengthOfListOptions = $("#lengthListOfOptionDemo option").length; console.log("The length is=" + lengthOfListOptions); 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 −

299 Views
Let’s say the following is our array −var details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'Javascript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ];Now, use the concept of map(). The code is as follows −Examplevar details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'JavaScript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ]; var output = details.map((detailsObject, index) => { var tempObject = {}; tempObject.subjectId= detailsObject.subjectId; tempObject.subjectName = detailsObject.subjectName; const getThePreviousObject = index != 0 ? details[index-1] : null; tempObject.previousSubjectName = ... Read More

3K+ Views
The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it.For example −The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3Let's write the code for this function −Exampleconst num = 2353454; const digits = (num, count = 0) => { if(num){ return digits(Math.floor(num / 10), ++count); }; return count; }; console.log(digits(num)); console.log(digits(123456)); console.log(digits(53453)); console.log(digits(5334534534));OutputThe output in the console will be −7 6 5 10