Found 6710 Articles for Javascript

How to remove last array element in JavaScript and return it?

Shubham Vora
Updated on 26-Aug-2022 14:17:28

15K+ Views

In this tutorial, we will learn how to remove the last array element in JavaScript and return it. In JavaScript, arrays are described as list-like objects. A single array object can store multiple values. An array object is stored in a variable. Array elements are stored in a memory location. Each array element is identified by its index value. You must process an array by inserting an element, removing or changing, etc. JavaScript has a lot of methods to update an array. Let us look at various ways to remove the last array element in JavaScript. Following are the Methods/Functions ... Read More

How to reverse the order of a JavaScript array?

Abhinanda Shri
Updated on 18-Jun-2020 08:18:32

310 Views

To reverse the order of a JavaScript array, use the JavaScript array() method. JavaScript array reverse() method reverses the elements of an array. The first array element becomes the last and the last becomes the first.ExampleYou can try to run the following code to reverse the order of a JavaScript array −           JavaScript Array reverse Method                        var arr = c.reverse();          document.write("Reversed array is : " + arr );          

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

Saurabh Jaiswal
Updated on 22-Aug-2022 07:51:19

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 last of the array is by using the Array push() method but we will discuss multiple methods by which we can achieve this. Here are some approaches to achieve this. Using the Array.prototype.push( ) Method Using the Array.prototype.splice( ) Method Using the Array Indexing Using the Array.prototype.push() Method To add an element in the last, use the JavaScript push() method. JavaScript array push() method appends the given element(s) in the ... Read More

How to convert an array into JavaScript string?

Shubham Vora
Updated on 22-Aug-2022 08:43:41

8K+ Views

An array is the most popular and adaptable data structure you might employ in your regular programming. Predefined methods and a few more techniques make converting an array into a string simple. The process of converting an array into a JavaScript String can be done in several methods, and in this article, we will be looking at them. Using the toString() Method Using the join() Method Using JSON.stringify() Method Using the Type Coercion Using the toString() Method There is an internal function toString(). It is a built-in JavaScript method that can be applied to any JavaScript variable. The ... Read More

How to find the length of an array in JavaScript?

Abhishek
Updated on 31-Oct-2022 07:58:20

14K+ Views

In this tutorial, we will learn about the method of finding the length of an array in JavaScript. The length of an array is the number of elements or items contained by a JavaScript array. In JavaScript, we have only one way or method to find out the length of an array. Using the length property Using the length property In JavaScript, we are allowed to use the length property of JavaScript for finding the length of an array. As we know the length of an array is the number of elements contained by the array, the length ... Read More

How to create a zero-filled JavaScript array?

Ramu Prasad
Updated on 13-Jan-2020 10:28:00

128 Views

To create a zero-filled JavaScript array, use the Unit8Array typed array.ExampleYou can try to run the following code:Live Demo                    var arr1 = ["marketing", "technical", "finance", "sales"];          var arr2 = new Uint8Array(4);          document.write(arr1);          document.write("Zero filled array: "+arr2);          

How to append an item into a JavaScript array?

Sravani S
Updated on 13-Jan-2020 10:22:22

246 Views

To append an item to a JavaScript array, use the push() method.ExampleYou can try to run the following code to append an item:Live Demo                    var arr = ["marketing", "technical", "finance", "sales"];          arr.push("HR");          document.write(arr);           Outputmarketing,technical,finance,sales,HR

How to define integer constants in JavaScript?

V Jyothi
Updated on 13-Jan-2020 10:18:44

1K+ Views

ECMAScript allows usage of const to define constants in JavaScript. To define integer constants in JavaScript, use the const,const MY_VAL = 5; // This will throw an error MY_VAL = 10;As shown above, MY_VAL is a constant and value 5 is assigned. On assigning, another value to a constant variable shows an error.Using const will not allow you to reassign any value to MY_VAL again. If you will assign a new value to a constant, then it would lead to an error.

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

Abhishek
Updated on 25-Nov-2022 07:50:49

10K+ Views

In JavaScript, we can check for particular class whether it is contained by an HTML element or not, in the HTML document. During the development of the web page, a developer uses many classes and sometimes assign similar classes to different elements which requires same styles in CSS. In this process, the developer may forget the class given to the particular element, then the developer may need check for the class inside the element using the JavaScript. In this tutorial, we are going to discuss how we can test if an element contains class in JavaScript. To check for the ... Read More

How to randomize (shuffle) a JavaScript array?

Shubham Vora
Updated on 06-Sep-2022 14:00:53

2K+ Views

In this tutorial, we will learn the methods to randomize or shuffle a JavaScript array. We can achieve this by using existing shuffle functions in some libraries or algorithms. Let’s move forward to discuss this. Using the Fisher-Yates Algorithm and Destructuring Assignment Here, the algorithm iterates through the array from the last index to the first index. In each looping, it swaps the array values and creates a random permutation of a finite sequence. We can follow the syntax below for using this algorithm. Syntax for (var i=arr.length – 1;i>0;i--) { var j = Math.floor(Math.random() * (i ... Read More

Advertisements