Front End Technology Articles

Page 339 of 652

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

How to append an item into a JavaScript array?

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

To append an item to a JavaScript array, you can use several methods. The most common approach is the push() method, which adds elements to the end of an array. Using push() Method The push() method adds one or more elements to the end of an array and returns the new length: var arr = ["marketing", "technical", "finance", "sales"]; arr.push("HR"); ...

Read More

How to create a zero-filled JavaScript array?

Ramu Prasad
Ramu Prasad
Updated on 15-Mar-2026 206 Views

To create a zero-filled JavaScript array, you have several methods available. The most common approaches include using Array.fill(), Array.from(), or typed arrays like Uint8Array. Using Array.fill() Method The Array.fill() method is the most straightforward way to create a zero-filled array: // Create array of length 5 filled with zeros var zeroArray = new Array(5).fill(0); document.write("Zero-filled array: " ...

Read More

How to convert an array into JavaScript string?

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

An array is the most popular and adaptable data structure you might employ in your regular programming. Converting an array into a JavaScript String can be done using several predefined methods and techniques. Using the toString() Method Using the join() Method Using JSON.stringify() Method Using Type Coercion Using the toString() Method The toString() method is a built-in JavaScript function that converts array elements into a comma-separated string. It's the simplest way to convert an array to string format. ...

Read More

How to add an element in the last of a JavaScript array?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 6K+ Views

In this tutorial, we will learn how to add an element to the last of a JavaScript array. The most common method to add an element to the end of the array is by using the Array push() method, but we will discuss multiple methods to achieve this. Using the Array.prototype.push() Method Using the Array.prototype.splice() Method Using Array Indexing Using the Array.prototype.push() Method To add an element at the end, use the JavaScript push() method. The push() method appends the given element(s) to the end of the array and ...

Read More

How to serialize a JavaScript array?

Prabhdeep Singh
Prabhdeep Singh
Updated on 15-Mar-2026 3K+ Views

In JavaScript, serializing an array means converting it into a string format that can be stored, transmitted, or reconstructed later. While jQuery's serializeArray() method is useful for form data, JavaScript provides native methods for general array serialization. What is Array Serialization? Array serialization converts a JavaScript array into a string representation. The most common approach uses JSON.stringify() to convert arrays into JSON strings, which can be easily transmitted to servers or stored in local storage. Using JSON.stringify() (Recommended) The JSON.stringify() method is the standard way to serialize JavaScript arrays: // Simple array serialization let ...

Read More

How to extract the number of minutes from current time in JavaScript?

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

JavaScript uses timestamps based on Unix time (milliseconds since January 1, 1970 UTC) to handle dates and times. While humans understand time in hours and minutes, JavaScript provides built-in methods to extract specific time components from Date objects. In this tutorial, we will learn how to extract the number of minutes from the current time in JavaScript using the getMinutes() method. Date Object Methods The JavaScript Date object provides several methods to extract time components: getHours() − Returns the current hour (0-23) in 24-hour format getMinutes() − Returns the current minutes (0-59) getSeconds() − Returns ...

Read More

How to add 30 minutes to a JavaScript Date object?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 12K+ Views

In JavaScript, there are multiple ways to add 30 minutes to a Date object. This tutorial covers two effective methods: using the setMinutes() method and using the getTime() method. Using the setMinutes() Method The setMinutes() method sets the minutes for a date object and automatically handles overflow to the next hour or day. Syntax Date.setMinutes(min, sec, ms); Parameters min − An integer between 0 and 59, representing the minutes. sec − An integer between 0 and 59, representing the seconds. Optional. ms ...

Read More

How to clone a Date object in JavaScript?

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

In this tutorial, we will learn to clone a Date object in JavaScript. We can create a new object of the Date class that contains the date and time according to the user's requirement. Sometimes, it needs to copy the date, which means cloning it. In such cases, users can use the different approaches given below. Also, we will learn to set the new time and date in the cloned date in this tutorial. Using the Date.getTime() Method Users can simply create the new object of the Date class and get the total number of milliseconds from ...

Read More
Showing 3381–3390 of 6,519 articles
« Prev 1 337 338 339 340 341 652 Next »
Advertisements