JavaScript Flashcards
What is the purpose of variables?
assign values
How do you declare a variable?
use var, let, const
How do you initialize a variable?
equal sign
What characters are allowed in variables names?
must begin with a letter, dollar sign, or underscore
can contain numbers but cannot use a dash or period
cannot use keywords or reserved words
case sensitive
What dose it mean to say that variable names are case sensitive?
if two variables are spelled the same way but have different casing, then js treats them as two different variables
What is the purpose of a string?
to store characters
What is the purpose of a number?
to store numbers
What is the purpose of a boolean?
to store something as true or false
What does the = equals sign operator mean in JS
assign a variable
How do you update the value of a variable?
you use the equal sign operator again
What is the difference between null and undefined?
a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address
a variable is undefined when a value is not assigned to it
Why is it a good habit to include labels when you log values to the browser console?
This will allow you to see if what you are storing is good and makes sense
Give five examples of JavaScript primitives
string, number, boolean, undefined, symbol, null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combining strings together
What purpose(s) does the + plus operator serve in JS?
addition, incrementing, concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equal” operator do?
adding or concatenating something to itself
What are objects used for?
store multiple types of data
What are object properties?
object properties are the keys of data that are stored in an object
Describe object literal notation
declare a variable, assignment operator, curly brackets
How do you remove a property from an objects?
use delete function
What are the two ways to get or update the value of a property?
dot and bracket notation
What are arrays used for?
ordered lists and groupings of information
Describe array literal notation
square brackets
How are arrays different from plain objects?
ordered, use index
What number represents the first index of an array?
0
What is the length property of an array?
can be called to find the length of the array
How do you calculate the last index of an array?
array.length-1
What two effects does a return statement have on the behavior of a function?
returns a value and exits code block
Why are function parameters useful?
allows functions to run with certain values
What is the difference between a parameter and an argument?
parameter is for defining, argument for calling
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition has function keyword and {}
Describe the parts of a function call.
function call consists of function name and parentheses and possible arguments
Describe the parts of a function definition.
function keyword, function name, parentheses, possible parameters, {}
What is a function in JavaScript?
code that can be repeated over and over again easily
Why do we log things to the console?
so that we can see that things are being input correctly
What is a method?
a function that is a property of an object
How is a method different from any other function?
functions themselves are objects, so a method is actually an object reference to a function
How do you remove the last element from an array?
pop method
How do you round a number down to the nearest integer?
floor method
How do you generate a random number?
random method
How do you delete an element from an array?
splice method
How do you append an element to an array?
push or unshift (prepend) method
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?
mdn, console log, they do not
Roughly how many string methods are there according to the MDN Web docs?
a lot
Roughly how many array methods are there according to the MDN Web docs?
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
there is the equal to, less than, greater than, strictly equal to, less than or equal to, and greater than or equal to
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
to make a decision
Is else required in order to use an if statement?
no
Describe the syntax of an if statement
if statement, () with conditionals inside, {} with rule to run if conditions are met
What are the three logical operators?
logical and, logical or, logical not
How do you compare two different expressions in the same condition?
use logical and or logical or
When does the condition expression of a while loop get evaluated?
after the initialization, and at the beginning of every loop
What does “iteration” mean in the context of loops?
you loop and then add something to count up
What is the purpose of a condition expression in a loop?
to see if you continue looping
What is the purpose of a loop?
to perform actions over and over again
How do you iterate through the keys of an object?
for in loops
What does the ++ increment operator do?
increment by 1
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
When does the final expression of a for loop get evaluated?
just before the condition is met
When does the initialization expression of a for loop get evaluated?
beginning of the loop
What is the event.target?
the object of the element that the event had happened to
What is the affect of setting an element to display: none?
removes from document flow
What does the element.matches() method take as an argument and what does it return?
takes in a string and returns a boolean
How can you retrieve the value of an element’s attribute?
getAttribute()
At what steps of the solution would it be helpful to log things to the console?
when you assign variables
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?
you would have to include many more event listeners
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?
you would have to include many more if statements
What is JSON?
json is a string whose format resembles an object literal format
What are serialization and deserialization?
serialization is making objects into strings and deserialization is the reverse
Why are serialization and deserialization useful?
converting to json and back
How do you serialize a data structure into a JSON string using JavaScript?
stringify
How do you deserialize a JSON string into a data structure using JavaScript?
parse
How do you store data in localStorage?
you can store data in localStorage by storage.setItem() method
How do you retrieve data from localStorage?
use storage.getItem() method
What data type can localStorage save in the browser?
JSON strings
When does the ‘beforeunload’ event fire on the window object?
when the window, the document, and its resources are about to be unloaded
What is a method?
a method is a function which is a property of an object
How can you tell the difference between a method definition and a method call?
a method will be defined within an object, similar to a function while a method is called with an object. and then the method name and the ()
Describe method definition syntax (structure).
var object = { method: function (parameter) { do something; } }
Describe method call syntax (structure).
object.method(argument);
How is a method different from any other function?
it is a property of an object
What is the defining characteristic of Object-Oriented Programming?
it is based on the concept of objects which contain data and code
What are the four “principles” of Object-Oriented Programming?
abstraction, encapsulation, inheritance, and polymorphism
What is “abstraction”?
abstraction is modeling and simplifies things for us
What does API stand for?
application programming interface
What is the purpose of an API?
gives programmers a way to interact with a system in a simplified, consistent fashion
What is this in JavaScript?
this is an implicit parameter that uses the object that it is attached to
What does it mean to say that this is an implicit parameter?
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
What does this refer to in the following code snippet? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
the object character
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
message will be logged to the console with string:
‘It’s-a-me, Mario!’
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
you are essentially calling this.firstName on something that does not have a first name property so it returns undefined
How can you tell what the value of this will be for a particular function or method definition?
cannot be sure
How can you tell what the value of this is for a particular function or method call?
left of the dot
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
What is a prototype in JavaScript?
prototypes are the mechanism by which javascript objects inherit features from one another; it is a template object that it inherits methods and properties from
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
use prototype objects to assign those things to them
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
in the object __proto__
What does the new operator do?
creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof operator do?
it tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object
What is a “callback” function?
A callback function is a function passed into another function which is then invoked inside the outer function to complete some kind of routine or action; often used for asynchronous operations
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
use the setTimeout() function
How can you set up a function to be called repeatedly without using a loop?
use setInterval() function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
setTimeout() returns a positive integer value which identifies the timer created by the call to setTimeout()
setInterval() returns a numeric, non-zero value which identifies the timer createdby the call to setInterval()
What is a code block? What are some examples of a code block?
A code block is a group of statements that are delimited by a pair of curly brackets if (condition) { // this is a block and will run code in this block if the statement is met }
What does block scope mean?
things exist only within the space of the code block; things that are created within the block scope can only be accessed within the block scope
What is the scope of a variable declared with const or let?
they are block scoped
What is the difference between let and const?
let can be reassigned while const creates only a read-only reference to a value and this reference cannot be reassigned but the value can be changed
Why is it possible to .push() a new value into a const variable that points to an array?
only the reference cannot be changed but the value that is being point at can be changed
How should you decide on which type of declaration to use?
are you going to be reassigning variables? use let. if not then use const
What is the syntax for writing a template literal?
instead of using single quotes or double quotes, a template literal uses backticks
What is “string interpolation”?
the ability to substitute part of the string for the values of variables or expressions which is also called string formatting
What is destructuring, conceptually?
Breaking down into individual pieces
What is the syntax for object destructuring?
var { property, property: variableNameYouWant } = obj
What is the syntax for array destructuring?
const [ firstIndexToCopy, secondIndexToCopy, , fourthIndexToCopy ] = arr
How can you tell the difference between destructuring and creating Object/Array literals?
One uses {} and the other uses []
also, object literals does not need to consider any indexes or empty indexes while you need to account for them in the array literals
What is the syntax for defining an arrow function?
if there are no parameters or multiple parameters, you need parentheses but if there is only one parameter, you can omit them
arrow function keyword is necessary
curly brackets are necessary if you are using statements and not just expressions
() => { return x }
When an arrow function’s body is left without curly braces, what changes in its functionality?
does not return anything
How is the value of this determined within an arrow function?
at definition time
What are the three states a Promise can be in?
pending, fulfilled, rejected
How do you handle the fulfillment of a Promise?
use a then statement
How do you handle the rejection of a Promise?
use the optional argument of a then statement or a catch statement
What is Array.prototype.filter useful for?
it is selective for the elements in your array
What is Array.prototype.map useful for?
Making adjustments to all the elements in your array
What is “syntactic sugar”?
syntactic sugar is syntax within a programming language to make things easier to read or to express;
makes it easier for the programmer to accomplish certain or to make existing language more intuitive
What is the typeof an ES6 class?
it is a function
Describe ES6 class syntax.
class className { constructor (name) { this.name = name }
getName() {
return this.name;
}
}
What is “refactoring”?
refactoring is the process of restructuring existing computer code without changing its external behavior; in line with syntactic sugar in making things more intuitive and efficient for the programmer
How are ES Modules different from CommonJS modules?
different syntax, static, has direct support for asynchronous loading and configurable module loading
What kind of modules can Webpack support?
ecmascript, commonjs, amd, assets, webassembly
What does fetch() return?
fetch returns a promise for a response at all;
fetch only fails if you spell it wrong or if you have no internet connection
What is the default request method used by fetch()?
get
How do you specify the request method (GET, POST, etc.) when calling fetch?
in the optional arguments
What must the return value of myFunction be if the following expression is possible?
myFunction()();
it returns a function
What does this code do? const wrap = value => () => value;
wrap is being assigned an anonymous function that takes a value as a parameter and then returns a function that has no parameters and then returns the value parameter that was initially declared in the parent anonymous function
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
a function’s scope is determined during the definition
What allows JavaScript functions to remember values from their surroundings?
scope