Web Development Articles

Page 406 of 801

How to get the first n values of an array in JavaScript?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 1K+ Views

In JavaScript, getting the first n elements from an array is a common operation. There are several built-in methods to accomplish this efficiently. First n values means extracting elements from index 0 to index n-1. For example, if n=3, you get elements at positions 0, 1, and 2. Using the slice() Method (Recommended) The slice() method is the most common and efficient way to get the first n elements. It creates a new array without modifying the original. Syntax array.slice(0, n) Example 1: Basic Usage ...

Read More

How to invert an object in JavaScript?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 3K+ Views

Inverting an object in JavaScript means swapping its keys and values, so that {name: "John", age: 25} becomes {"John": "name", "25": "age"}. This is useful for creating lookup tables or reverse mappings. There are several ways to invert objects in JavaScript, from using libraries like Underscore.js to native JavaScript methods. Using Underscore.js _.invert() The _.invert() function from Underscore.js provides a simple way to swap object keys and values. Syntax _.invert(object) Parameters: object − The object to invert Return Value: A new object with keys and values ...

Read More

What is the relation between 'null' and '0' in JavaScript?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 4K+ Views

In JavaScript, the relationship between null and 0 creates a seemingly contradictory behavior that confuses many developers. While null is neither greater than nor equal to 0 in equality checks, it evaluates as greater than or equal to 0 in comparison operations. Understanding null and 0 null is a primitive value representing an intentional absence of value, while 0 is a number. The confusing behavior occurs due to JavaScript's type coercion rules, which differ between equality (==) and comparison (>, =,

Read More

How to remove the last digit of a number and execute the remaining digits in JavaScript?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 5K+ Views

In JavaScript, you can remove the last digit from a number using different approaches. The most common methods involve string manipulation or mathematical operations like bitwise operations. When working with numbers, you typically need to convert the number to a string to use string methods, or use mathematical operations to avoid type conversion altogether. Method 1: Using String substring() The substring() method extracts characters from a string between two specified indices. Syntax str.substring(startIndex, endIndex) To remove the last digit, use str.substring(0, str.length - 1) where the first parameter is the starting index ...

Read More

What is the importance of '+' operator in type conversion in JavaScript?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 275 Views

JavaScript is a dynamically typed language where operations often automatically convert values to the appropriate type. The + operator plays a crucial role in type conversion, behaving differently depending on the operand types. JavaScript provides many ways to convert data from one form to another, with two most common types of conversion: Converting values to strings Converting values to numbers The '+' Operator's Dual Behavior The + operator in JavaScript serves two purposes: Addition: When both operands are numbers String ...

Read More

How to find the inner height and inner width of a browser window in JavaScript?

Lokesh Yadav
Lokesh Yadav
Updated on 15-Mar-2026 701 Views

In this article we are going to learn how to find the inner height and inner width of a browser window using JavaScript. JavaScript defines window object methods and properties. The properties include inner height and inner width, which helps us to calculate the height and width of a window's content area excluding scrollbars, toolbars, and other browser UI elements. There are two main approaches to calculate the inner height and inner width of a window: Using innerHeight and innerWidth properties − The window.innerHeight and window.innerWidth properties return the viewport dimensions including padding ...

Read More

How to know the browser language and browser platform in JavaScript?

Lokesh Yadav
Lokesh Yadav
Updated on 15-Mar-2026 780 Views

In this article we are going to discuss how to know the browser language and browser platform with the help of examples in JavaScript To know the various properties of the browser, JavaScript provides the Navigator object. The Navigator object has several properties, which include - connection, credentials, cookies, geolocation, language, platform, etc. We are concerned with Navigator.language and Navigator.platform to know the language and platform of the browser. Let us discuss about them in detail. Navigator.language The read-only Navigator.language property returns a string that represents the browser UI language. It returns a string that contains the ...

Read More

How to get a particular anchor in a document in JavaScript?

Lokesh Yadav
Lokesh Yadav
Updated on 15-Mar-2026 2K+ Views

In this article we will learn how to get a particular anchor in a document in JavaScript. JavaScript provides multiple ways to access anchor elements ( tags) in a document. Anchor elements follow an array-like structure, allowing you to select specific anchors by index or use DOM methods to retrieve them. There are two primary methods to get a particular anchor element: using document.anchors collection or document.getElementsByTagName("a") method. Syntax The syntax to get a particular anchor tag is shown below: // Method 1: Using document.anchors collection document.anchors[index].innerHTML // Method 2: Using getElementsByTagName document.getElementsByTagName("a")[index].innerHTML ...

Read More

How to find the number of links in a document in JavaScript?

Lokesh Yadav
Lokesh Yadav
Updated on 15-Mar-2026 1K+ Views

The document.links property in JavaScript provides access to all links in a document. It returns a collection of and elements that contain an href attribute. Syntax To get the total number of links in a document, use: document.links.length The document.links property returns an HTMLCollection that behaves like an array, allowing you to access individual links by index and get the total count using the length property. Example 1: Basic Link Count Here's a simple example to count the links in a document: JavaScript ...

Read More

How to display the domain of the server that loaded a document in JavaScript?

Lokesh Yadav
Lokesh Yadav
Updated on 15-Mar-2026 589 Views

In this following article we are going to learn how to display the domain of the server that loaded a document in JavaScript. To display the domain of the server, we use domain property of the document interface, which returns the domain part of the current document. The domain property will return NULL if the document was created in memory. Attempting to set the domain property of the document interface results in SecurityError. To understand better, let's look into the syntax and usage of domain property with suitable examples in JavaScript. Using document.domain Property We can ...

Read More
Showing 4051–4060 of 8,010 articles
« Prev 1 404 405 406 407 408 801 Next »
Advertisements