- 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 6683 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
215 Views
For this, use the addEventListener() with mousedown event.ExampleFollowing is the code − Document document.addEventListener("mousedown", function () { console.log("Mouse down event is happening"); }); 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.OutputThis will produce the following output on console −When you click the mouse, the event will generate. The console output is shown below −
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
At first, set a new Date in JavaScript −var dateValue = new Date("2021-01-12 10:10:20");Use new Date() along with setHours() and getHours() to add time.ExampleFollowing is the code −var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); console.log("The date value is=" + dateValue.toString()); console.log("Only Hours value after incrementing=" + dateValue.getHours());To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo291.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo291.js The date value is=Tue Jan 12 2021 12:10:20 GMT+0530 (India Standard Time) Only Hours value after incrementing=12Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
271 Views
For this, use forEach() along with [] to an associative array.ExampleFollowing is the code −var result = {}; var names = ['John', 'David', 'Mike', 'Sam', 'Bob', 'Adam']; names.forEach((nameObject, counter) => { var generatedValues = { [nameObject]: counter }; Object.assign(result, generatedValues) }) console.log(result);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo290.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo290.js { John: 0, David: 1, Mike: 2, Sam: 3, Bob: 4, Adam: 5 }
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
3K+ Views
When we press the escape key, the event generated is detected using the keyup and keydown event handlers. When the escape key is pushed on the keyboard, the event handler runs across the page. Let’s dive into the article for getting better understanding on handling esc keydown on JavaScript popup window. Using keydown event When a key is pushed, the keydown event is triggered. The keydown event is called for all keys, regardless of whether they generate a character value, in contrast to the deprecated keypress event. While keypress shows which character was entered, keydown and keyup events give a ... Read More
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
340 Views
To obtain dates in JavaScript, use either the new Date() or Date() function Object() { [native code] } (either current date or a specific date). While the Date() function Object()produces a string containing the current date and time, the new Date() function Object()creates a new Date object. Let’s dive into the article for getting better understanding on how to format date value in JavaScript How to Format Dates in JavaScript? Date formatting is dependent on you and your requirements. In some nations, the year (06/22/2022) comes first, then the month. In other cases, the day precedes the month, the year ... Read More
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
2K+ Views
The task we are going to perform in this article is it possible to create a new data type in JavaScript. For instance, let’s consider we have variable student − var student = "username" console.log(typeof student) Now, I want to declare the type of that variable and print that in the output, and it will look like this − Output:"string" Let’s dive into the article to learn more about creating a new data type in JavaScript. Yes, it is possible to create a new data type in JavaScript, you can use the concept of Class. If you want ... Read More
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
544 Views
The innerHTML property of an element in JavaScript allows you to access or modify any HTML or XML markup that is there. Use the method insertAdjacentHTML () to insert HTML into the page as opposed to changing an element's content. In JavaScript to change a tag’s innerHTML based on their order can be done using document.querySelectorAll(). Let’s jump into the article to learn more about changing the tag’s innerHTML based on their order. Using document.querySelectorAll() The static NodeList returned by the Document function querySelectorAll() lists all of the document's elements that match the given set of selectors. Syntax Following is ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Let’s say the following is our button −Press MeOn filling the below input field, the color of the above button should change −ExampleFollowing is the code − Live Demo Document UserName: Press Me function changeTheColorOfButtonDemo() { if (document.getElementById("changeColorDemo").value !== "") { document.getElementById("buttonDemo").style.background = "green"; } else { document.getElementById("buttonDemo").style.background = "skyblue"; } } To run ... Read More
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
542 Views
These days, password generators can be found all over the internet. Without a strong enough password, websites frequently won't let you establish an account. The task we are going to perform was how to create a password generator JavaScript. Let’s dive into the article for getting better understanding on creating a password generator. For this, use Math.random() to create random passwords. Using Math.random() method A floating-point pseudo-random number in the range [0, 1], 0 (inclusive), and 1(exclusive) is returned by the JavaScript function Math.random(). You can then resize this random number to fit the necessary range. Syntax Following is the ... Read More
![Yaswanth Varma](https://www.tutorialspoint.com/assets/profiles/511236/profile/60_1602256-1662106429.jpg)
476 Views
In JavaScript Arrays are used to store multiple values in a single variable. This is compared to a variable that can store only one value. Each item in an array has a number attached to it, called a numeric index, that allows you to access it. In JavaScript, arrays start at index zero and can be manipulated with various methods. This basically tells about true or false. If the item is found then it returns true, else false is returned. You can achieve the same in JavaScript arrays, using includes. Let’s dive into the article to understand more about the ... Read More