function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}
function
keyword, followed by a name, followed by parentheses: ()
(parameter1, parameter2, ...)
{}
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
return
statement, the function will stop executingreturn
value. The return value is “returned” back to the “caller”:let x = myFunction(4, 3); // Function is called, return value will end up in x
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
The result in x
will be 12
()
will return the function object instead of the function resultfunction toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius;
let text = "The temperature is " + toCelsius(77) + " Celsius";
// code here can NOT use carName
function myFunction() {
let carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
=
assigns a value to a variable:let x = 10;
+
adds numbers:let x = 5;
let y = 2;
let z = x + y;
*
multiplies numbers:let x = 5;
let y = 2;
let z = x * y;
There are different types of JavaScript operators:
+
Addition
-
Subtraction
*
Multiplication
**
Exponentiation (ES2016)
/
Division
%
Modulus (Division Remainder)
++
Increment
--
Decrement
Operator Example Same As
=
x = y x = y
+=
x += y x = x + y
-=
x -= y x = x - y
*=
x *= y x = x * y
/=
x /= y x = x / y
%=
x %= y x = x % y
**=
x **= y x = x ** y
Operator Description
==
equal to
===
equal value and equal type
!=
not equal
!==
not equal value or not equal type
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
?
ternary operator
Operator Description
&&
logical and
||
logical or
!
logical not