Javascript Articles

Page 294 of 534

How to generate Prime Numbers in JavaScript?

Ayyan
Ayyan
Updated on 15-Mar-2026 3K+ Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can generate prime numbers using different approaches. Basic Prime Check Algorithm The most straightforward method checks if a number is divisible by any number from 2 to itself minus 1: JavaScript Prime Numbers for (var limit = 1; limit 1) { document.write(limit + ""); } } 2 3 5 7 11 13 17 19 Optimized Prime Generator Function For better performance, we only need to check divisors up to the square root of the number: function isPrime(num) { if (num

Read More

Set the font stretch of an element with CSS

Samual Sam
Samual Sam
Updated on 15-Mar-2026 70 Views

The CSS font-stretch property controls the width of characters in a font, allowing you to make text narrower or wider. This property only works if the font family has condensed or expanded variants available. Syntax font-stretch: value; Possible Values The font-stretch property accepts the following values: normal - Default width ultra-condensed - Most narrow extra-condensed - Very narrow condensed - Narrow semi-condensed - Slightly narrow semi-expanded - Slightly wide expanded - Wide extra-expanded - Very wide ultra-expanded - Most wide wider - Relative to parent narrower - Relative to parent ...

Read More

How do I replace a character at a particular index in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 2K+ Views

JavaScript strings are immutable, meaning you can't directly replace a character at a specific index like in other languages. However, we can use several approaches to create a new string with the character replaced at the desired position. In this tutorial, we'll explore two effective methods to replace a character at a particular index in a JavaScript string. Replace Character by Converting String to Array Using String Slicing with substring() Replace Character by Converting String to Array This method converts the string to an array, modifies the character ...

Read More

How to create arrays in JavaScript?

Anjana
Anjana
Updated on 15-Mar-2026 556 Views

JavaScript provides multiple ways to create arrays. Arrays are used to store multiple values in a single variable and can hold different data types. Using Array Literal (Recommended) The most common and recommended way to create arrays is using square brackets: Array Literal Example var animals = ["Dog", "Cat", "Tiger"]; var numbers ...

Read More

Add or subtract space between the letters that make up a word with CSS

Nikitha N
Nikitha N
Updated on 15-Mar-2026 399 Views

To add or subtract space between the letters that make up a word, use the letter-spacing CSS property. This property controls the horizontal spacing between characters in text. Syntax letter-spacing: normal | length | initial | inherit; Property Values normal - Default spacing between letters length - Specific spacing value (px, em, rem, etc.) initial - Sets to default value inherit - Inherits from parent element Example: Adding Letter Spacing ...

Read More

How to access properties of an array of objects in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 5K+ Views

In JavaScript, accessing properties of objects within an array is a common task. Whether you're working with a single object or an array of objects, there are several methods to retrieve property values effectively. JavaScript objects are collections of key-value pairs called properties. When you have an array containing multiple objects, you need specific techniques to access the properties of each object. Let's explore the main approaches. Using Dot Notation Dot notation is the most straightforward way to access object properties when the property name is known and is a valid JavaScript identifier. The syntax is object.propertyName. ...

Read More

How to merge two arrays in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 4K+ Views

In this tutorial, we will learn how to merge two arrays in JavaScript. In JavaScript, we can merge two arrays using different approaches. Here are the three most common methods: Using the Array concat() method Using the spread operator Using loop iteration Using the Array concat() Method The Array concat() method merges two or more arrays and returns a new array. This is one of the most popular methods for merging arrays in JavaScript. The method operates on an array, takes another array as a ...

Read More

How to extend an existing JavaScript array with another array?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 3K+ Views

In this tutorial, let's find out how to extend an existing JavaScript array with another array. There are multiple methods to accomplish this task, and choosing the right one depends on whether you want to modify the original array or create a new one. Using Array push() Method with Spread Syntax The push() method adds elements to the end of an array and modifies the original array. When combined with the spread operator, it can extend an array with all elements from another array. Syntax array1.push(...array2) Example This method modifies the original ...

Read More

How to randomize (shuffle) a JavaScript array?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 2K+ Views

In this tutorial, we will learn different methods to randomize or shuffle a JavaScript array. Shuffling arrays is a common requirement in programming, useful for games, random sampling, or creating unpredictable sequences. Using the Fisher-Yates Algorithm The Fisher-Yates algorithm is the most reliable method for shuffling arrays. It iterates through the array from the last index to the first, swapping each element with a randomly selected element from the remaining unshuffled portion. Syntax for (var i = arr.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + ...

Read More

How to test if an element contains class in JavaScript in HTML?

Abhishek
Abhishek
Updated on 15-Mar-2026 10K+ Views

In JavaScript, we can check whether an HTML element contains a particular class using the classList.contains() method. This is especially useful when working with dynamic content or when you need to conditionally apply styles or behavior based on an element's classes. The classList.contains() method is the standard and most reliable way to test for class existence on DOM elements. Syntax element.classList.contains(className) Parameters: className - A string representing the class name to check for Return Value: Returns true if the element contains the specified class, false otherwise. Example: Basic ...

Read More
Showing 2931–2940 of 5,340 articles
« Prev 1 292 293 294 295 296 534 Next »
Advertisements