Home > Web Front-end > JS Tutorial > body text

Examples of functions with variable number of parameters in JavaScript

PHPz
Release: 2018-09-30 15:10:54
Original
1531 people have browsed it

JavaScript allows a function to pass a variable number of parameters because there is a built-in object called arguments, which is an array of all parameters passed by a function.

Off-topic: I came into contact with JavaScript very early, but I didn’t pay attention to it. I saw many cool and dazzling web pages with JavaScript in them. Google’s application of JavaScript had the greatest impact on me. I was determined to learn it from scratch, hence the JavaScript & Ajax section. I plan to record this column as study notes, so the notes for each article may be very short, just one or two sentences of annotations.

JavaScript allows a function to pass a variable number of parameters because there is a built-in object called arguments, which is an array of all parameters passed by a function. Give an example and you will understand.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript参数个数可变的函数</title>
<mce:script language="javascript" type="text/javascript"><!--
function testparams()
{
var params = "";
for (var i=0; i<arguments.length; i++) {
params = params + " " + arguments[i];
}
alert(params);
}

testparams("abc", 123);
testparams(123, "456", 789);
testparams();
// --></mce:script>
</head>
<body>
</body>
</html>
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

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