
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

838 Views
Let’s say the following is our object −var details = { firstName: 'John', lastName: '', countryName: 'US' }Use Object.keys() along with find() to get the key name with empty value. Following is the code −Examplevar details = { firstName: 'John', lastName: '', countryName: 'US' } var result = Object.keys(details).find(key=> (details[key] === '' || details[key] === null)); console.log("The key is="+result);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo118.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo118.js The key is=lastNameRead More

5K+ Views
Let’s say the following is our object −var apiJSONObject = [ {subjectName:"MySQL"}, {subjectName:"Java"}, {subjectName:"JavaScript"}, {subjectName:"MongoDB"} ]Let’s check for existence of a value “JavaScript” −Examplevar apiJSONObject = [ {subjectName:"MySQL"}, {subjectName:"Java"}, {subjectName:"JavaScript"}, {subjectName:"MongoDB"} ] for(var i=0;i node demo117.js The search found in JSON Object

3K+ Views
Let’s see an example wherein we are validating input text onsubmit −Example Live Demo Document function validateTheForm(){ var validation = (document.getElementById('txtInput').value == 'gmail'); if(!validation){ alert('Something went wrong...Plese write gmail intext box and click'); return false; } return true; } 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” ... Read More

2K+ Views
For this, use forEach() loop along with push(). Following is the code −Examplevar studentDetails= [ { studentId:1, studentName:"John" }, { studentId:1, studentName:"David" }, { studentId:2, studentName:"Bob" }, { studentId:2, studentName:"Carol" } ] studentObject={}; studentDetails.forEach (function (obj){ studentObject[obj.studentId] = studentObject[obj.studentId] || []; studentObject[obj.studentId].push(obj.studentName); }); console.log(studentObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo116.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo116.js { '1': [ 'John', 'David' ], '2': [ 'Bob', 'Carol' ] }

1K+ Views
You can create an associative array in JavaScript using an array of objects with key and value pair.Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys.Examplevar customerDetails= [ { "customerId":"customer-1", "customerName":"David", "customerCountryName":"US" }, { "customerId":"customer-2", "customerName":"Bob", "customerCountryName":"UK" }, { "customerId":"customer-3", "customerName":"Carol", "customerCountryName":"AUS" } ] for(var i=0;i node demo115.js David Bob Carol

786 Views
Let’s say the following is our array −var listOfNames = [ 'John', 'David', 'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents of the particular index in reverse order.Example Live Demo Document Click The Button To get the Reverse Value var listOfNames = [ 'John', 'David', 'Bob' ]; count=listOfNames.length-1; function reverseTheArray(){ document.getElementById('reverseTheArray').innerHTML = listOfNames[count]; count--; if (count

4K+ Views
To assign multiple variables to the same value, the syntax is as followsvar anyVariableName1, anyVariableName2, anyVariableName3……….N; yourVariableName1=yourVariableName2=yourVariableName3=.........N=yourValue;Let’s say the following are our variables and we are assigning same value −var first, second, third, fourth, fifth; first=second=third=fourth=fifth=100;Examplevar first, second, third, fourth, fifth; first=second=third=fourth=fifth=100; console.log(first); console.log(second); console.log(third); console.log(fourth); console.log(fifth); console.log("The sum of all values="+(first+second+third+fourth+fifth));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo114.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo114.js 100 100 100 100 100 The sum of all values=500Read More

152 Views
To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −Examplevar studentObject={ 101: "John", 102: "David", 103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId,studentName])=>({studentId ,studentName})); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo113.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo113.js [ { studentId: '101', studentName: 'John' }, { studentId: '102', studentName: 'David' }, { studentId: '103', studentName: 'Bob' } ]

374 Views
For this, use map() along with join(‘’).Then ‘’ is for new line. Following is the code −ExamplestudentDetails = [ [101, 'John', 'JavaScript'], [102, 'Bob', 'MySQL'] ]; var studentFormat = '||Id||Name||subjName||'; var seperate = ''; seperate = seperate + studentDetails.map(obj => `|${obj.join('|')}|`).join(''); studentFormat = studentFormat + seperate; console.log(studentFormat);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo112.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo112.js ||Id||Name||subjName|| |101|John|JavaScript| |102|Bob|MySQL|

276 Views
Let’s say the following is our current array −var listOfNames=["John", "Mike", "Sam", "Carol"];Use JSON.parse(JSON.stringify()) to create new array and set values from the old array above.Examplefunction createNewArray(listOfNames) { return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames=["John", "Mike", "Sam", "Carol"]; var namesArray = listOfNames.slice(); console.log("The new Array="); console.log(namesArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo111.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo111.js The new Array= [ 'John', 'Mike', 'Sam', 'Carol' ]Read More