JavaScript Flashcards
What is the purpose of variables?
To store data that we can go back to later. Permanence of data
How do you declare a variable?
var (variable name) then assignment operator (=) with variable value
How do you initialize (assign a value to) a variable?
var keyword then variable name with the assignment operator (=)
What characters are allowed in variable names?
$, letters, numbers, underscore (variable can’t begin with number
What does it mean to say that variable names are “case sensitive”?
words with lower case and upper case are completely separate things to JavaScript
What is the purpose of a string?
To store characters in a row that JavaScript won’t read as code
What is the purpose of a number?
To store numeric values
What is the purpose of a boolean?
Act as an “on or off” switch (indicating true or false)
What does the = operator mean in JavaScript?
Assignment operator to give something value
How do you update the value of a variable?
You just redeclare that variable as normal, except without the keyword “var”, “let”, or “const”
What is the difference between null and undefined?
undefined is the way JavaScript creates or says something is empty or has no value (empty area which could have been left empty for random reasons)
null is something that we HAVE assign to make something empty or have no value (empty parking lot is on purpose so we can fill it with cars)
Why is it a good habit to include “labels” when you log values to the browser console?
So that we can identify what exactly we are using the console.log method on.
Give five examples of JavaScript primitives.
String, boolean, null, undefined, and numbers
What data type is returned by an arithmetic operation?
number data type
What is string concatenation?
it’s using the plus sign operator to combine two or more strings together
What purpose(s) does the + plus operator serve in JavaScript?
it’s used for string concatenation or adding numbers together
What data type is returned by comparing two values (, ===, etc)?
a boolean
the strict equality operator (three equal signs) checks for same value and type
What does the += “plus-equals” operator do?
it adds the value of the right operand and a variable, then it assigns the result of that expression to the variable.
so a += 3 is the same as “var a = (a’s old value) + 3”
What are objects used for?
objects are used to store a collection of data
What are object properties?
object properties are the keys
Describe object literal notation.
keyword ‘var’ followed by a name then ‘=’ followed by curly brackets ‘{ }’
How do you remove a property from an object?
keyword ‘delete’ followed by object.property
What are the two ways to get or update the value of a property?
bracket notation or dot notation
What are arrays used for?
For representing lists of data
Describe array literal notation.
keyword ‘var’ followed by the variable name then an equal sign with square brackets [ ].
The square brackets are the only part of the array literal notation.
How are arrays different from “plain” objects?
Main thing arrays use numeric indexes (not created by you) while objects have alphanumeric indexes. All arrays come with property name (.length) and it is automatic. You also add stuff into the array differently.
Arrays are more like grocery lists (we care how much is on it) while objects are more like a dictionary.
What number represents the first index of an array?
arrayName[0]
Always starts at index 0
What is the length property of an array?
It stores the total number of items in an array
How do you calculate the last index of an array?
arrayName.length - 1
The length property stores a “true count.” So an array with two item the length is 2, which means we need to subtract 1.
What is a function in JavaScript?
A repeatable block of code
Describe the parts of a function definition.
the var keyboard, function keyword, optional function name, infinite parameters within parenthesis, and then opening curly brace for code block
Describe the parts of a function call.
function name and then the arguments within parenthesis, followed by semi-colon
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition creates the function and tells it what to do.
function call makes the function do the code.
What is the difference between a parameter and an argument?
A parameter is what a function takes when it is defined. (placeholder values)
An argument is what the function takes when it is called. (real values)
Why are function parameters useful?
They’re placeholders for the future input values. It makes it easier to identify what the function is doing.
What two effects does a return statement have on the behavior of a function?
It specifies a return value for the function and it exits/ends the function.
Why do we log things to the console?
To see how our code is working. It’s a debugging tool to constantly check the output of our code.
What is a method?
A function which is a property of an object
How is a method different from any other function?
A method is predefined? Methods are functions part of an object.
How do you remove the last element from an array?
array.pop()
No value needed in parenthesis
How do you round a number down to the nearest integer?
Math.floor()
It returns the largest integer less than or equal to a given number. So 5.95 is equal to 5, but -3.23 is equal to 3. Math.trunc() does something similar
Math.round is for nearest integer
How do you generate a random number?
Math.random()
Only goes from 0 up to, but not including 1
How do you delete an element from an array?
array.splice()
first parameter is starting point, second parameter is how many deletions, and third parameter is an optional and infinite number of items to add.
How do you append an element to an array?
array.push(‘element’) adds to the end of an array because it’s asking “APPEND” which means add to the end.
while,
array.unshift(‘element’) adds to the front of an array
*infinite # of elements can be taken
How do you break a string up into an array?
string.split() with the parameters being the indicator for what to split the string by. There’s also an optional limit parameter for setting the limit of substrings in the array.
Do string methods change the original string? How would you check if you weren’t sure?
No, Strings are immutable so can’t modify them. To check we would use the console: assign any string to a variable, use the split method with a separator for each character, and then type the string variable again.
Roughly how many string methods are there according to the MDN Web docs?
A lot…40-50
Is the return value of a function or method useful in every situation?
No, sometimes we are just concerned on what the method is doing rather than its return. For example, splice() returns an array of the deleted elements, but we are more concerned with the original array that splice was used on.
Roughly how many array methods are there according to the MDN Web docs?
A lot…40-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.
, ==, ===, <=, >=, !=, !==,
What data type do comparison expressions evaluate to?
ALWAYS a boolean (true or false)
What is the purpose of an if statement?
do determine if something is true or false, then execute some code based on that value
different behavior based on different data (allow us to make decisions)
Is else required in order to use an if statement?
no it’s not
Describe the syntax (structure) of an if statement.
if keyword, followed by parenthesis enclosing the test condition that compares two operands with a comparison operator then opening/closing curly brace.
What are the three logical operators?
&& (and), || (or), and ! (returns opposite value)
How do you compare two different expressions in the same condition?
Logical && operator and logical || operator (often called ‘pipes’ or vertical bar)
What is the purpose of a loop?
A way to do something more than once without typing it all out ourselves
What is the purpose of a condition expression in a loop?
To set a stopping point for the loop, it’s the brakes
What does “iteration” mean in the context of loops?
A single time that the code block runs
When does the condition expression of a while loop get evaluated?
Before each iteration to decide whether we should stop or not
When does the initialization expression of a for loop get evaluated?
Only at the start, before the first iteration. (it is the first thing that happens before anything else in the loop and it never happens again)
When does the condition expression of a for loop get evaluated?
Happens after the initialization and before each iteration of the loop
When does the final expression of a for loop get evaluated?
After running the code block, but before the condition runs again
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
The keyword break
What does the ++ increment operator do?
Increments the operand by 1, BUT it also reassigns the value.
We can use += to increment by more than 1.
i++ vs ++i
Ex: if counter = 1
counter++ + counter++ + ++counter + counter++
1 + 2 + 4 + 4 = 11
2 = 3 = 4 = 5
Answer is 11 for expression. Counter would equal 5.
How do you iterate through the keys of an object?
With a “for in” loop
For var KEYS in OBJECTS; If you need to loop through an object, use for in loop.
Why do we log things to the console?
We log things so that we can be sure that we are targeting/writing code for the right element or to evaluate values
What is a “model”?
A copy of the tree of objects of the HTML page
Which “document” is being referred to in the phrase Document Object Model?
The entire html document