JavaScript Flashcards
What is the purpose of variables?
To store, represent, and access data via an easily accessible name called an identifier.
How do you declare a variable?
var keyword, or in ES6: let and const keywords.
How do you initialize (assign a value to) a variable?
Using the assignment operator = followed by the value, followed by a semi-colon.
What characters are allowed in variable names?
Letters, numbers, dollar sign, or an underscore.
Cannot begin with a number.
Notes: Using the underscore is not very common in JavaScript, but a convention among professional programmers is to use it as an alias for “private (hidden)” variables.
Using the $ is not too common, but professional programmers often use it as an alias for the main function in a JavaScript library or using them for variables holding DOM elements. In jQuery, for instance, the main function $ is used to select HTML elements. In jQuery $(“p”); means “select all p elements”.
What does it mean to say that variable names are “case sensitive”?
That changing the case of a letter in a declared variable name will not access the value. The memory address for a variable is assigned to the identifier using the case the declaration was made it. Guidelines state to use camelCase.
What is the purpose of a string?
For storing and manipulating a sequence of characters or text
What is the purpose of a number?
To store and manipulate numerical values and to do math, more precisely 64 bit floating point numbers (up to 15 digits and 17 decimals), positive and negative infinity, and NaN.
What is the purpose of a boolean?
To find out if an expression or a variable is true or false or when you need a data type that can only have two values, like on and off. Used for comparisons and for decision-making in a program (conditionals).
What does the = operator mean in JavaScript?
It’s an assignment operator, it assigns value to a variable
How do you update the value of a variable?
Using the assignment operator, math functions or other methods, or changing property values
What is the difference between null and undefined?
Undefined is the absence of data, no value is assigned, is falsy.
Null is the intentional absence of data, treated as a falsy value. Usually a placeholder for data that later gets created or added.
Why is it a good habit to include “labels” when you log values to the browser console?
It describes the value being logged, it can help with debugging by adding clarity to what you’re looking at and what’s happing in your script.
Give five examples of JavaScript primitives.
string, number, boolean, undefined, and null.
- extra -
bigint, symbol,
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
It’s the operation of joining strings together, which can be done with the addition operator or a template literal – or you can cheat with just using a comma between strings, lol.
What purpose(s) does the + plus operator serve in JavaScript?
Concatenation, addition, and the unary plus operator converts its operand to Number type.
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
It adds the value to the right of the addition assignment operator to the variable and then assigns the result of the expression to that variable
What are objects used for?
To store a group of key/value pairs and methods as a single entity, or model, as Duckett describes it.
What are object properties?
Variables that store some related data about the object, often referred to as key/value pairs.
Describe object literal notation.
Variable declaration or identifier for the for the object, then wrapped in curly braces, property or keys and values separated by a colon, multiple key/value pairs are separated by commas.
ex: var objectName = {
property: value,
property: value
}
How do you remove a property from an object?
Using the delete keyword followed by object.property (or object[‘property’])
What are the two ways to get or update the value of a property?
Dot notation or bracket notation.
What are arrays used for?
To store lists, usually ordered lists.
Describe array literal notation.
Variable declaration to store the array in, then wrapped in square brackets, a list of values separated by commas.
Ex: var arrayName = [item1, item2, item3];
How are arrays different from “plain” objects?
They do not hold custom keys for which the values are paired, rather they are numerically indexed.
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?
array.length - 1
What is a function in JavaScript?
It’s a set of statements that performs a task, like a procedure. Referred to as a power tool, per the reading.
Describe the parts of a function definition.
1) A function is defined with the function keyword,
2) An optional function name,
3) then 0 or more parameters wrapped in parentheses, separated by commas
4) Then, the function’s code block wrapped in curly brackets,
5) Which may contain an optional return statement.
Describe the parts of a function call.
1) function name
2) optional comma-separated argument list wrapped in parentheses
3) followed by a semi-colon.
W3 called this ‘calling from code’.
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function definition includes the function keyword (in ES5), optional parameters, and a code block defining what the function does.
A function call uses the defined function and includes the arguments you want to pass into the function.
What is the difference between a parameter and an argument?
Parameters are placeholders for values that can be passed into a function and are listed inside parentheses in the function declaration. They are usually a variable with a descriptive identifier. They are local variables scoped to a function block.
Arguments are the actual values you’re passing into the function and are wrapped by parentheses when you invoke the function.
Why are function parameters useful?
They are variables accessible by the function code block and tell us what to pass into a function, and often (not always), the number of arguments to pass in.
What two effects does a return statement have on the behavior of a function?
1) Causes the function to produce a value we can use in our program.
2) Exits the code block and prevents any more code past the return statement from being run.
Why do we log things to the console?
To assist with debugging and to add clarity to what you’re looking at and what’s happing in your script.
What is a method?
It’s a function that’s stored as a property of an object.
There are 2 kinds – instance methods and static methods.
Instance methods are built-in functions that are typically used on an instance of an object, typically an object you would create, so the method is called on the instance of the created object.
Static methods are directly accessible from an object’s constructor and are referenced by the object name and can be invoked without creating an instance of an object. Ex: Math object methods, and Array object methods.
How is a method different from any other function?
Methods are directly associated with an object.
How do you remove the last element from an array?
.pop(); method
ex: array.pop();
How do you round a number down to the nearest integer?
Math.floor(); method
ex: Math.floor(number)
How do you generate a random number?
Math.random(); (kind of)
This produced a pseudo-random number from 0 up to, but not including 1.
How do you delete an element from an array?
1) You can use the .splice(); method on a specific index and for a number of indexes.
2) You can use the .pop(); method to remove one from the end.
3) You can use the .shift(); method to remove one from the front of an array.
4) You can reassign the length property to delete a number of indexes at the end of an array, but this is very dangerous and should probably be avoided.
How do you append an element to an array?
.push(); method
ex: array.push(‘new-item’);
How do you break a string up into an array?
.split(); method and pass in arguments that define the separator, and a limit of splits (optional).
(There is also the Array.from(); method if you want to split each character as a different index.)
Do string methods change the original string? How would you check if you weren’t sure?
No, strings are immutable. You can check by logging the variable to which the original string was assigned to the console.
Roughly how many string methods are there according to the MDN Web docs?
Around 3 static, and 33 instance methods.
Is the return value of a function or method useful in every situation?
No, because sometimes the function output is not germane to the task we’re trying to accomplish. For instance, in this exercise, the unshift and push methods return the new length of the array and we may not always care about the new length, but rather care about the index values of the array.
Roughly how many array methods are there according to the MDN Web docs?
Around 3 static, and 35 instance methods.
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?
It checks conditions as true or false, and should the condition(s) be met, the following code block executes.
Is else required in order to use an if statement?
No, if you are looking for a condition to evaluate to true, the only other else is false, and therefore is not needed.
Describe the syntax (structure) of an if statement.
1) If keyword,
2) Followed by a condition wrapped in parentheses,
3) Followed by a code block to execute if the condition is met wrapped in curly braces.
What are the three logical operators?
1) && (AND)
2) || (OR)
3) ! (NOT)
How do you compare two different expressions in the same condition?
Using the logical AND or OR operators.
What is the purpose of a loop?
To repeatedly check conditions and perform the same set of steps (run a code block or statement) until the condition evaluates false.
NOTE: A do…while loop will always run once, even if the condition evaluates to false.
What is the purpose of a condition expression in a loop?
To tell the loop when to run and when to stop.
What does “iteration” mean in the context of loops?
It’s referring to the repetition each time the code is run, each loop.
When does the condition expression of a while loop get evaluated?
It’s evaluated before executing the statement, or the code block, every loop.
When does the initialization expression of a for loop get evaluated?
It’s evaluated first, before anything else, and only gets evaluated once.
Typically used to initialize a counter variable.
When does the condition expression of a for loop get evaluated?
After the initialization expression on the first loop, and before each loop iteration and
If omitted, the condition always evaluates to true.
When does the final expression of a for loop get evaluated?
At the end of each loop iteration, before the next condition check. Used to update or increment the counter variable.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break keyword.
What does the ++ increment operator do?
Increments the counter variable by 1, it’s the same +=1
How do you iterate through the keys of an object?
Using a for…in loop.
Arrays are for…of loops.
What is a “model”?
It is a representation or recreation of something, in this case html recreated as nodes and objects
Which “document” is being referred to in the phrase Document Object Model?
The HTML document.
What is the word “object” referring to in the phrase Document Object Model?
JavaScript Object
What is a DOM Tree?
It is a model of a webpage, and is constructed out of four main types of nodes, the document node, element nodes, attribute nodes, and text nodes.
Give two examples of document methods that retrieve a single element from the DOM.
.querySelector()
.getElementByID()
Give one example of a document method that retrieves multiple elements from the DOM at once.
.getElementsByClassName()
-extra-
.getElementsByTagName()
.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
To access and manipulate the return value, especially if you need it more than once
What console method allows you to inspect the properties of a DOM element object?
Console.dir - displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.
In other words, console.dir() is the way to see all the properties of a specified JavaScript object in console by which the developer can easily get the properties of the object.
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
In order for the browser to parse the HTML first, then the script can access the HTML elements.
Any time you want to access the DOM and use JavaScript to manipulate it, we must add the script tag at the bottom of the body.
What does document.querySelector() take as its argument and what does it return?
It takes a CSS selector (in string form) as it’s argument.
It returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
What does document.querySelectorAll() take as its argument and what does it return?
It takes a selector (in string form) as the argument.
It returns a NodeList representing a list of the document’s elements that match the specified group of selectors.
What is the purpose of events and event handling?
Events are fired to notify code of “interesting changes”, and have your script do something in response to the event. Handling events allows you to execute specified code on a specified element at the time of the event.
Are all possible parameters required to use a JavaScript method or function?
No, not always. They can be optional.