Functions give programs complex, reusable units of behavior. Just like variables, they have to be declared. To do this, we use the function
keyword. Here's an example of a function declaration:
function sayHello() { console.log('hello'); }
Let's make another function:
function sayGoodbye() { console.log('goodbye'); }
Now we have two functions named sayHello
and sayGoodbye
. We can invoke a function by writing its name (or the name of a variable that points to it) followed by parentheses. If the function takes any arguments, we put these inside the parentheses.
sayHello(); // prints 'hello' sayGoodbye(); // prints 'hello'
printDriverHeight
that logs the driver's height when invoked.printNavigatorName
that prints the navigator's name.The functions your wrote above are neat, but they can only log the values you hard-coded into them. We can expand the behavior of functions with parameters:
function howTall(height) { console.log('I am ' + height + ' tall'); } howTall('5 feet'); // prints 'I am 5 feet tall'
In the example above, the function howTall
accepts a parameter called height
, and its behavior changes depending on this input. In the following challenges, you'll write functions with parameters.
printName
.name
and print that name.printName
with the driver's name as an argument.printName
with the navigator's name as an argument.printGroupName
.driverName
and navigatorName
.Usually, in addition to side effects like printing things, we want our functions to return a value so that it can be used elsewhere. We can store this return value in a variable:
function timesTwo(age) { return age * 2; } let myAge = 26; let twiceMyAge = timesTwo(myAge); // twiceMyAge === 52
If we don't use the return
keyword when defining our function, it will return undefined
.
function timesTwo(age) { age * 2; } let myAge = 26; let twiceMyAge = timesTwo(myAge); // twiceMyAge === undefined
In the challenges below, you will write functions with return
statements.
multiplyHeight
.driverHeight
and navigatorHeight
.square.
.number
as a parameter.getArea
.radius
as a parameter.square
to calculate the square of the radius.The logical operators &&
(and), ||
(or), and !
(not) help you establish powerful control flow.
A && B
evaluates to true
only if A and B are both trueA || B
evaluates to true
if one or both the items are true!
inverts a true
to a false
, or a false
to a true
Here are some examples:
true && true // true true && false // false false && true // false false && false // false true || true // true false || true // true true || false // true false || false // false !true // false !false // true !!true // true let isYoung = true; let isTall = false; isYoung && isTall // false isYoung && !isTall // true
When we compare two values using a comparison operator, the result is a Boolean value. JavaScript's comparison operators include the following:
==
(equals)===
(strictly equals, in data type and value)!=
(does not equal)!==
(strictly does not equal)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)Here are some examples:
6 == 5 // false 6 === 5 // false 6 != 5 // true '5' == 5 // true '5' === 5 // false '5' != 5 // false '5' !== 5 // true 6 > 5 // true !(6 > 5) // false 6 >= 5 // true 6 >= 6 // true 6 > 6 // false 7 < 8 // true 7 <= 7 // true
We can use a combination of Boolean expressions and control flow statements to control the flow of the program and create "if this, then that" functionality.
Here are some examples:
if (someCondition) { // code that will run if someCondition evaluates to true } if (someCondition) { // code that will run if someCondition evaluates to true } else { // code that will run if someCondition evaluates to false } if (condition1) { // code that will run if condition1 evaluates to true } else if (condition2) { // code that will run if condition1 evaluates to false and // condition2 evaluates to true } if (condition1) { // code that will run if condition1 evaluates to true } else if (condition2) { // code that will run if condition1 evaluates to false and // condition2 evaluates to true } else { // code that will run if both condition1 and condition2 // evaluate to false }
Another type of control flow statement is a loop. We can use a loop to do things repeatedly. A for
loop typically uses a variable called an iterator to do something a certain number of times.
Here is an example:
for (let i = 0; i < 5; i++) { // code that will run 5 times }
A for
loop typically consists of three statements:
++
operator increases the value of i by 1 at the end of each loop).for (let i = 0; i < 5; i++) { console.log(i); } // This would print 0, 1, 2, 3, 4
greeting
that accepts a string name
and a boolean casual
.name
if casual
is true or 'Hello ' + name
if casual
is false.true
.false
.isCodesmithOpen
that accepts a time
parameter (a number).time
is greater than 9, log 'Codesmith is open.'smallMediumOrLarge
that will accept a number howHungry
, which will represent how hungry you are on a scale of 1 to 5.howHungry
is 5, log 'Large please!'howHungry
is 3 or 4, log 'Medium please!'isAGoodBoy
that takes two parameters that we expect to be Boolean values: wellBehaved
and isCute
.wellBehaved
and isCute
, log 'pet'.shouldEatBanana
that accepts color
and smellsGood
parameters.color
is 'yellow' and if smellsGood
is truecolor
is 'green', log 'wait'.countTo5
that uses a for
loop to log the numbers 1 through 5.countMost
that uses a for
loop to loop over the numbers 1 to 5.