- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 9313 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
35 Views
For iterating and printing, use forEach() loop in JavaScript.Exampleconst details =[ { "studentId":101, "studentName": "John Doe", }, { "studentId":102, "studentName": "David Miller", }, ]; details.forEach(obj=>{ console.log("StudentId="+obj.studentId); console.log("StudentName="+obj.studentName); })To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo174.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo174.js StudentId=101 StudentName=John Doe StudentId=102 StudentName=David Miller
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
54 Views
You can use destructed array in this case.Examplefunction multiply(firstParameterDefaultValue=10, secondParameterValue) { return firstParameterDefaultValue * secondParameterValue; } console.log("The result="+multiply(...[,10]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo173.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo173.js The result=100
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
147 Views
If you won’t pass value to a function(), it will print the default value otherwise given parameter will be printed.Following is the code. We are setting a default here i.e. “Jack” in this case to avoid any undefined error when a function is called without any parameter −Examplefunction display({ name = 'Jack' } = {}) { console.log(`Hi My Name is ${name}!`); } display(); display({name:"Taylor Swift"});To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo171.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo171.js Hi My Name is Jack! Hi My ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Yes, we can use a new line using “” in console.log(). Following is the code −Exampleconst studentDetailsObject = new Object() studentDetailsObject.name = 'David' studentDetailsObject.subjectName = 'JavaScript' studentDetailsObject.countryName = 'US' studentDetailsObject.print = function(){ console.log('hello David'); } console.log("studentObject", "", studentDetailsObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo170.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo170.js studentObject { name: 'David', subjectName: 'JavaScript', countryName: 'US', print: [Function] }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
7K+ Views
For this, use JSON.parse() along with JSON.stringify().Example Live Demo Document Click The Button To get the Pretty JSON function printTheJSONInPrettyFormat() { var badJSON = document.getElementById('prettyJSONFormat').value; var parseJSON = JSON.parse(badJSON); var JSONInPrettyFormat = JSON.stringify(parseJSON, undefined, 4); document.getElementById('prettyJSONFormat').value = JSONInPrettyFormat; } To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
480 Views
Let’s say the following is our radio button group − Gender: Male Female To check a radio in radio group, you need to set checked property to true in JavaScript. Following is the code −Example Live Demo Document Gender: Male Female var tagValues = document.getElementsByTagName('input'); for (const obj of tagValues) { if (obj.value === 'Male'){ obj.checked = 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” in VS Code editor.OutputThis will produce the following output −
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
321 Views
For this, you can use the concept of modular operator along with divide. Following is the code −Examplevar divideInteger = function(value, divide) { var num; var modular = value % divide; if(modular == 0){ num = value/divide; sumOfDivideParts = Array(divide).fill(num); } else { num = (value-modular)/divide; sumOfDivideParts = Array(divide).fill(num); for(i=0;i node demo169.js [ 6, 6, 6, 6, 6, 6, 7, 7 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
50 Views
Let’s say the following is our object −var lastName ={ "John":"Smith", "David":"Miller", "Bob":"Taylor" }Following is our array −var firstName=[ "Bob", "John", "David" ]Display resultant array based on the object’s order determined by the first array, use map(). Following is the code −Examplevar firstName=[ "Bob", "John", "David" ] var lastName ={ "John":"Smith", "David":"Miller", "Bob":"Taylor" } var values = firstName.map(getValues => lastName[getValues]); console.log(values);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo168.js. This will produce the following output −PS C:\Users\Amit\javascript-code> node ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
339 Views
To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter.Examplefunction replaceWithTheCapitalLetter(values){ return values.charAt(0).toUpperCase() + values.slice(1); } var word="javascript" console.log(replaceWithTheCapitalLetter(word));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo167.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo167.js Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
166 Views
To avoid using variable name as a literal, use square brackets. Following is the code −Examplevar name = "David" var putTheAllData = [] putTheAllData.push( { name: "The name is name will remain same" } ) putTheAllData.push( { [name]: "The name is David will be changed [name]"} ) console.log(putTheAllData);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo166.js. This will produce the following output −PS C:\Users\Amit\javascript-code> node demo166.js [ { name: 'The name is name will remain same' }, { David: 'The name is David will be changed [name]' } ]