- 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)
302 Views
To get seconds since epoch, use the below syntax −var anyVariableName= new Date(); Math.round(yourDateVariableName.getTime() / 1000);At first, get the current date −var currentDate= new Date();Now, get seconds since epoch −Examplevar currentDate= new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log(epochSeconds);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo67.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo67.js 1594821507
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
654 Views
As we know the value 0 is for day Sunday and 6 for Saturday. First of all, you need to get day with the help of getDay().Let’s set a date −var givenDate=new Date("2020-07-18");Now, we will get the day −var currentDay = givenDate.getDay();Following is the code to determine if date is weekend −Examplevar givenDate=new Date("2020-07-18"); var currentDay = givenDate.getDay(); var dateIsInWeekend = (currentDay === 6) || (currentDay === 0); if(dateIsInWeekend==true){ console.log("The given date "+givenDate+" is a Weekend"); } else { console.log("The given date " +givenDate+"is a not a Weekend"); }To run the above program, you need to use the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
12K+ Views
Let’s say following is our array −const names = ['John', 'David', 'Mike', 'Sam', 'Carol', 'Bob'];Now, we will filter the array −var nameObject=names.filter((allNameObject) => !['David', 'Mike', 'Sam', 'Carol'].includes(allNameObject));Exampleconst names = ['John', 'David', 'Mike', 'Sam', 'Carol', 'Bob']; console.log("The names are="); console.log(names); var nameObject=names.filter((allNameObject) => !['David', 'Mike', 'Sam', 'Carol'].includes(allNameObject)); console.log("After filter="); console.log(nameObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo65.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo65.js The names are= [ 'John', 'David', 'Mike', 'Sam', 'Carol', 'Bob' ] After filter= [ 'John', 'Bob' ]Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
117 Views
Flat array of objects into an object, you can use the concept of reduce(). Let’s say following is our array of objects −const studentDetails = [ {Name: "Chris"}, {Age: 22} ]Exampleconst studentDetails = [ {Name: "Chris"}, {Age: 22} ] const objectStudent = studentDetails.reduce((obj, value) => { return { ...obj, ...value } }, {}) console.log(objectStudent);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo64.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo64.js { Name: 'Chris', Age: 22 }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
205 Views
To split a string with separator after every word, the syntax is as follows −var anyVariableName=yourVariableName.split('parameter').filter(value=>value)Let’s say, the following is our string with separator −var sentence="-My-Name-is-John-Smith-I-live-in-US";Now, split the string with separator as in the below code.Examplevar sentence="-My-Name-is-John-Smith-I-live-in-US"; console.log("The value="+sentence); var result=sentence.split('-').filter(value=>value) console.log("After split()="); console.log(result);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo63.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo63.js The value=-My-Name-is-John-Smith-I-live-in-US After split()= [ 'My', 'Name', 'is', 'John', 'Smith', 'I', 'live', 'in', 'US' ]Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
455 Views
You can use the length property from JavaScript to get the length. Let’s create an Associative array −var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript";Let us now get the length of the associative array −Examplevar details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript"; console.log("The length=="+Object.keys(details).length);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo.js The length==4Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
846 Views
To convert the HH:MM:SS to seconds, you need to multiply the HOUR with 60X60 and Minute with 60:Examplevar time = '10:8:20'; var actualTime = time.split(':'); console.log("The time="+time); var totalSeconds = (+actualTime[0]) * 60 * 60 + (+actualTime[1]) * 60 + (+actualTime[2]); console.log("Total Seconds="+totalSeconds);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo61.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo61.js The time=10:8:20 Total Seconds=36500
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
You need to subtract one week i.e. 7 days from the current date. Following is the syntax −var anyVariableName=new Date(yourCurrentDate.setDate(yourCurrentDate.getDate() - 7)At first, get the current date −var currentDate = new Date(); console.log("The current Date="+currentDate);Now, set the new date with setDate() method and subtract 7 days −Examplevar currentDate = new Date(); console.log("The current Date="+currentDate); var before7Daysdate=new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date="+before7Daysdate);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo60.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo60.js The current Date=Tue Jul 14 2020 19:12:43 GMT+0530 (India ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
149 Views
To updates, use the concept of fill() in JavaScript. The fill() method is used to fill the array elements with a static value. Following is the code −Exampleconst array= Array(4) var fillWithTrueValue=array.fill(true); const matrixWithOnlyBooleanTrue = Array(4).fill(fillWithTrueValue); console.log(matrixWithOnlyBooleanTrue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo59.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo59.js [ [ true, true, true, true ], [ true, true, true, true ], [ true, true, true, true ], [ true, true, true, true ] ]