JavaScript Flashcards
• What is the purpose of variables?
o Variables create permanence in your data
o It creates a box and stores information
• How do you declare a variable?
o Use a keyword like let or var.
• How do you initialize (assign a value to) a variable?
o Use an assignment operator (SINGLE EQUAL SIGN “=”)
• What characters are allowed in variable names?
o _ underscore and $. BUT variable names CANNOT start with a numeric value.
• What does it mean to say that variable names are “case sensitive”?
o String and string are different.
o It is not good practice to use variable names that are the same but with different casing.
• What is the purpose of a string?
o JavaScript strings are for storing and manipulating TEXT.
o These character are written INSIDE quotes.
o The string object is used to represent and manipulate a sequence of characters
• What is the purpose of a number?
o To store numeric values FOR calculations – mathematical operations
• What is the purpose of a boolean?
o To express a value of True or false
o Kind of like a light switch –
o Necessary for conditions and comparisons
o Helpful to determine whether or not to execute or not. (ON/OFF | it IS or IS NOT)
o Purpose of Booleans is to make DECISIONS (Yes or No)
• What does the = operator mean in JavaScript?
o Assigner
• How do you update the value of a variable?
o Use the assignment operator and WOULD NOT USE the keyword.
o VAR keyword is only necessary when creating the variable for the first time.
o CONFUSING.
o Does this variable already exist in my code?
• What is the difference between null and undefined?
o Null is assigned
Null is a value that can only exist because someone has assigned it to something.
It is purposeful – since someone intentionally put it there
o Undefined
Undefined is organic.
If there is no return value, it will come back as undefined.
Undefined is assigned by the browser – the ONLY value that JS can use to say “nothing”
• Why is it a good habit to include “labels” when you log values to the browser console?
o For clarity so that when we return to the code, we can see what we were working on and not get confused.
o Labels makes it clear what we were working with.
• Give five examples of JavaScript primitives.
o String, number, Boolean, null, undefined.
• What data type is returned by an arithmetic operation?
o Numbers – not necessarily integers.
• What is string concatenation?
o Adding together multiple strings.
o Concatenation will NEVER change the original string.
o Strings are immutable.
o They can never, EVER change.
• What purpose(s) does the + plus operator serve in JavaScript?
o Summing numerical data or string concatenation.
• What data type is returned by comparing two values (<, >, ===, etc)?
o Boolean
• What does the += “plus-equals” operator do?
o Shorthand for reassigning a value of the original variable to the new value.
o =+ will make a lasting change to something while + will add but will not save the previous value.
• What are objects used for?
o Objects group together a set of variables and functions to create a model of something you would recognize from the real world.
o In an object, variables and functions take on new names.
• What are object properties?
o In an object, variables become known as properties.
o If a variable is part of an object, it is called a PROPERTY.
o Properties tell us about the object, such as the name of a hotel or the number of rooms it has.
o Each individual hotel might have a different name and a different number of rooms.
• Describe object literal notation.
o THEY ARE A SET OF CURLY BRACES
o Properties separated by a colon.
o Commas to separate lines.
• How do you remove a property from an object?
To delete a property, use the delete keyword followed by the object name and property name.
ex: delete hotel.name;
• What are the two ways to get or update the value of a property?
o Use a dot or the brackets
o Bracket notation might be used when a property is inside an existing object.
• What are arrays used for?
o Allows us to group data types (typically alike – but can be different) and gives us a way to put it into a list format and helps us deal with them one item at a time and see the scope of each item.
• Describe array literal notation.
o Enclosed in square brackets [ ] – separated by commas.
• How are arrays different from “plain” objects?
o Arrays do not have individually named pieces of data – BUT are ordered sequentially numerically indexed.
o Arrays have an order and tell us how many properties are inside. Objects do NOT.
o To add data to an array, you use a method named PUSH.
o To add data to a property of an object, use the assignment operator.
o Arrays show and tell us how many things we are working with.
• What number represents the first index of an array?
o 0 ZERO
• What is the length property of an array?
o Property in all arrays
o Returns a true count of how many items are in that array.
• How do you calculate the last index of an array?
o Write out the length property of the length object.
o Whatever the value is of the array object and subtract 1.
• What is a function in JavaScript?
o A block of code
o A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value
• Describe the parts of a function definition.
o Function keyword, optional name, parameter list (with optional parameters), code block, return
• Describe the parts of a function call.
o Write the name of the function, parenthesis, and any number of arguments.
o What if there are no parameters?!
EMPTY PARENTHESIS.
A function call at its most basic form is the name and empty parenthesis.
Without parenthesis, NOTHING happens.
• When comparing them side-by-side, what are the differences between a function call and a function definition?
o Function call has the function name and argument.
o Function definition has function keyword – otherwise, it is a function call.
• What is the difference between a parameter and an argument?
o Parameter is a placeholder that just stays there.
o Argument, actual value that takes space in the placeholder.
• Why are function parameters useful?
o Without parameters, you’d have to create a new function for each use.
o Parameters provide mutability.
• What two effects does a return statement have on the behavior of a function?
o Causes the function to produce a value
o Prevents any more code in the function’s code block from being run. AKA – IT STOPS THE FUNCTION ENTIRELY!
o Functions that are meant to do a job won’t have a return BUT Functions that are meant to have a value WILL HAVE a return.
• Why do we log things to the console?
o A window we can communicate with the code –
a debugging tool
o Log stuff out and see what values are being used!
• What is a method?
• A method is a function which is a property of an object.
There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or
Static Methods which are tasks that are called directly on an object constructor.
• How is a method different from any other function?
o A function is a block of code
o A method requires you to specify what object that function is attached to USING DOT NOTATION. (library.push(js))
• How do you remove the last element from an array?
o pop() method.
• How do you round a number down to the nearest integer?
o Floor method of the Math object. –> math.floor
• How do you generate a random number?
o Math.random
(range 0 to 1… not including 1 – so, .9999999999)
• How do you delete an element from an array?
o Math.splice
• How do you append an element to an array?
o Push method
• How do you break a string up into an array?
o Split method – give it a set of character and it will look for those character and chop them out.
• Do string methods change the original string?
How would you check if you weren’t sure?
o It does not. No string methods EVER change the original string – they are IMMUTABLE!
o If not sure, you can console.log the method of the string.
o Log out the string and see if it changes!
• Roughly how many string methods are there according to the MDN Web docs?
o Many many.
• Is the return value of a function or method useful in every situation?
o a return value is used where the function is an intermediate step in a calculation of some kind. You want to get to a final result, which involves some values that need to be calculated by a function. After the function calculates the value, it can return the result so it can be stored in a variable; and you can use this variable in the next stage of the calculation.
• Roughly how many array methods are there according to the MDN Web docs?
o Many many.
• What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
o MDN
• Give 6 examples of comparison operators.
o Simply equal to
o NEVER USE = =
• What data type do comparison expressions evaluate to?
o Booleans
• What is the purpose of an if statement?
o To determine whether to run a code block and check a condition
• Is else required in order to use an if statement?
o Nope
• Describe the syntax (structure) of an if statement.
o If keyword with the condition which is evaluated to true/false
o If keyword, condition, code block.
• What are the three logical operators?
o And &&
o Or ||
o Not !
• How do you compare two different expressions in the same condition?
o Use AND / OR && or ||
• What is the purpose of a loop?
o Loops offer us the ability to repeat a single step / an action
• What is the purpose of a condition expression in a loop?
o To evaluate when to stop
o In the absence of a condition, loops will go on forever.
• What does “iteration” mean in the context of loops?
o One time that the code block gets run.
• When does the condition expression of a while loop get evaluated?
o At the beginning before each iteration
(BEFORE the code block runs)
• When does the initialization expression of a for loop get evaluated?
o The initialization step occurs one time only,
before the loop begins
o BEFORE ANYTHING – the very first thing.
• When does the condition expression of a for loop get evaluated?
o First time, after the initialization
o Every subsequent time, before the code block of the next iteration
And after the final expression or the initialization
• When does the final expression of a for loop get evaluated?
o Before the condition and 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?
o Break.
• What does the ++ increment operator do?
o Adds AND saves one to the variable
• How do you iterate through the keys of an object?
o With a for – in loop