

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Array fill() function
The fill() method of JavaScript is used to fill the elements in an array with a static value.
The syntax is as follows −
array.fill(val, start, end)
Above, val is the value to fill the array, start is the index to begin filling the array, whereas end is the index to stop filling the array.
Let us now implement the fill() method in JavaScript −
Example
<!DOCTYPE html> <html> <body> <h2>Ranking Points</h2> <button onclick="display()">Fill</button> <p id="demo"></p> <script> var pointsArr = [50, 100, 200, 300, 400, 500, 600]; document.getElementById("demo").innerHTML = pointsArr; function display() { document.getElementById("demo").innerHTML = pointsArr.fill(1000); } </script> </body> <!DOCTYPE html>
Output
Click “Fill” button to fill the array −
Example
<!DOCTYPE html> <html> <body> <h2>Student Name</h2> <button onclick="display()">Fill</button> <p id="demo"></p> <script> var stdName = ["John", "Tom", "David"]; document.getElementById("demo").innerHTML = stdName; function display() { document.getElementById("demo").innerHTML = stdName.fill("Jacob"); } </script> </body> <!DOCTYPE html>
Output
Click “Fill” to fill the array −
- Related Questions & Answers
- Fill missing numeric values in a JavaScript array
- JavaScript Array find() function
- JavaScript Array findIndex() function
- JavaScript Array some() function
- Compare and fill arrays - JavaScript
- Array findIndex() function in JavaScript
- Array some() function in JavaScript
- Array::fill() and array::swap() in C++ STL?
- Sorting Array with JavaScript reduce function - JavaScript
- How to fill columns with JavaScript?
- divisibleBy() function over array in JavaScript
- How to fill array values in Java?
- Currified function that multiples array elements in JavaScript
- Java Program to fill elements in a float array
- Java Program to fill elements in a short array
Advertisements