Javascript Articles

Page 275 of 534

What is the usage of onunload event in JavaScript?

Sai Nath
Sai Nath
Updated on 15-Mar-2026 422 Views

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 More

What is nodeValue property in JavaScript HTML DOM?

Vikyath Ram
Vikyath Ram
Updated on 15-Mar-2026 349 Views

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 More

Which event occurs in JavaScript when an element is content is copied to clipboard?

Nitya Raut
Nitya Raut
Updated on 15-Mar-2026 195 Views

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 More

What is the role of altKey Mouse Event in JavaScript?

Sravani S
Sravani S
Updated on 15-Mar-2026 278 Views

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 More

How [ ] is converted to Number in JavaScript?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 15-Mar-2026 265 Views

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 More

What is the role of pageX Mouse Event in JavaScript?

Krantik Chavan
Krantik Chavan
Updated on 15-Mar-2026 431 Views

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 More

What will happen when [50,100] is converted to Number in JavaScript?

Sai Subramanyam
Sai Subramanyam
Updated on 15-Mar-2026 174 Views

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 More

What is the role of screenX Mouse Event in JavaScript?

Anjana
Anjana
Updated on 15-Mar-2026 227 Views

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 More

How [ ] is converted to String in JavaScript?

Swarali Sree
Swarali Sree
Updated on 15-Mar-2026 224 Views

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 More

How -Infinity is converted to Boolean in JavaScript?

Jai Janardhan
Jai Janardhan
Updated on 15-Mar-2026 251 Views

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
Showing 2741–2750 of 5,340 articles
« Prev 1 273 274 275 276 277 534 Next »
Advertisements