Create a Back-End App with JavaScript Flashcards
JavaScript Conditionals and Functions
Conditionals
Ternary Operator
The ternary operator allows for a compact syntax in the case of binary (choosing between two choices) decisions. It accepts a condition followed by a ? operator, and then two expressions separated by a :. If the condition evaluates to truthy, the first expression is executed, otherwise, the second expression is executed.
JavaScript Conditionals and Functions
Conditionals
else Statement
An else block can be added to an if block or series of if-else if blocks. The else block will be executed only if the if condition fails.
JavaScript Conditionals and Functions
Conditionals
switch Statement
The switch statements provide a means of checking an expression against multiple case clauses. If a case matches, the code inside that clause is executed.
The case clause should finish with a break keyword. If no case matches but a default clause is included, the code inside default will be executed.
Note: If break is omitted from the block of a case, the switch statement will continue to check against case values until a break is encountered or the flow is broken.
JavaScript Conditionals and Functions
Conditionals
Logical Operator:
and/or/not
- and: &&
- or: ||
- not:!
JavaScript Conditionals and Functions
Conditionals
Comparison Operators
=== strict equal
!== strict not equal
> greater than
>= greater than or equal
< less than
<= less than or equal
JavaScript Conditionals and Functions
Conditionals
else if Clause
JavaScript Conditionals and Functions
Conditionals
Truthy and Falsy
In JavaScript, values evaluate to true or false when evaluated as Booleans.
- Values that evaluate to true are known as truthy
- Values that evaluate to false are known as falsy
Falsy values include false, 0, empty strings, null undefined, and NaN. All other values are truthy.
JavaScript Conditionals and Functions
Functions
Arrow Functions (ES6)
Arrow function expressions were introduced in ES6. These expressions are clean and concise. The syntax for an arrow function expression does not require the function keyword and uses a fat arrow => to separate the parameter(s) from the body.
There are several variations of arrow functions:
- Arrow functions with a single parameter do not require () around the parameter list.
- Arrow functions with a single expression can use the concise function body which returns the result of the expression without the return keyword.
JavaScript Conditionals and Functions
Functions
Functions
Functions are one of the fundamental building blocks in JavaScript. A function is a reusable set of statements to perform a task or calculate a value. Functions can be passed one or more values and can return a value at the end of their execution. In order to use a function, you must define it somewhere in the scope where you wish to call it.
The example code provided contains a function that takes in 2 values and returns the sum of those numbers.
JavaScript Conditionals and Functions
Functions
Anonymous Functions
Anonymous functions in JavaScript do not have a name property. They can be defined using the function keyword, or as an arrow function. See the code example for the difference between a named function and an anonymous function.
JavaScript Conditionals and Functions
Functions
Function Expressions
Function expressions create functions inside an expression instead of as a function declaration. They can be anonymous and/or assigned to a variable.
JavaScript Conditionals and Functions
Functions
Function Parameters
Inputs to functions are known as parameters when a function is declared or defined. Parameters are used as variables inside the function body. When the function is called, these parameters will have the value of whatever is passed in as arguments. It is possible to define a function without parameters.
JavaScript Conditionals and Functions
Scope
Scope
Scope is a concept that refers to where values and functions can be accessed.
Various scopes include:
- Global scope (a value/function in the global scope can be used anywhere in the entire program)
- File or module scope (the value/function can only be accessed from within the file)
- Function scope (only visible within the function),
- Code block scope (only visible within a { … } codeblock)
JavaScript Conditionals and Functions
Scope
Block Scoped Variables
const and let are block scoped variables, meaning they are only accessible in their block or nested blocks. In the given code block, trying to print the statusMessage using the console.log() method will result in a ReferenceError. It is accessible only inside that if block.
JavaScript Conditionals and Functions
Scope
Block Scoped Variables
const and let are block scoped variables, meaning they are only accessible in their block or nested blocks. In the given code block, trying to print the statusMessage using the console.log() method will result in a ReferenceError. It is accessible only inside that if block.
JavaScript Arrays, Loops, and Iterators
Arrays
Method .push()
The .push() method of JavaScript arrays can be used to add one or more elements to the end of an array. .push() mutates the original array and returns the new length of the array.
JavaScript Arrays, Loops, and Iterators
Arrays
Method .pop()
The .pop() method removes the last element from an array and returns that element.
JavaScript Arrays, Loops, and Iterators
Loops
While Loop
The while loop creates a loop that is executed as long as a specified condition evaluates to true. The loop will continue to run until the condition evaluates to false. The condition is specified before the loop, and usually, some variable is incremented or altered in the while loop body to determine when the loop should stop.
JavaScript Arrays, Loops, and Iterators
Loops
Reverse Loop
A for loop can iterate “in reverse” by initializing the loop variable to the starting value, testing for when the variable hits the ending value, and decrementing (subtracting from) the loop variable at each iteration.
JavaScript Arrays, Loops, and Iterators
Loops
Do…While Statement
A do…while statement creates a loop that executes a block of code once, checks if a condition is true, and then repeats the loop as long as the condition is true. They are used when you want the code to always execute at least once. The loop ends when the condition evaluates to false.
JavaScript Arrays, Loops, and Iterators
Loops
Looping Through Arrays
An array’s length can be evaluated with the .length property. This is extremely helpful for looping through arrays, as the .length of the array can be used as the stopping condition in the loop.
JavaScript Arrays, Loops, and Iterators
Loops
Break Keyword
Within a loop, the break keyword may be used to exit the loop immediately, continuing execution after the loop body.
Here, the break keyword is used to exit the loop when i is greater than 5.
JavaScript Arrays, Loops, and Iterators
Loops
Nested For Loop
A nested for loop is when a for loop runs inside another for loop.
The inner loop will run all its iterations for each iteration of the outer loop.
JavaScript Arrays, Loops, and Iterators
Iterators
Functions Assigned to Variables
In JavaScript, functions are a data type just as strings, numbers, and arrays are data types. Therefore, functions can be assigned as values to variables, but are different from all other data types because they can be invoked.
JavaScript Arrays, Loops, and Iterators
Iterators
Callback Functions
In JavaScript, a callback function is a function that is passed into another function as an argument. This function can then be invoked during the execution of that higher order function (that it is an argument of).
Since, in JavaScript, functions are objects, functions can be passed as arguments.