Home > Web Front-end > JS Tutorial > How to use fill method

How to use fill method

不言
Release: 2021-04-20 10:39:01
Original
15624 people have browsed it

The fill method can be used to fill the entire array or a part of the array. The syntax of the fill method is "arr.fill(value,begin,end)", where the parameter arr is to be filled with a static value. array.

How to use fill method

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.

The fill() method is a method of filling elements in an array with static values. It can be used to fill the entire array or a part of the array. In this article, we will introduce to you the usage of the fill method. .

Basic syntax of fill method

arr.fill(value,begin,end)
Copy after login

arr is the array to be filled with static values.

value defines the static value to replace the array element

begin defines the starting index to fill the array with the static value. If this value is not defined, the default starting index is 0.

end defines the last index of the array to be filled with a static value. If this value is not defined, by default the last index is used as the ending value.

Let’s look at specific examples

The code is as follows

<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta charset="utf-8">
   </head>
   <body> 
<script type="text/javascript"> 
   var array = [ 1, 2, 3, 4 ]; 
  console.log(array.fill(0, 2, 4));
   var array = [ 1, 2, 3, 4 ]; 
  console.log(array.fill(9, 2)); 
  var array = [ 1, 2, 3, 4 ]; 
  console.log(array.fill(6)); 
</script> 
   </body>
</html>
Copy after login

The running results are as follows

How to use fill method

This article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use fill method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template