JavaScript ES5 Flashcards
What is the purpose of variables?
Store data for future access
How do you declare a variable?
var quantity;
How do you initialize (assign a value to) a variable?
quantity = 3;
What characters are allowed in variable names?
Letters
Numbers (cannot start with)
$
_
What does it mean to say that variable names are “case sensitive”?
Variables are stored with case sensitive names
What is the purpose of a string?
Represent text
What is the purpose of a number?
Mathematical operations
What is the purpose of a boolean?
Used with conditional logic to run specific blocks of code
What does the = operator mean in JavaScript?
Assignment operator assigns values to variables
How do you update the value of a variable?
let quantity = 3;
//reassign quantity = 7;
What is the difference between null and undefined?
null:
a non-existent value that is intentionally assigned by the user, usually a placeholder value
undefined:
a non-existent value that is automatically assigned by JavaScript if no value is given
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?
Combination of at least one string value with another string value or primitive value
What purpose(s) does the + plus operator serve in JavaScript?
Addition
Concatenation
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
Addition Assignment:
Adds the left value with the right value, and the result of this expression is reassigned to the left
What are objects used for?
Encapsulating characteristic data by key value pairs
What are object properties?
Variables inside of an object
Describe object literal notation.
var obj = {};
How do you remove a property from an object?
delete hotel.booked;
What are the two ways to get or update the value of a property?
Dot Notation:
vehicle.color = black
Bracket Notation:
vehicle[“color”] = “black”
What are arrays used for?
Store a list of numerically, zero based indexed data, where order may matter
Describe array literal notation.
var array = [];
How are arrays different from “plain” objects?
- Numerical Indexes
- Ordered
- Special methods to modify array values (push, pop, shift, unshift, splice, etc)
What number represents the first index of an array?
0
What is the length property of an array?
Returns the number of array entries
How do you calculate the last index of an array?
arr[arr.length – 1]
What is a function in JavaScript?
A set of reusable code
Describe the parts of a function definition.
let endOfSentence = ‘something’
function example (params) { return 'I am returning ' + params; }
Describe the parts of a function call.
example(endOfSentence)
“I am returning something”
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call:
- No code block
- arguments
Function Definition:
- function keyword
- Code block
- return statement
- parameters
What is the difference between a parameter and an argument?
Parameter exist in function definitions and are placeholders (variables)
Argument are passed into a function’s parameters in a function call as values
Why are function parameters useful?
Allow for dynamic data and reusability
What two effects does a return statement have on the behavior of a function?
Returns the output of the function
Immediately exits the function
How is a method different from any other function?
Methods are attached to objects
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();
How do you generate a random number?
Math.random();
How do you delete an element from an array?
array. splice(start index, [delete count], [replacement]);
array. spice(3, 1)
How do you append an element to an array?
array.push();
How do you break a string up into an array?
string.split();
Do string methods change the original string? How would you check if you weren’t sure?
String methods DO NOT change the original string
Check by console.log the original string after using the method
Is the return value of a function or method useful in every situation?
No
Give 6 examples of comparison operators.
> (gt) < (lt) <= (lte) >= (gte) === (strictly equal) !== (not strictly equal)
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Take decisions on code based on if a specified condition is truthy, if falsy the else block can run
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (condition) { //code block to run if condition is met }
What are the three logical operators?
&& (and)
|| (or)
! (not)
How do you compare two different expressions in the same condition?
|| (or)
&& (and)
What is the purpose of a loop?
To automate tasks to a set end point determined by a condition
What is the purpose of a condition expression in a loop?
The condition expressions tells the loop when to stop
What does “iteration” mean in the context of loops?
Iteration refers to when the code inside the curly braces run
When does the condition expression of a while loop get evaluated?
Prior to each pass through the loop
When does the initialization expression of a for loop get evaluated?
Before anything
When does the condition expression of a for loop get evaluated?
Before each loop iteration
When does the final expression of a for loop get evaluated?
End of each iteration, and before evaluation
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break statement
What does the ++ increment operator do?
Adds one, after the value is read
How do you iterate through the keys of an object?
for in statement:
e.g. for(var key in object)
Get Values:
Insert the variable in brackets pointing at the object
e.g. object[variable]
What event is fired when a user places their cursor in a form control?
Focus
What event is fired when a user’s cursor leaves a form control?
Blur
What event is fired as a user changes the value of a form control?
Input
What event is fired when a user clicks a button type “submit” within a form?
Submit
What does the event.preventDefault() method do?
Prevents the event’s default behavior
What does submitting a form without event.preventDefault() do?
Reloads the page
What property of a form element object contains all of the form’s controls.
$form.elements
What property of a form control object gets and sets its value?
event.target.value
What is the event.target?
The node of where the event occurred
What is the affect of setting an element to display: none?
Removes element from the document flow
What does the element.matches() method take as an argument and what does it return?
Argument: CSS selector
Returns Boolean
How can you retrieve the value of an element’s attribute?
$element.getAttribute(“attrName”)
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?
Multiple event listeners addressing each tab and view
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?
Multiple conditions for the various indexes
What is JSON?
Text-based data format following the object syntax that can be independently created and stored apart from JavaScript.
What are serialization and deserialization?
Serialization is the process of converting an object to a stream of bytes so it can be stored or sent over the network (back end for example).
Deserialization is the process of converting the stream of bytes back into an object in memory.
Why are serialization and deserialization useful?
It allows developers to share and store data
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
How to you store data in localStorage?
localStorage.setItem(‘keyName’, keyValue);
How to you retrieve data from localStorage?
var item = localStorage.getItem(‘keyName’);
What data type can localStorage save in the browser?
String
When does the ‘beforeunload’ event fire on the window object?
Before the user navigates away from the page, or reloads the page
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?
How can you tell the difference between a method definition and a method call?
Method Definition: function keyword, code block
Method Call:
object.method()
Describe method definition syntax (structure).
Method name & Colon
Function keyword & Optional parameters
Code block with return statement
Comma (if following methods or properties)
Describe method call syntax (structure).
Method name of the object with zero or more arguments
object.method()
How is a method different from any other function?
A method is different from any other function because it exists within the object’s scope
What is the defining characteristic of Object-Oriented Programming?
Objects that can hold data and behavior
What are the four “pillars” of Object-Oriented Programming?
Abstraction - the concept of wrapping up complex actions in simple methods
Encapsulation - keep state and logic internal
Inheritance - Classes can have parent classes. Child classes will inherit all of the behavior and attributes of the parent class
Polymorphism - we can call the same method on different objects
What is “abstraction”?
The omission of complex functionality or details to simplify and create further emphasis on other things that may be more important.
What does API stand for?
Application Programming Interface – the connection between computers or between computer programs.
What is the purpose of an API?
Abstract information of how a system works, and only showing relevant information for the programmer
What is this in JavaScript?
“This” is a keyword implicit parameter that refers to the object it’s within at call time
What does it mean to say that this is an “implicit parameter”?
“this” 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?
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?
Method: this will refer to the object at the left of the dot at call time.
Function: this will refer to the window if there is no dot or object at call time
What kind of inheritance does the JavaScript programming language use?
Prototypal inheritance
What is a prototype in JavaScript?
The original template object that other objects can 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?
It is included in the data type’s prototype
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
Looks into the object’s prototype
What does the new operator do for an Object constructor?
- Creates empty object
- Binds __proto__ to the new instance
- Binds “this” to the newly created object
- 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?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
What is a “callback” function?
A callback function is a function which is:
- accessible by another function, and
- is invoked after the first function if that first function completes
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() function will delay execution based on the milliseconds, added as second argument
How can you set up a function to be called repeatedly without using a loop?
setInterval() can be used to repeatedly call without a loop
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
timeoutId
intervalId
What is a client?
A requester of a service
What is a server?
The providers of a resource or service
Always waiting for client requests
Which HTTP method does a browser issue to a web server when you visit a URL?
GET method
What three things are on the start-line of an HTTP request message?
- HTTP method (GET, PUT, POST, HEAD, OPTIONS)
- Request Target (usually URL, or absolute path)
- Protocol version (indicator of the expected version to use for the response)
What three things are on the start-line of an HTTP response message?
Status Line
- Protocol version (HTTP/1.1)
- Status Code (success or failure codes)
- Status text (HTTP/1.1 404 Not Found.)
What are HTTP headers?
HTTP headers let the client and the server pass additional information with an HTTP request or response.
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN docs on HTTP Headers
Is a body required for a valid HTTP request or response message?
No
What is AJAX?
The use of XMLHttpRequest to communicate with servers to send or retrieve data
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 Object
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
Load event
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They both have the EventTarget prototype that contains the addEventListener method