- 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 9315 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
351 Views
Let’s say the following is our object −const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ]You can use split() on the basis of space.ExampleFollowing is the code −const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ] const objectValues = employeeDetails.map(emp => { var ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
197 Views
Let’s say the following are our array elements −10,20,10,50,60,10,20,40,50To remove duplicate elements, use … new Set().ExampleFollowing is the code −var arrayWithNoDuplicateNumbers = [...new Set([10,20,10,50,60,10,20,40,50])]; console.log("No Duplicate values=") console.log(arrayWithNoDuplicateNumbers);To run the above program, use the following command −node fileName.js. Here, my file name is demo243.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo243.js No Duplicate values= [ 10, 20, 50, 60, 40 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
131 Views
For this, you need to use if condition to compare triplets.Let’s say we are passing the following values −35, 36, 37, 33, 48, 50ExampleFollowing is the code −function tripletsSolution(first, second, third, fourth, fifth, sixth) { var storedResult = [] if (first > fourth || second > fifth || third > sixth) { storedResult = storedResult + 1; } if (first < fourth || second < fifth || third < sixth) { storedResult = storedResult + 1; } return storedResult.split(''); } console.log(tripletsSolution(35, 36, 37, 33, 48, 50));To run the above program, use the following command −node fileName.js.Here, my file name is demo242.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo242.js [ '1', '1' ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ Views
To fire an alert on click of a button, use addEventListener(). Let’s say the following is our button on an HTML web page −Please Press MeExampleFollowing is the code − Live Demo Document Please Press Me var pressedButton = document.getElementsByTagName("button")[0]; pressedButton.addEventListener("click", function (event) { alert("You have pressed the button..........") }) To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
344 Views
Let’s say we have the following hyphen delimited string with negative or range of numbers −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS";To split, use regular expressions. Following is the code −ExampleFollowing is the code −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; var regularExpression = /-(?=[A-Za-z-]|\d+-\d)/; var result1 = firstValue.split(regularExpression); var result2 = secondValue.split(regularExpression); console.log(result1); console.log(result2);To run the above program, use the following command −node fileName.js. Here, my file name is demo241.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo241.js [ 'John', 'Smith', '90-100', 'US' ] [ 'David', 'Miller', '-120', 'AUS' ]Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
203 Views
There is no in-built function to replace all occurrences of word. You need to create your own function.Let’s say the following is our string −var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";ExampleFollowing is the code −var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; console.log(sentence); function replaceYesWithHi(word, sentence, replaceValue) { return sentence.split(word).join(replaceValue); } var replacementofYesWithHi = replaceYesWithHi("Yes", sentence, "Hi"); console.log(replacementofYesWithHi);To run the above program, use the following command −node fileName.js.Here, my file name is demo240.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo240.js Yes, My Name is John Smith. I live in US. Yes, My Favourite ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
309 Views
Let’s say the following is our string”var stringValue="453.000.00.00.0000";To convert the above string with zeroes to number, use parseInt() along with replace() −var numberValue = parseInt(stringValue.replace(/\./gm, '')); ExampleFollowing is the complete code −var stringValue="453.000.00.00.0000"; var numberValue = parseInt(stringValue.replace(/\./gm, '')); console.log("Original value="+stringValue) console.log("After modification the value="+numberValue);To run the above program, use the following command −node fileName.js. Here, my file name is demo239.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo239.js Original value=453.000.00.00.0000 After modification the value=45300000000000
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
Let’s say the following is our string −const keyName = 'username';To set a string as a key for an object, use the [] and pass the string name −const stringToObject = { [keyName]: 'David Miller' };ExampleFollowing is the complete code −const keyName = 'username'; const stringToObject = { [keyName]: 'David Miller' }; console.log("Your String Value="+keyName); console.log("Your Object Value=") console.log(stringToObject);To run the above program, use the following command −node fileName.js.Here, my file name is demo238.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo238.js Your String Value=username Your Object Value= { username: 'David Miller' }Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
605 Views
The best way to reduce and merge a collection of objects, use the concept of Object.values() along with reduce().Following is the object −var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: 11, marks: 98, studentName: "Bob" } ];ExampleFollowing is the code to reduce and merge −var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: 11, marks: 98, studentName: "Bob" } ]; output = Object.values(details.reduce((value, object) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
414 Views
Let’s say the following are our time data −var startHour = dayjs().hour(10) var endHour = dayjs().hour(22)To get the difference, use the diff() method −ExampleFollowing is the code − Live Demo Document var startHour = dayjs().hour(10) var endHour = dayjs().hour(22) console.log("The hours difference is=" + endHour.diff(startHour, "hours")); To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output is as follows −