JavaScript Flashcards
What is the purpose of variables?
Variables are used to store information for the computer to be able to reference back to. It brings permanence to data.
How do you declare a variable?
You declare a variable with the var keyword followed by the variable name. A variable can start with a letter, $, or _
var fullName
How do you initialize (assign a value to) a variable?
If a variable has not been declared yet, you use the variable keyword, the variable name, and then initialize the variable with the assignment operator =
var fullName = ‘Brandon Moy’;
What characters are allowed in variable names?
Lowercase letters, uppercase letters (camel case), numbers, $, and _
Variables cannot start with numbers
What does it mean to say that variable names are “case sensitive”?
It means that the capitalization must be consistent across the entire variable
var Score is different from var score
What is the purpose of a string?
A string it used to record a data type that lets us store any types of characters
e.g. ‘Hello World!’
What is the purpose of a number?
A data type number is recorded to recall a number value (can be used for calculations or incrementation)
var width = 10; var height = 10;
What is the purpose of a boolean?
A boolean is used to define a value as true or false. It is used for decision making.
What does the = operator mean in JavaScript?
The = operator assigns the value on the right side to the variable on the left side
How do you update the value of a variable?
You can update a value of a variable by assigning it a new value further down the script
early: var fullName = ‘Brandon’;
later: fullName = ‘Brandon Moy’;
What is the difference between null and undefined?
Both are empty values but for different purposes. Null is a nonexistent or invalid object (cannot be used by JavaScript [can be used as a placeholder as null can ONLY be assigned]) whereas undefined is returned by JavaScript when there is no value assigned
Null - used by programmers to show empty
Undefined - JavaScript’s way to show empty
Why is it a good habit to include “labels” when you log values to the browser console?
Labels print information on the values being logged to the console for more ease of readability
Give five examples of JavaScript primitives.
String, Numbers, Boolean, Null, Undefined
What data type is returned by an arithmetic operation?
A number data type is returned by an arithmetic operation. If a string is put through an arithmetic operation JavaScript will print back NaN (not a number)
What is string concatenation?
String concatenation is when two or more strings are combined together to create a new string.
‘Hello’ + ‘ ‘ + ‘World!’ will return as ‘Hello World!’
What purpose(s) does the + plus operator serve in JavaScript?
The + operator can be used as arithmetic operator or string concatenator
What data type is returned by comparing two values (<, >, ===, etc)?
When comparing two values a boolean is returned for the value of the comparison
What does the += “plus-equals” operator do?
The += operator adds the new value to the starting variable and then assigns this new combined value back to the variable.
What are objects used for?
Objects are used to store multiple pieces of information into location without affecting other data and multiple pieces of information into one location to call back to.
What are object properties?
Object properties are just variables assigned to one object
var objectName = { property: propertyValue };
Describe object literal notation.
You use the variable keyword, the variable name, and you assign a grouping of properties to it inside of { }. Each property is notated property: value and a comma is used to separate the properties.
var objectName = {
property: value,
property2: value
};
How do you remove a property from an object?
delete operator is used to remove a property
delete object.property;
What are the two ways to get or update the value of a property?
Dot notation - object.property = newValue;
Square bracket - object[‘property’] = newValue;
What are arrays used for?
Arrays are used for storing a list of data. e.g. List of students, colors, hotels, cars
Describe array literal notation.
Square bracket with value separated by a comma
How are arrays different from “plain” objects?
Objects will assign a value to a property for the key value pair while an array assigns the value to an index number for the key value pair. Arrays have set orders and length properties.
What number represents the first index of an array?
The number for the first index of an array is 0 since computers start counting from 0
What is the length property of an array?
The length property of an array is a count of how many values are stored in the array.
How do you calculate the last index of an array?
You can subtract 1 from the length of the array.
What is a function in JavaScript?
A list of steps that can be reused multiple times in the code.
Describe the parts of a function definition.
The function keyword, the (optional) name of the function, the parameter list separated by commas and surrounded by parenthesis, the open curly brace for the function block, code within the code block, probably a return
Describe the parts of a function call.
The function name, 0 or more arguments surrounded by parenthesis
When comparing them side-by-side, what are the differences between a function call and a function definition?
The definition has { } for code block, function keyword
What is the difference between a parameter and an argument?
Parameter - used in definition, placeholder for a future value
Argument - used in function call, actually value provided
Why are function parameters useful?
Parameters allow us to create variance. They allow us to create ONE function for MULTIPLE uses
What two effects does a return statement have on the behavior of a function?
1) It allows the results to be used outside of the function
2) It stops the function
Why do we log things to the console?
We log things to the console so that developers can check and see the output and the state of variables and functions. The console is the developers way of communicating with JavaScript
What is a method?
A method is a reference to a function that is used as a property of an object. E.g. Math.max() is the max method OF the math object
How is a method different from any other function?
A method is called as a property of the object it is pulling the information off of.
How do you remove the last element from an array?
You use the .pop() method of the array object to remove the last element
How do you round a number down to the nearest integer?
You can use the .floor() method of the array object to round down to the nearest integer
How do you generate a random number?
The .random() method of the max object will generate a random number between 0 and 1 which can then be multiplied by the length of the array you want to generate randomly from
How do you delete an element from an array?
The .splice(start, quantity) can delete elements from an array
How do you append an element to an array?
You use the .push() method to append an element to an array
How do you break a string up into an array?
Use the .split() method with the argument string value ‘ ‘ to separate the string at the argument locations
Do string methods change the original string? How would you check if you weren’t sure
They do not change the original string. We can console.log() the original variable housing the string we change. Instead of changing the original string, string methods create a new string.
Roughly how many string methods are there according to the MDN Web docs?
50
Is the return value of a function or method useful in every situation?
It might not be useful in every situation so we as developers need to create situations where the functions or methods we create/use are useful
Roughly how many array methods are there according to the MDN Web docs?
A lot. . .
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
> , <, >=, <=, ==, ===, !=. !==.
What data type do comparison expressions evaluate to?
Booleans
What is the purpose of an if statement?
If statements are used to create branching actions or decision making
Is else required in order to use an if statement?
Else is not required to use an if statement, but if the statement is false then the if statement will return undefined
Describe the syntax (structure) of an if statement.
If keyword, condition to check surrounded by parenthesis, curly brace to start code block for what to run if parameter is true, what to run if the parameter is true in the code block, closing curly brace
if (condition) {
statement
}
What are the three logical operators?
&& (and)
|| (or)
!(not)
How do you compare two different expressions in the same condition?
You can use || for or, or && for and between the two expressions but keep both of them within the same ( ) parameter
What is the purpose of a loop?
The purpose of a loop is to repeat an action
What is the purpose of a condition expression in a loop?
The condition expression in a loop allows JavaScript to know when the loop should be stopped
What does “iteration” mean in the context of loops?
An iteration is each time a loop is run. The first iteration would be the first time the loop is run and the last iteration is the final time a loop is run.
When does the condition expression of a while loop get evaluated?
The condition expression is evaluated before executing the statement in each iteration
When does the initialization expression of a for loop get evaluated?
The initialization expression of a for loop is evaluated once before the loop is begun
When does the condition expression of a for loop get evaluated?
The condition expression of a for loop gets evaluated before every iteration
When does the final expression of a for loop get evaluated?
The final expression of a for loops gets evaluated at the end of each iteration