Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 275 of 534
What is the usage of onunload event in JavaScript?
The onunload event triggers when a user navigates away from a page, closes the browser tab, or refreshes the page. This event is useful for cleanup tasks like saving user data or showing farewell messages. Syntax // HTML attribute // JavaScript event listener window.addEventListener('unload', function(event) { // Cleanup code }); Example: Using onunload with HTML Attribute Close the page and see what happens! This event may give unexpected results ...
Read MoreWhat is nodeValue property in JavaScript HTML DOM?
The nodeValue property in JavaScript HTML DOM returns the value of a node. It returns null for element nodes and the actual content for text nodes. Syntax node.nodeValue Return Value The nodeValue property returns: Text nodes: The text content Comment nodes: The comment text Element nodes: null Attribute nodes: The attribute value Example: Getting Text Node Value You can try to run the following code to learn how to get nodeValue property. ...
Read MoreWhich event occurs in JavaScript when an element is content is copied to clipboard?
When a user copies an element's content, the oncopy event triggers in JavaScript. This event fires when the user performs a copy operation using Ctrl+C, right-click copy, or any other method to copy selected content. Syntax element.oncopy = function() { // Code to execute when content is copied }; // Or using addEventListener element.addEventListener('copy', function() { // Code to execute when content is copied }); Example: Basic oncopy Event ...
Read MoreWhat is the role of altKey Mouse Event in JavaScript?
The altKey property of mouse events in JavaScript indicates whether the Alt key was pressed when a mouse button was clicked. This property returns a boolean value: true if the Alt key was held down, false otherwise. Syntax event.altKey Return Value Returns true if the Alt key was pressed during the mouse event, false if it was not pressed. Example The following example demonstrates how to detect whether the Alt key was pressed during a mouse click: Press and hold ALT key and ...
Read MoreHow [ ] is converted to Number in JavaScript?
In JavaScript, an empty array [] converts to the number 0 when using type coercion. This happens through a multi-step conversion process that JavaScript performs automatically. How the Conversion Works When converting [] to a number, JavaScript follows these steps: First, it calls the valueOf() method on the array, which returns the array itself Since the result isn't a primitive, it calls toString() on the array An empty array's toString() returns an empty string "" Finally, the empty string converts to 0 Using Number() Method ...
Read MoreWhat is the role of pageX Mouse Event in JavaScript?
The pageX property of mouse events returns the horizontal coordinate (X-axis) of the mouse pointer relative to the entire document, including any scrolled content. Unlike clientX, which is relative to the viewport, pageX gives the absolute position within the document. Syntax event.pageX Return Value Returns a number representing the horizontal pixel coordinate relative to the left edge of the entire document. Example Click anywhere on the paragraph to see the mouse coordinates: Click here ...
Read MoreWhat will happen when [50,100] is converted to Number in JavaScript?
Use the Number() method in JavaScript to convert to Number. When an array like [50, 100] is converted to Number in JavaScript, the result might be surprising. What Happens with Array to Number Conversion? When Number() is called on an array, JavaScript first converts the array to a string, then attempts to convert that string to a number. For arrays with multiple elements, this results in NaN (Not a Number). Example Convert [50, 100] to Number ...
Read MoreWhat is the role of screenX Mouse Event in JavaScript?
The screenX mouse event property returns the horizontal coordinate of the mouse pointer relative to the user's screen when an event is triggered. This is useful for tracking precise mouse positions across the entire screen. Syntax event.screenX Return Value Returns a number representing the horizontal distance in pixels from the left edge of the user's screen to the mouse pointer position. Example Click anywhere on the paragraph below to see the mouse coordinates displayed: ...
Read MoreHow [ ] is converted to String in JavaScript?
In JavaScript, an empty array [] converts to an empty string "" when converted to a string. This happens through JavaScript's automatic type conversion or by using explicit conversion methods. Using String() Method The String() method explicitly converts any value to a string: Convert [] to String var myVal = []; document.write("String: " + String(myVal)); document.write("Type: " + typeof String(myVal)); ...
Read MoreHow -Infinity is converted to Boolean in JavaScript?
In JavaScript, -Infinity is considered a truthy value and converts to true when converted to Boolean. This might be surprising since negative infinity seems conceptually "negative, " but JavaScript treats all non-zero numbers (including infinities) as truthy. Using Boolean() Constructor The Boolean() constructor explicitly converts values to their boolean equivalent: Convert -Infinity to Boolean var myVal = -Infinity; document.write("Boolean: ...
Read More