JavaScript Flashcards
What is the purpose of variables?
To store data and information to be used for later
How do you declare a variable?
By using the var keyword and giving it a variable name
How do you initialize (assign a value to) a variable?
By using the “=” sign (assignment operator)
What characters are allowed in variable names?
Letters, numbers, dollar signs ($), or an underscore (_)
What does it mean to say that variable names are “case sensitive”?
Score and score are different variable names. Lowercase and Uppercase are different
What is the purpose of a string?
Add new text content into a page
What is the purpose of a number?
Represent numerical values. They are good for tasks that involve counting or calculating sums.
What is the purpose of a boolean?
Helpful when determining which part of a script should run.
What does the = operator mean in JavaScript?
Assignment operator
How do you update the value of a variable?
Assign a new variable value but with a different value. Don’t use the var keyword
What is the difference between null and undefined?
Null means an empty or non-existent value. Null is assigned, and explicitly means nothing.
Undefined means a variable has been declared, but the value of that variable has not yet been defined.
Why is it a good habit to include “labels” when you log values to the browser console?
It is much clearer which variables are being logged. If you do not include “labels”, it can be very confusing instead of helpful.
Give five examples of JavaScript primitives.
String, number, undefined, boolean, null
Primitives are stored data/values.
What data type is returned by an arithmetic operation?
A number
What is string concatenation?
The process of joining together 2 or more strings to create one new string
What purpose(s) does the + plus operator serve in JavaScript?
It adds numbers or concatenate strings
What data type is returned by comparing two values (, ===, etc)?
A boolean
What does the += “plus-equals” operator do?
adds the value of the right operand to a variable and assigns the result to the variable
What are objects used for?
group together a set of variables and functions to create a model of something you would recognize from the real world.
What are object properties?
A variable that is part of an object. Properties tell us about the object.
Describe object literal notation.
To access a property or method of an object you use the name of the object, followed by a period, then the name of the property or method you want to access.
How do you remove a property from an object?
Use the delete keyword and then use dot notation to identify the property or method you want to remove from the object
delete user.firstName
What are the two ways to get or update the value of a property?
Dot notation or square brackets
hotel.name = ‘park’;
hotel[‘name’] = ‘park‘;
What are arrays used for?
Store a list of values and items
Describe array literal notation.
Var with variable name, asmt operator (), [square bracket], values inside of it, [close with square bracket]
How are arrays different from “plain” objects?
Arrays use numerically indexed instead of property names.
What number represents the first index of an array?
[0]
What is the length property of an array?
The number of items in an array
How do you calculate the last index of an array?
Length - 1
What is a function in JavaScript?
An object that can allow you to package up code for use later in your program when called.
Describe the parts of a function definition.
- The function keyword to begin the creation of a new function.
- An optional name.
- A comma-separated list of zero or more parameters, surrounded by () parentheses.
- The start of the function’s code block, as indicated by an { opening curly brace.
- An optional return statement to stop the function
- The end of the function’s code block, as indicated by a } closing curly brace.
Describe the parts of a function call.
- The function’s name.
2. A comma-separated list of zero or more arguments surrounded by () parentheses.
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition requires a function keyword. Function call just needs the name.
What is the difference between a parameter and an argument?
When we define a function, we declare parameters and that when we call a function, we pass it arguments.
Why are function parameters useful?
Parameters alter function behavior. Useful because it can make modifications to how they run. They are local variables to the functions which are necessary for the function to operate correctly.
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a value we can use in our program.
Prevents any more code in the function’s code block from being run.
Why do we log things to the console?
To test if our code is working
What is a method?
Methods are functions stored as object properties
How is a method different from any other function?
Methods are attached to object.
How do you remove the last element from an array?
.pop
How do you round a number down to the nearest integer?
Math.floor
How do you generate a random number?
Math.random
How do you delete an element from an array?
array.splice method ( index , how many you want to delete)
How do you append an element to an array?
.push (Add to end)
.unshift (Add to beginning)
How do you break a string up into an array?
.split method
Do string methods change the original string? How would you check if you weren’t sure?
It does not change original string.
Check with .console log
Roughly how many string methods are there according to the MDN Web docs?
36
Is the return value of a function or method useful in every situation?
No because the return value is not always something you want to have. Sometimes you just want the functionality.
Roughly how many array methods are there according to the MDN Web docs?
50
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.
- Greater Than (>)
- Less Than (=)
- Equal To (==)
- Not Equal To( !=)
- Strict Not Equal To (!==)
What data type do comparison expressions evaluate to?
Boolean (true or false)
What is the purpose of an if statement?
Evaluates (or checks) a condition. IF the condition evaluates to true, any statements in the subsequent code block are executed.
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
Start with an if statement, Opening parentheses, condition operand, comparison operator, operand. Closing parentheses. Opening curly bracket. Return statement. Closing Curly Bracket.
What are the three logical operators?
Logical And (&&) Logical Or (||) Logical Not (!)
How do you compare two different expressions in the same condition?
Logical And (&&) or Logical Or (||)
What is the purpose of a loop?
Loops is to do something as fast possible a certain number of times.
What is the purpose of a condition expression in a loop?
To decide when the loop stop
What does “iteration” mean in the context of loops?
Everytime it goes through a loop
When does the condition expression of a while loop get evaluated?
Before each loop is run
When does the initialization expression of a for loop get evaluated?
At the start of the for loop. Before the loop begins.
When does the condition expression of a for loop get evaluated?
After the Initialization has been set. After the final expression runs
When does the final expression of a for loop get evaluated?
After the condition statement. After the code block.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break keyword will break out of loop but not end the function.
What does the ++ increment operator do?
increments (adds one to) its operand and reassigns the value
How do you iterate through the keys of an object?
For…in loop
for (var property in object)
Why do we log things to the console?
To make sure our code is working properly
What is a “model”?
Representation of a web page with a DOM tree
Which “document” is being referred to in the phrase Document Object Model?
The tree structure of word page
What is the word “object” referring to in the phrase Document Object Model?
All the elements within the DOM tree which are then created into objects.
What is a DOM Tree?
As a browser loads a web page, it creates a model of that page. The model is called a DOM tree, and it is stored in the browsers’ memory. A hierarchical chart which breaks down all the pieces of a document with four main types of nodes
Give two examples of document methods that retrieve a single element from the DOM.
querySelector (get anything), getElementByID (Only get ID)
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementsByClassName, getElementsByTagName, querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
It saves the browser from looking through the DOM tree to find the same element again.
What console method allows you to inspect the properties of a DOM element object?
console.dir
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
It takes a CSS Selector as an argument and returns the first of the matching elements.