Javascript Flashcards
What is the purpose of variables?
To store values within them
How do you declare a variable?
var, let or const followed by variable name
How do you initialize (assign a value to) a variable?
Assignment operator =
What characters are allowed in variable names?
Letters, numbers, underscore, $
What does it mean to say that variable names are “case sensitive”?
Calling on the variable will require matching the case of all letters exactly
What is the purpose of a string?
Represents letters
What is the purpose of a number?
Represents numerical values
What is the purpose of a boolean?
Represents true or false, 1 or 0
What does the = operator mean in JavaScript?
Assignment operator
How do you update the value of a variable?
variable = new value
What is the difference between null and undefined?
Null means there is no value whereas undefined means the value is not declared yet
Why is it a good habit to include “labels” when you log values to the browser console?
To see what you are logging
Give five examples of JavaScript primitives.
number, string, boolean, null, undefined
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Combining multiple string inputs
What purpose(s) does the + plus operator serve in JavaScript?
To concatenate strings or to add numbers
What data type is returned by comparing two values (<, >, ===, etc)?
Boolean values
What does the += “plus-equals” operator do?
Add and assign
What are objects used for?
To group variables and functions
What are object properties?
Keys and values
Describe object literal notation.
Declare variable = {key: value, key: value}
How do you remove a property from an object?
delete object.key
What are the two ways to get or update the value of a property?
Dot or bracket notation, pair with assignment operator if updating
What are arrays used for?
To have a list of items
Describe array literal notation.
array name = [item, item, item];
How are arrays different from “plain” objects?
Uses brackets rather than curly braces, and indexes rather than keys
What number represents the first index of an array?
0
What is the length property of an array?
array.length
Returns total length of the array or total number of items within the list
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
A block of code that can be called on and executed
Describe the parts of a function definition.
function declaration, name, parameters, function code block
Describe the parts of a function call.
function name (argument)
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition will have “function” prior to the function name. Function call may include arguments whereas definition would have parameters
What is the difference between a parameter and an argument?
Parameters are placeholders whereas arguments are the actual data we want to use
Why are function parameters useful?
They act as placeholders that can be referenced within the function code block
What two effects does a return statement have on the behavior of a function?
Produces a value, exits the function
Why do we log things to the console?
To check our results
What is a method?
A function that is a property of an object
How is a method different from any other function?
It is property of an object. Functions don’t have a dynamic “this” binding
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?
Math.random() method
How do you delete an element from an array?
.pop(), .shift(), .splice()
How do you append an element to an array?
.push(), .unshift()
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?
No they are not referenced data types. They are immutable. You can check using console.log()
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, some returned values may be created to be used at a later time or we may just want it to execute an action without returning the results
Roughly how many array methods are there according to the MDN Web docs?
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?
Boolean
What is the purpose of an if statement?
To create a condition for executing code
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if(condition){
code
}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
Enclose each expression in parentheses and use an and/or operator
What is the purpose of a loop?
To execute code repeatedly until condition is met or code is broken
What is the purpose of a condition expression in a loop?
To define a stopping point for the loop or whether code should be executed
What does “iteration” mean in the context of loops?
Each cycle of the loop
When does the condition expression of a while loop get evaluated?
At the beginning of each iteration
When does the initialization expression of a for loop get evaluated?
Once before the loop begins
When does the condition expression of a for loop get evaluated?
At the start 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?
break
What does the ++ increment operator do?
Increase the variable by 1 and reassign the new value to the variable
How do you iterate through the keys of an object?
for (let key in object)
What is JSON?
String/Text-based data format that follows JavaScript object syntax
What are serialization and deserialization?
Serialization is turning objects into a string. Deserialization is the reverse where you convert a string into an object in memory
Why are serialization and deserialization useful?
They help us store and transmit data to and from servers/devices, and begin to collect/modify data again. They also give us an object that can be used across various languages
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
How do you store data in localStorage?
localStorage.setItem(key, value)
How do you retrieve data from localStorage?
localStorage.getItem(key)
What data type can localStorage save in the browser?
JSON string
When does the ‘beforeunload’ event fire on the window object?
When the window is in the process of being closed/refreshed and the DOM is cleared
What is a method?
A method is a function that is a property of an object
How can you tell the difference between a method definition and a method call?
A definition will be a property with a function assigned to it, whereas a method call will be the object name with the property called on it and ()
Describe method definition syntax (structure).
methodName: function () { example function }
Describe method call syntax (structure).
object.method()
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 contains data as properties and behavior as methods
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
Being able to work on complex things in simple ways
What does API stand for?
Application programming interface
What is the purpose of an API?
To allow multiple programs to communicate with each other
What is this in JavaScript?
An implicit parameter of all JS functions. It refers to the calling object; if none, it refers to the window object
What does it mean to say that this is an “implicit parameter”?
It is used in the code block but not called out as a parameter or declared
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?
You can’t tell until it is being called on
How can you tell what the value of this is for a particular function or method call?
By looking at the property & object being called on the ‘this’ parameter
What kind of inheritance does the JavaScript programming language use?
Prototype-based inheritance
What is a prototype in JavaScript?
An object with certain behaviors (methods) or data (properties) that can used by other objects
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
They have stored prototypes that automatically come with JavaScript
If an object does not have its own property or method by a given key, where does JavaScript look for it?
In the object prototype
What kind of inheritance does the JavaScript programming language use?
Prototype-based inheritance or prototypal inheritance
What is a prototype in JavaScript?
An object contains the functionality that you want to prototype on other objects. Or, an object with certain behaviors (methods) or data (properties) that can used by other objects
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
They inherit it from stored prototypes that automatically come with JavaScript
If an object does not have its own property or method by a given key, where does JavaScript look for it?
In the object prototype chain
What does the new operator do?
- Creates a blank JS object
- Points the new instance’s prototype to the constructor function’s prototype
- Execute the constructor function and ties ‘this’ to the new object
- If the object returns a non-primitive, then the return value will become the result of the new expression
- Primitives are boolean, string, numbers, undefined
- Non-primitives are everything else
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype
What does the instanceof operator do?
Returns a boolean on whether the object’s prototype chain includes the specified constructor
What is a “callback” function?
A function passed into another function as an argument
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?
setTimeout() - this will allow you to delay the code and have it executed once
How can you set up a function to be called repeatedly without using a loop?
setInterval() - in contrast to setTimeout(), the setInterval() function will repeat the code until it is stopped
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
A numeric timeout or interval ID to identify the timer/interval
What is a client?
The server requester of information
What is a server?
- A device that provides a service to another computer
- A resource or service provider
Which HTTP method does a browser issue to a web server when you visit a URL?
Get
What three things are on the start-line of an HTTP request message?
- HTTP method, a verb (GET, PUT, POST) or noun (HEAD, OPTIONS)
- Request target (URLs) and domain
- HTTP version to define structure of remaining message
What three things are on the start-line of an HTTP response message?
Protocol version, status code, status text
What are HTTP headers?
- case-insensitive name followed by colon : and its value
- Communicates to client and server what information to pass
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
Is a body required for a valid HTTP request or response message?
No, it will depend on the response
What is AJAX?
An asynchronous processing technique used to request and load data from a server without reloading a page
What does the AJAX acronym stand for?
Asynchronous JavaScript and XML
Which object is built into the browser for making HTTP requests in JavaScript?
new XMLHttpRequest()
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load or onload
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Prototype inheritance. An XMLHttpRequest is an object that supports events, therefore, able to have this functionality