JS Flashcards
What is the purpose of variables?
Variables are used to store information that will be used later
How do you declare a variable?
We declare a variable by using the var keyword followed by the variable
name, then assignment operator followed by a value
How do you initialize (assign a value to) a variable?
We use variable name, followed by the assignment
operator (=) followed by a variable value
What characters are allowed in variable names?
letters, numbers, underscore, and dollar signs, but
variables can not begin with a number
What does it mean to say that variable names are ”case sensitive”?
It means uppercase and lowercase
letters has an affect on the word name. As an example, ”A” is different that ”a
What is the purpose of a string?
Used for tasks involving text content or tasks involving words or
phrases
What is the purpose of a number?
Used for tasks involving counting, adding, or mathematical operations,or anything that involves numbers
What is the purpose of a boolean?
Used for true/false tasks or used for decision making
What does the = operator mean in JavaScript?
= is an assignment operator. It sets a variable to
equal something or update a variable’s value
How do you update the value of a variable?
We will use the assignment operator for the variable
What is the difference between null and undefined?
null means the value is nonexistent where undefined
means the variable has not been defined or the variable has just been declared
Why is it a good habit to include ”labels” when you log values to the browser console?
So we can know what the outputs are referring to
Give five examples of JavaScript primitives
- string
- number
- boolean
- undefined
- null
- symbol
- bigint
2
What data type is returned by an arithmetic operation?
numbers
What is string concatenation?
It is combining two strings into one string
What purpose(s) does the + plus operator serve in JavaScript?
this is used for string concatenation
and addition
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += ”plus-equals” operator do?
this will add the number and then set the variable
equal to the result
What are objects used for?
Objects are a group of variables and functions used to create a model of
something from the real world
What are object properties?
Object properties are variables that are apart of an object
Describe object literal notation
Object literal notation is used to create an object. We start with a
var keyword followed by a variable name with the assignment operator and curly braces. Inside the
curly braces, we have a key and their value separated by a colon. Each key is separated by a comma
How do you remove a property from an object?
We would use the delete operator followed by the
object name, period, and the property we want to remove
What are the two ways to get or update the value of a property?
to access the value of a property, we would use object, period, followed by the property name (object.newValue).
To update the value of an object property, we would used, object, open bracket, property name, close
bracket, assignment operator, and the new property value. (object[’property’] = newValue)
What are arrays used for?
Arrays are used for storing data in an organized and predictable manner.
Each value is enumerated. Store a list of data in a set order
Describe array literal notation.
We start with a var keyword, followed by the array name, then
assignment operator, open bracket, values stored in the array in order, and close bracket. (var array
= [1,2,3])
How are arrays different from ”plain” objects?
Arrays differ from plain objects for arrays are ordered
where objects are not
What number represents the first index of an array?
0
What is the length property of an array?
It will give the number of items in an array
How do you calculate the last index of an array?
We will take the length of the array and subtract 1
What is a function in JavaScript?
Functions are a “chunk” of code that you can use over and over
again, rather than writing it out multiple times
Describe the parts of a function definition.
function keyword, function name (optional), open curly
brace, code, return statement, and close curly brace
Describe the parts of a function call.
a function call is when we are calling a function to run their block
of code with arguments. We need the function name, and the arguments
When comparing them side-by-side, what are the differences between a function call and a function
definition?
A function call will use specific arguments where a function definition with have a general
parameter. Also, there is no curly braces in a function call or function keyword
What is the difference between a parameter and an argument?
A parameter is used in a function
definition where argument is used in calling a function
Why are function parameters useful?
They are a placeholder for what kind of inputs the function will
take. They could give a clue into what the function wants as an argument and used for variance for
functions
What two effects does a return statement have on the behavior of a function?
The return statement causes the function to produce a value we can use in our program and Prevents any more code in the
function’s code block from being run
Why do we log things to the console?
So we can verify the outputs
What is a method?
A method is a function which is a property of an object
How is a method different from any other function?
It is because they are within an object
How do you remove the last element from an array?
We can use the pop() method of the array object
to remove the last element from an array
How do you round a number down to the nearest integer?
We can use the floor() method of the Math object
How do you generate a random number?
We can use the random() method of the Math object
How do you delete an element from an array?
We can use splice() method of the array object with a
start index followed by 1 item
How do you append an element to an array?
We can use the push() method of the array object with an argument of the object we want to append
How do you break a string up into an array?
We can use the split() method of the string object
Do string methods change the original string?
How would you check if you weren’t sure? No. We can
check by console.logging the original 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?
No. Not in all cases. Like, the
pop() method will remove the last value of the array, but we may not care about the last value we
removed
Roughly how many array methods are there according to the MDN Web docs?
30 or 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.
== (equal to), != (not equal to), > (greater than), < (less
than), === (strict equal to ), !==(not strict equal to), >= (greater than or equal to), <= (less than
or equal to)
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
They are used for decision making
Is else required in order to use an if statement?
No, else statements are not required
Describe the syntax (structure) of an if statement.
We start with an if keyword, followed by open
parentheses, a compassion expression, then a closed parentheses, followed by an open brace, with
something to happen if the condition is true, the a close brace.
What are the three logical operators?
&& (logical and), || (logical or), !(logical not)
How do you compare two different expressions in the same condition?
We can use parentheses to separate the expressions and use && or ||
What is the purpose of a loop?
To repeat a set of code until a certain condition is reached
What is the purpose of a condition expression in a loop?
To give the loop a stopping point
What does ”iteration” mean in the context of loops?
iteration means each time the loop code is ran
When does the condition expression of a while loop get evaluated?
It gets evaluated at the beginning of every iteration
When does the initialization expression of a for loop get evaluated?
At the beginning before the loop.
It only happens one time
When does the condition expression of a for loop get evaluated?
At the beginning of each iteration
When does the final expression of a for loop get evaluated?
At the end of each iteration
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
The break keyword
What does the ++ increment operator do?
++ adds one to the variable and assign the new value to
the variable
How do you iterate through the keys of an object?
We can use for in loops
What event is fired when a user places their cursor in a form control?
The focus event
What event is fired when a user’s cursor leaves a form control?
The blur event
What event is fired as a user changes the value of a form control?
The input event
What event is fired when a user clicks the ”submit” button within a ?
The submit event is
attached to the form element
What does the event.preventDefault() method do?
The preventDefault() method of the Event interface
tells the user agent that if the event does not get explicitly handled, its default action should not be
taken as it normally would be. Prevent the default behavior of the event
What does submitting a form without event.preventDefault() do?
Without it, the page will automatically reload when the submit button is pressed
What property of a form element object contains all of the form’s controls.
The elements property
What property of a form control object gets and sets its value?
The value property
What is one risk of writing a lot of code without checking to see if it works so far?
Our code could not
work and it would be difficult to find where it went wrong
What is an advantage of having your console open when writing a JavaScript program?
So we can
verify the outputs are what we expect it to be
What is the event.target?
This will gives us the most specific element the event is interacting with
What is the affect of setting an element to display: none?
The display of the element will not appear
on the web page and removed from the document flow
What does the element.matches() method take as an argument and what does it return?
It takes CSS Selectors and returns true if the Element matches the selectors. Otherwise, false.
How can you retrieve the value of an element’s attribute?
We can use the getAttribute() method of
the element object. It takes a string of the name of the attribute
At what steps of the solution would it be helpful to log things to the console?
Every time we changed
the view
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how
would your JavaScript code be written instead?
We would need to use event listener for everything
button
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript
code be written instead?
We can write a conditional for every element in the loop
What is JSON?
JSON is a text-based data format following JavaScript object syntax that can repre-
sent numbers, booleans, strings, null, arrays (ordered sequences of values), and objects (string-value
mappings) made up of these values (or of other arrays and objects)
What are serialization and deserialization?
Serialization is the process of turning an object in memory
into a stream of bytes so you can do stuff like store it on disk or send it over the network. Deserialization
is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
They are useful for storing, transmitting, and retrieving data
How do you serialize a data structure into a JSON string using JavaScript?
We can use JSON.stringify()
method
How do you deserialize a JSON string into a data structure using JavaScript?
We can use JSON.parse()
method
How do you store data in localStorage?
We can use the setItem() method of the localStorage object
How do you retrieve data from localStorage?
We can use the getItem() method of the localStorage
object
What data type can localStorage save in the browser?
Strings
When does the ’beforeunload’ event fire on the window object?
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible
and the event is still cancelable at this point
What is a method?
A method is a function that is stored in a property of an object
How can you tell the difference between a method definition and a method call?
A method definition is located inside of an object literal, where a method call is begin call from an object
Describe method definition syntax (structure).
We start with an object in object literal notation. Next,
we give the function a name, followed by a semicolon, followed by a function keyword and parenthesis,
followed by an opening curly brace and the function code block.
Describe method call syntax (structure).
We start with an object. Then we use the object name,
followed by the name of the function, opening parenthesis, the arguments, and closing parenthesis
How is a method different from any other function?
Methods are stored inside objects. They can not
be called outside of the object
What is the defining characteristic of Object-Oriented Programming?
We need an object to store the data and behaviors of the object
What are the four ”principles” of Object-Oriented Programming?
– Abstraction - The idea of generalizing details without needing a full understanding of the me-
chanics of how it actually functions
– Encapsulation - The idea of limiting access to data/methods that are stored within an object
– Inheritance - The mechanism of basing an object or class upon another object (prototype-based
inheritance) or class (class-based inheritance)
– Polymorphism - Polymorphism is the provision of a single interface to entities of different types
or the use of a single symbol to represent multiple different types.
What is ”abstraction”?
Abstraction is used to generalize ideas without needing to understand the full mechanics of how something works
What does API stand for?
Application programming interface (API) is a way for two or more computer
programs to communicate with each other
What is the purpose of an API?
Application programming interface connects computers or pieces of
software to each other
What is this in JavaScript?
This is an implicit parameter of all JavaScript functions
What does it mean to say that this is an ”implicit parameter”?
By being an implicit parameter, it
means that it is available in a function’s code block even though it was never included in the function’s
parameter list or declared with var
When is the value of this determined in a function; call time or definition time?
Call time
How can you tell what the value of this will be for a particular function or method definition?
the
value of this can be recognized as ”the object to the left of the dot” when the function is called (as a
method). If there is no value to the left of the dot when the function is called, then by default, this
will be the global window object. If you cannot see the function being called, then you do not know
what the value of this will be.
How can you tell what the value of this is for a particular function or method call?
the value of this can be recognized as ”the object to the left of the dot” when the function is called (as a method). If
there is no value to the left of the dot when the function is called, then by default, this will be the
global window object.