Lokesh Yadav

Lokesh Yadav

38 Articles Published

Articles by Lokesh Yadav

Page 3 of 4

Execute digits in even places in a JavaScript array?

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

In JavaScript, extracting elements from even positions in an array requires understanding that array indexing starts from 0. Elements at even positions are at indices 0, 2, 4, etc., while elements at odd positions are at indices 1, 3, 5, etc. To extract elements from even positions, we can use various methods like filter(), traditional loops, or specific iteration patterns. Understanding Array Positions JavaScript arrays are zero-indexed, meaning: Even positions: indices 0, 2, 4, 6... Odd positions: indices 1, 3, 5, 7... Using the filter() Method The filter() method creates a new array ...

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

Number of elements a particular tag contains in JavaScript?

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

In this article we are going to learn about counting the number of child elements a particular tag contains in JavaScript with suitable examples. There are two main approaches to count child elements in JavaScript. You can use the childElementCount property to count direct child elements, or use the length property with methods like getElementsByClassName() to count elements by class name. Let's explore both methods with practical examples to understand how to count elements effectively. Using the childElementCount Property The childElementCount property returns the number of direct child elements within a specific parent element. It only ...

Read More

Replace a child node with a new node in JavaScript?

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

In this article we are going to learn how to replace a child node with a new node in JavaScript with suitable examples. To replace a child node with a new node, JavaScript provides two built-in DOM methods: replaceChild() and replaceWith(). Both methods allow you to swap an existing node with a new one in the DOM tree. The methods replaceChild() and replaceWith() are supported by all modern browsers and are features of the DOM specification. replaceChild() Method The replaceChild() method is called on the parent element to replace one of its child nodes. Syntax ...

Read More

How to convert a value to a number in JavaScript?

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

JavaScript provides several methods to convert values to numbers. Each method handles different types of input and has specific use cases. Let's explore the four main approaches: Number(), parseInt(), parseFloat(), and the unary operator (+). Using Number() Method The Number() method converts any value to a number. It's the most comprehensive conversion method and handles various data types. Convert value to number using Number() Number() Method Examples ...

Read More

How to check whether a number is finite or not in JavaScript?

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

In JavaScript, the isFinite() method checks whether a value is a finite number. It returns true for finite numbers and false for infinite values, NaN, or non-numeric values. Syntax isFinite(value) Parameters: value - The value to be tested for finiteness Return Value: Returns true if the value is finite, false otherwise. Example 1: Basic Usage Here's a function that demonstrates basic usage of isFinite(): Check Finite Numbers Checking division results: ...

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 566 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

How to convert a JSON string into a JavaScript object?

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

JSON (JavaScript Object Notation) is a lightweight data format commonly used for data exchange between servers and web applications. When receiving data from a server, it typically arrives as a JSON string that needs to be converted into a JavaScript object to be usable in your code. Why JSON.parse() Over eval() There are two possible ways to convert a JSON string into a JavaScript object: eval() and JSON.parse(). However, eval() is unsafe and vulnerable to code injection attacks, making it unsuitable for parsing JSON data. JSON.parse() is the recommended and secure method. Syntax JSON.parse(text[, reviver]) ...

Read More

How to convert JavaScript objects to primitive data types manually?

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

In JavaScript, objects can be converted to primitive data types (string, number, boolean) using built-in methods. This conversion is essential for operations that require primitive values rather than object references. JavaScript provides several methods for manual conversion including toString(), toDateString(), and valueOf(). Each method serves different conversion purposes and returns specific primitive types. Using the toString() Method The toString() method converts any JavaScript object to its string representation. This method is available on all objects and returns a string primitive. Syntax object.toString() Example Converting various object types to strings: ...

Read More

An element inside another element in JavaScript?

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

In JavaScript, accessing elements nested inside other elements is a common task when working with the DOM. We can access inner elements using various methods and check containment relationships using the contains() method. JavaScript provides several ways to access nested elements. The most common approach uses getElementsByTagName() on a parent element, while contains() helps verify parent-child relationships. Accessing Nested Elements To access an element inside another element, we first get the parent element, then search within it: document.getElementById('parentID').getElementsByTagName('tagName')[index] document.getElementById('parentID').getElementsByClassName('className')[index] Method 1: Using getElementsByTagName() This example shows how to access and modify elements ...

Read More
Showing 21–30 of 38 articles
Advertisements