We'll take a look at 3 ways of defining functions in JavaScript →
function foo(arg1, arg2) {
return arg1 + arg2;
}
const foo = function(arg1, arg2) {
return arg1 + arg2;
}
(arg1, arg2) => { return arg1 + arg2 }
Function declaration syntax:
function foo(arg1, arg2) {
return arg1 + arg2;
}
function
(note that return type is not specified)undefined
We'll see later that function declarations are special in that they can used before they are declared in your code!
Another way to create a function is by using a function expression (remember, functions are first-class citizens): →
const doubleTheNumber = function(n) {
return n + n;
};
console.log(doubleTheNumber(5));
function
doubleTheNumber
, can be called/invoked because it's a function!Function expressions, as the name implies, are expressions! →
// to initialize a variable
const doubleTheNumber = function(n) { return n + n; };
// as an argument
const numbers = [1, 2, 3, 4];
numbers.map(function(n) { return n + n; });
// as a return value
function f() {
return function(n) {
return n + n;
});
}
Introduced in ES6, arrow functions are a way of writing function expressions in a very concise syntax. →
(arg1, arg2) => { /* body goes here */}
arguments
object (we'll see this later)this
is the value of this
where it was defined (we'll talk about this
later as well)There are a few ways to write arrow functions. →
(p1, p2, ..., pN) => { statements }
(p1, p2, ..., pN) => expression // same as { return expression; }
singleParam => { statements }
() => { statements }
Let's create a function called myPow
. It will (surprisingly) raise some number to a power: →
base
and exponent
base
raised to the exponent
const myPow = function(base, exponent) {
let result = 1;
for(var i = 0; i < exponent; i++) {
result = result * base;
}
return result;
};
console.log(myPow(2,0));
console.log(myPow(2,1));
console.log(myPow(2,8));
That line was a bit verbose, wasn't it?
result = result * base;
We can tighten that up using another assignment operator.
var x = 3;
x *= 2;
console.log(x);
// this works with +=, -= and /= as well
This is when things start to get interesting…
Again, scope is the area or portion of your program where a variable name or identifier is available.
(but with ES6, let
and const
give you block level scope!)
Variables declared at the "top level" of your program (outside of functions) and variables declared without const
, let
or var
(in most cases) are in the global scope.
var
are local to the functionconst
or let
are local to the block that they're declared inlet
, const
or var
affect the global scope… ⊙﹏⊙
Based on the previous slide, what is the output of the following code? →
let x = "hi!"; // hello... I'm a global variable
const f = function() {
let x = "from f";
};
const g = function() {
x = "from g";
};
console.log(x)
f();
console.log(x);
g();
console.log(x);
hi!
hi!
from g
let
or const
(or var
) when declaring variables plzThe world is really a flat plate supported on the back of a giant tortoise;
(Let's look at nested functions)
Variables declared without const
, let
, or var
actually mask the variable in nearest enclosing scope (if it's not a const
) (usually global, but a bit tricky for nested functions).
let x = 1;
function f() {
let x = 2;
function g() {
x = 3;
console.log(x);
}
g(x);
console.log(x);
}
f();
console.log(x);
3
3
1
What is the output of this code? What would happen if we put console.log(y)
right at the end of function, outer
's body? →
const outer = function() {
let x = "outside";
let inner = function() {
x += " modified by inside";
let y = "inner";
};
console.log(x);
inner();
console.log(x);
};
outer();
outside
outside modified by inside
// we would get a ReferenceError if we tried to print out y from the outer function
const sayHello = function() {
console.log("Hola!");
};
sayHello();
sayHello = function(x) {
return x * x
};
console.log(sayHello(5));
You can even pass functions around, return them as values, etc.
const callTwice = function(f) {
f();
f();
};
const g = function() {
console.log("nobody's home!");
};
callTwice(g);
nobody's home!
nobody's home!
Let's take a look at function declarations again →
function f(x) {
return x;
}
However, functions defined in this manner, through function declarations, are hoisted to the top of the scope:
console.log(f(5));
function f(x) {
return x;
}
From Speaking JavaScript:
What happens when you put such a function declaration inside an if statement or a loop?
An Array literal, assignment and the length property
const numbers = [1, 2, 3];
console.log(numbers[0]);
console.log(numbers.length);
// an empty array ... []
Write a function that: →
Array
Array
will only 0 or more values that are of type, Number
→Array
Array
is empty, just return undefined