JavaScript: function Operator
Description
The function operator defines a function inside an expression.
Version
Implemented in JavaScript 1.5
Syntax
var1 = function( parameter1, parameter2,......parametern) { statement(s) }
Parameters
parameters: A list of arguments to the function.
statement(s): JavaScript statements.
Example:
The following web document demonstrates the use of function operator. It sets x to a function that returns the square of two numbers.
HTML Code
<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript function operator example.</title>
;meta name="description" content="This document contains an example of JavaScript function operator "/>
</head>
<script src="javascript-function-operator-example1.js"></script>
</body>
</html>
JS Code
var a = 12;
var b = 14;
var x = function(a,b)
{
return a*a+2*a*b+b*b;
};
console.log("The square of "+a+ " and "+b +" is " +x(a,b));
View the example in the browser
Practice the example online
See the Pen function-1 by w3resource (@w3resource) on CodePen.
See also
Conditional Operator
comma
delete
in
instanceof
new
this
typeof
void
Previous: JavaScript: delete Operator
Next: JavaScript: in Operator
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
How to insert an item into an array at a specific index (JavaScript)?
What you want is the splice function on the native array object.
arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join());
Ref: https://bit.ly/2BXbp04
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework