JavaScript Flashcards
What is the purpose of variables?
To store data or information or values.
How do you declare a variable?
Using the variable keywords such as: ‘var’, ‘let’, or ‘const’
How do you initialize (assign a value to) a variable?
by using the assignment operator =
What characters are allowed in variable names?
letters, numbers, $, _, capitol letters. Do not start with a number.
What does it mean to say that variable names are “case sensitive”?
upper case and lower case matters.
What is the purpose of a string?
to store text values
What is the purpose of a number?
to store numbers
What is the purpose of a boolean?
there are only 2 values, true or false
What does the = operator mean in JavaScript?
assigns a value to a variable
How do you update the value of a variable?
assign the variable a new value
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.
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
Why is it a good habit to include “labels” when you log values to the browser console?
it is much clearer which variables are being logged and in what order.
Give five examples of JavaScript primitives.
strings, numbers, booleans, undefined, null
What data type is returned by an arithmetic operation?
a number
What is string concatenation?
combining multiple strings into 1 string
What purpose(s) does the + plus operator serve in JavaScript?
to add numbers, or to concatenate strings
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds more to a variable then reassigns it. called addition assignment
What are objects used for?
Object group together variables and functions to create a model of something from the real world.
What are object properties?
variables inside an object
Describe object literal notation.
Name/key/property is separated from value by a colon.
Name/key/property-value pairs are separated by commas.
No comma follows the last name/key/property-value pair.
How do you remove a property from an object?
by using the delete keyword.
delete objectName.propertyName
What are the two ways to get or update the value of a property?
dot notation and bracket notation
What are arrays used for?
storing a list of values in a variable
Describe array literal notation.
var array = [item1, item2, item3, etc ]
How are arrays different from “plain” objects?
arrays have index numbers(starting at 0) instead of keys/property names.
What number represents the first index of an array?
0
What is the length property of an array?
.length returns how many things are in the array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
Functions are a special kind of object that is “callable”. The code inside the function runs when the function is “called”.
Describe the parts of a function definition.
the function keyword,
an optional name,
zero or more parameters,
a code block,
an optional return statement
Describe the parts of a function call.
The function’s name. A comma-separated list of zero or more arguments surrounded by () parentheses.
When comparing them side-by-side, what are the differences between a function call and a function definition?
The function definition contains the code.
Calling a function does NOT use the “function” keyword.
What is the difference between a parameter and an argument?
A parameter is like a placeholder. When we define a function, we declare parameters and that when we call a function, we pass it arguments.
Why are function parameters useful?
Parameters allow a function to perform tasks without knowing the specific input values ahead of time.
What two effects does a return statement have on the behavior of a function?
A return statement causes the function to produce a value.
A return statement also exits the function; no code after the return statement is executed.
Why do we log things to the console?
To check if our code is working as intended.
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?
methods are associated with objects.
How do you remove the last element from an array?
calling the pop() method.
array.pop()
How do you round a number down to the nearest integer?
calling the floor() method of the Math object.
Math.floor()
How do you generate a random number?
calling the random() method of the Math object
Math.random()
How do you delete an element from an array?
calling the splice() method with 2 arguments, the first argument is the index at which to start removing elements, and the second argument is how many elements to remove. Adding a third argument will place the third argument at first argument index.
How do you append an element to an array?
calling the push() method will append one or more elements to the end of an array.
using the unshift() method will append one or more elements to the beginning of an array.
How do you break a string up into an array?
calling the split() method
Do string methods change the original string? How would you check if you weren’t sure?
No,
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.
Roughly how many array methods are there according to the MDN Web docs?
40
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 run code if a condition is met.
Is else required in order to use an if statement?
No.
Describe the syntax (structure) of an if statement.
keyword ‘if’ (CONDITION HERE) {
CODE BLOCK
}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
&& or ||
What is the purpose of a loop?
To run the same code more than once without having to write it more than once.
What is the purpose of a condition expression in a loop?
To tell the loop when to stop.
What does “iteration” mean in the context of loops?
everytime a loop completes a cycle
When does the condition expression of a while loop get evaluated?
at the start of each iteration.
When does the initialization expression of a for loop get evaluated?
At the start of the loop.
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?
break
What does the ++ increment operator do?
increments value by 1
How do you iterate through the keys of an object?
for in loop
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?
the function keyword
Describe method definition syntax (structure).
{property: function (param) {
code block
}}.
Describe method call syntax (structure).
object.property(arguments)
How is a method different from any other function?
Function — a set of instructions that perform a task. Method — a set of instructions that are associated with an object.
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
boils down to being able to work with (possibly) complex things in simple ways.
What does API stand for?
Application Programming Interface.
What is the purpose of an API?
to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
What is this in JavaScript?
this is an implicit parameter of all JavaScript functions. In the most basic sense, this refers to the object its in, or the window object.
What does it mean to say that this is an “implicit parameter”?
capable of being understood from something else though unexpressed
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 window object, because its not being called.
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
“It’s-a-me, Mario!” because its being called by the character object.
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
“It’s-a-me, undefined!” because
How can you tell what the value of this will be for a particular function or method definition?
You can’t because the this value is determined when it is being called.
How can you tell what the value of this is for a particular function or method call?
based on which object is calling it. left of the dot(.).
What kind of inheritance does the JavaScript programming language use?
JavaScript includes a specific kind of inheritance known as prototype-based (or prototypal) inheritance. JavaScript objects give certain behaviors (methods) or data (properties) to other objects.
What is a prototype in JavaScript?
a JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be 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?
Those methods are defined on a “prototype” object and arrays, strings, and numbers simply borrow those methods when they’re needed.
Inherited from the prototype chain.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
the object’s prototype.
Prototype chain.
What does the new operator do?
- Creates a blank, plain JavaScript object. For convenience, let’s call it newInstance.
- Points newInstance’s [[Prototype]] to the constructor function’s prototype property, if the prototype is an Object. Otherwise, newInstance stays as a plain object with Object.prototype as its [[Prototype]].
Note: Properties/objects added to the constructor function’s prototype property are therefore accessible to all instances created from the constructor function. - Executes the constructor function with the given arguments, binding newInstance as the ‘this’ context (i.e. all references to this in the constructor function now refer to newInstance).
- If the constructor function returns a non-primitive, this return value becomes the result of the whole new expression. Otherwise, if the constructor function doesn’t return anything or returns a primitive, newInstance is returned instead. (Normally constructors don’t return a value, but they can choose to do so to override the normal object creation process.)
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
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 passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
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, delay, param1, param2, etc)
How can you set up a function to be called repeatedly without using a loop?
setInterval(function, delay, param1, param2, etc)
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
If this parameter is omitted, a value of 0 is used, meaning execute “immediately”, or more accurately, the next event cycle.
What do setTimeout() and setInterval() return?
ID’s used to cancel the respective timers using clearTimeout(“respectiveID”) and clearInterval(“respectiveID”)
What is AJAX?
AJAX is a technique for loading data into part of a page without having to refresh the entire page (Asynchronous). The data is often sent in JSON format (JavaScript Object Notation)
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 is fired when an XMLHttpRequest transaction completes successfully.
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototypal inheritance. EventTarget prototype.