JavaScript Flashcards
What is the purpose of variables?
Variables are meant for storing values.
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
variableName = value;
What characters are allowed in variable names?
Alphabet characters, underscores, dollar signs, numbers (but not in the first char).
What does it mean to say that variable names are “case sensitive”?
name and Name are separate variables.
What is the purpose of a string?
To store lists of characters, useful for communicating information to humans or computers.
What is the purpose of a number?
To store numerical values for record keeping or calculation.
What is the purpose of a boolean?
Storing true/false values, used in control flow.
What does the = operator mean in JavaScript?
The right value is being assigned to the left variable.
How do you update the value of a variable?
varName = value;
What is the difference between null and undefined?
Null is intentional, undefined is usually not.
Why is it a good habit to include “labels” when you log values to the browser console?
So you know what’s being logged.
Give five examples of JavaScript primitives.
string, number, boolean, undefined, null
What data type is returned by an arithmetic operation?
Number.
What is string concatenation?
Putting two or more string values together to form a greater string
What purpose(s) does the + plus operator serve in JavaScript?
Arithmetic addition and concatenation.
What data type is returned by comparing two values?
Booleans.
What does the += “plus-equals” operator do?
Add what’s on the right to current value of variable then assign result to the variable.
What are objects used for?
Storing variables and functions associated with a particular entity.
What are object properties?
Values stored in an object.
Describe object literal notation.
var objNew = { prop1: value, prop2: value }
How do you remove a property from an object?
delete obj.property;
What are the two ways to get or update the value of a property?
Dot and bracket notation.
What are arrays used for?
Storing ordered lists of values.
Describe array literal notation.
var array = [0, 1, 2, 3];
How are arrays different from “plain” objects?
Entries inside the array are ordered and indexed by number instead of property names, new entries are added by pushing.
What number represents the first index of an array?
0
What is the length property of an array?
Property of the array that contains how many entries it currently holds.
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
A special type of object that can be called, containing reusable code.
Describe the parts of a function definition.
function keyword, optional name, zero or more parameters, code-block, and a return statement.
Describe the parts of a function call.
Function name, parentheses, arguments if any.
When comparing them side-by-side, what are the differences between a function call and a function definition?
Call passes arguments and executes the code, definitions only specify parameters and code.
What is the difference between a parameter and an argument?
Parameters are placeholders, arguments contain actual values.
Why are function parameters useful?
They can act as placeholder variables for the expressions you want to evaluate so you can reuse functions with different variables.
What two effects does a return statement have on the behavior of a function?
It produces a value and ends the function.
Why do we log things to the console?
To check their values.
What is a method?
A function contained in an object.
How is a method different from any other function?
A method usually involves manipulating or retrieving data from within the object and is called as a property of the object.
How do you remove the last element from an array?
array.pop()
How do you round a number down to the nearest integer?
Math.floor(num)
How do you generate a random number?
Math.floor(Math.random * (max - min + 1) + min)
How do you delete an element from an array?
array.pop() or array.shift()
How do you append an element to an array?
array.push()
How do you break a string up into an array?
string.split(separator)
Do string methods change the original string? How would you check if you weren’t sure?
No, log the string again to make sure.
Roughly how many string methods are there according to the MDN Web docs?
39 not including inheritance.
Is the return value of a function or method useful in every situation?
No.
Roughly how many array methods are there according to the MDN Web docs?
36 not including inheritance.
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.
Sloppy equality, strict equality, not sloppy equal, not strictly equal, greater than, greater than or equal to, less than, less than or equal to
What data type do comparison expressions evaluate to?
Booleans.
What is the purpose of an if statement?
To evaluate a condition and execute a code block if true.
Is else required in order to use an if statement?
No.
Describe the syntax (structure) of an if statement.
if (condition) {
code block
}
What are the three logical operators?
AND, OR, NEGATION
How do you compare two different expressions in the same condition?
Put a comparison operator between them.
What is the purpose of a loop?
Loops run the same block of code a certain number of times.
What is the purpose of a condition expression in a loop?
Condition evaluates whether or not the code block is run again.
What does “iteration” mean in the context of loops?
An iteration is an instance of the code block being run within the loop.
When does the condition expression of a while loop get evaluated?
The condition expression is evaluated before executing the statement.
When does the initialization expression of a for loop get evaluated?
Initialization happens before anything else.
When does the condition expression of a for loop get evaluated?
After the initialization/final expression and before the statement is executed.
When does the final expression of a for loop get evaluated?
After the code block is executed.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
Increments the variable by 1.
How do you iterate through the keys of an object?
for…in loops.
What is JSON?
JSON is a format for converting JS objects into strings of text.
What are serialization and deserialization?
Serialization is the process of converting an object in memory into a stream of bytes, deserialization is converting the bytes to an object in memory.
Why are serialization and deserialization useful?
They’re useful for transmitting JS objects and the data they contain to other platforms that may not support JS but can read JSON strings.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(object)
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(stringJSON)
How to you store data in localStorage?
localStorage.setItem(‘name’, ‘value’)
How to you retrieve data from localStorage?
localStorage.getItem(‘name’)
What data type can localStorage save in the browser?
Strings. Other data types are converted to Strings and Objects must be stringified first.
When does the ‘beforeunload’ event fire on the window object?
Before the page gets unloaded, ie. closed, clicked away from, or refreshed.
What is a method?
A function that exists as a property of an object.
How can you tell the difference between a method definition and a method call?
A method definition specifies what the method will do, a method call executes the code block within the method.
Describe method definition syntax (structure).
object.method = function () { codeblock };
Describe method call syntax (structure).
object.method();
How is a method different from any other function?
Methods are associated with objects.
What is the defining characteristic of Object-Oriented Programming?
The use of Objects that model entities and their interactions with each other as an organizing principle of code.
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, polymorphism.
What is “abstraction”?
Simplification of complex code by hiding unnecessary data to make it friendlier for users.
What does API stand for?
Application programming interface.
What is the purpose of an API?
APIs “communicate” between different pieces of software.
What is this in JavaScript?
this is an implicit parameter that returns the value of the object from which the function was called.
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 explicitly included in the parameter list.
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); } };
Nothing yet.
How can you tell what the value of this will be for a particular function or method definition?
You can’t as it has no value yet and you don’t know what object it’ll be called off when executed.
How can you tell what the value of this is for a particular function or method call?
See what object the function is being called off.
What kind of inheritance does the JavaScript programming language use?
Prototypal inheritance.
What is a prototype in JavaScript?
An object corresponding to a given type from which every instance of the object type will inherit its properties.
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?
They inherit them from their respective object prototypes.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
It will go up the prototype chain looking for the nearest matching property.
What does the new operator do?
It 1) creates a JS object 2) adds __proto__ property that links to the constructor function’s prototype object 3) binds the newly created object instance as the this context 4) returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype property
What does the instanceof operator do?
Checks if the item on the left is an instance of the object on the right.
What is a “callback” function?
A function that gets passed as an argument for another function.
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?
Passing the function to the setTimeout() or setInterval() functions.
How can you set up a function to be called repeatedly without using a loop?
Use the 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?
A positive integer ID unique to the timeout or interval.
What is AJAX?
A technique that allows developers to build asynchronous web apps (can retrieve and update information on page without reloading it).
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHTTPRequest
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
The ‘load’ event
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They have the prototype of EventTarget in their prototype chains.
What is Array.prototype.filter useful for?
Returning an array that filters out all elements of the initial array that did not match the predicate.
What is Array.prototype.map useful for?
Returning an array that applies the same function to every element in the input array.
What is Array.prototype.reduce useful for?
Returning a value that is obtained by reducing the array to a single result with a “reducer” callback function (e.g. summing all elements).