CodeCademy Javascript notes Flashcards
Which function returns a random number between 0 and 1?
Math.random();
Which function takes a decimal number and rounds it down to the nearest whole number?
Math.floor()
How to introduce a single line comment in Javascript?
By using two forward slashes
//
How to introduce a multiline comment in Javascript?
A multi-line comment will comment out multiple lines, and is denoted with /* to begin the comment, and */ to end the comment:
/* here goes the multilines blabla */
What is string interpolation?
It’s when you insert a variable into a string.
You do so using the ‘+’
Which logical operator do we use to say both must be true?
&&
(= and)
Which logical operator do we use to say either can be true?
||
(two pipes = or)
Which logical operator do we use to say I want to make sure this is the opposite of what it really is?
!
(=not)
Which logical operator do we use to say these should not be equal to each other?
!==
(not equal value or not equal type)
Using else if is a great tool for when we have a few different conditions we’d like to consider.
else if is limited however. If we want to write a program with 25 different conditions, like a JavaScript cash register, we’d have to write a lot of code, and it can be difficult to read and understand.
To deal with times when you need many else if conditions, we can turn to a […] statement to write more concise and readable code.
switch statement
The switch keyword initiates the statement, and is followed by ( … ), which contains the condition that each case will compare to. In the example, the condition is moonPhase.
Inside the block, { … }, there are cases. case is like the else if part of an if/else if/else statement. The word following the first case is ‘full’. If moonPhase equalled ‘full’, that case’s console.log would run.
moonPhase equals ‘full’, so the second and third case statements are skipped. The first case runs since the case is ‘full’ matches moonPhase’s value. This particular program will log out: ‘Howwwwlll’.
Then the program stops with the break keyword. This keyword will prevent the switch statement from executing any more of its code.
At the end of each switch statement, there is a default condition. If none of the cases are true, then this code will run.
How to make a string interpolation/substitution in ES6?
using the ${ } syntax
Warning: CoffeeScript uses the #{ } syntax
What is the difference between parameter and argument?
- inputNumber* is a parameter, but when we call multiplyByThirteen(9), the 9 is called an argument. Therefore, arguments are provided when you call a function, and parameters receive arguments as their value. So, inputNumber is a parameter and its value is the argument 9, since we wrote multiplyByThirteen(9).
- *Parameters** let us write logic inside functions that can be modified based on when we call the function, which will help make our functions more flexible.
Which verb do we use to mean that we’re giving arguments to a function?
“pass” / “pass in”
Passing in arguments to a function
What should we put inside the takeOrder function to output
“Order: thin crust pizza topped with bacon”
function takeOrder(topping, crustType) { console.log(**...**); }
takeOrder(‘bacon’, ‘thin crust’);
What is the difference between Global Scope and Functional Scope?
Scope is the idea in programming that some variables are acessible/inaccessible from other parts of the program.
Global Scope refers to variables that are accessible to every part of the program.
Functional Scope refers to variables created inside functions, which are not accessible outside of its block.