
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- JavaScript Array find() function
- JavaScript Array findIndex() function
- JavaScript Array some() function
- Fill missing numeric values in a JavaScript array
- Array findIndex() function in JavaScript
- Array some() function in JavaScript
- Sorting Array with JavaScript reduce function - JavaScript
- divisibleBy() function over array in JavaScript
- Currified function that multiples array elements in JavaScript
- Array::fill() and array::swap() in C++ STL?
- Which algorithm does the JavaScript Array#sort() function use?
- Accessing an array returned by a function in JavaScript
- Compare and fill arrays - JavaScript
- How to fill columns with JavaScript?
- How to fill array values in Java?

Advertisements