JavaScript Flashcards
What is the purpose of variables?
Data storage for scripts to call back to
How do you declare a variable?
They are declared with
- var
- let
- const
How do you initialize (**assign a value to) a variable?
Variables are initialized by including the assignment operator ‘=’
example: var example = true
What characters are allowed in variable names?
- letters
- numbers (CANNOT be the first character of a variable name)
- underscores(_)
- dollar signs
What does it mean to say that variable names are “case sensitive”?
Variables names must follow the same casing when used later in a script, so the variables “fullName” and “FullName” would be detected as two different variables
What is the purpose of a string?
To store text (a sequence of characters)
What is the purpose of a number?
To store a piece of numerical data (for math/calculations)
What is the purpose of a boolean?
To store a state of something is or is not
What does the = operator mean in JavaScript?
This is the assignment operator, it initializes variables and can re-assign their values.
How do you update the value of a variable?
Re-assign it with the assignment operator
What is the difference between null and undefined?
Null
is a value explicitly assigned to not existUndefined
is a default value set to variables that have not been assigned
Give five examples of JavaScript primitives.
- Number
- Boolean
- String
- Undefined
- Null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
The adding of a string to the end of another string
The result is always a string.
What purpose(s) does the + plus operator serve in JavaScript?
It tells values to combine by either concatenating or adding
What data type is returned by comparing two values (<, >, ===, etc)?
Boolean (true/false)
What does the += “plus-equals” operator do?
Addition assignment operator: adds the value of the right operand to a variable and assigns the result to the variable. Can be used to add or concatenate.
What are objects used for?
They are used to group related variables and functions
What are object properties?
Related variables/functions stored in an object become a property/method of the object
Describe object literal notation.
list out the properties and methods within curly braces and assign it to a variable
{ hasProperty: true, name: 'example', };
What are the two ways to get or update the value of a property?
Dot notation and bracket notationexampleObject.name'
-dot notationexampleObject['name']
-bracket notation
What are arrays used for?
They are used to store lists of data where the order is important or unimportant
Note: Unlike objects the key that the variables are paired to are numbers as they are indexed.
Describe array literal notation.
A list contained in two square-brackets ([ ]) with each value separated with commas
How are arrays different from “plain” objects?
The key that the variables are paired to are numbers and not properties. They are accessed with bracket-notation unless using a method.
(ex.)console.log(exampleArray[0])
What number represents the first index of an array?
0
What is the length
property of an array?
A property that returns the length of the array/object
How do you calculate the last index of an array?
Subtract the array length by 1
This is due to the array indexing the variables stored within using zero-based counting
(ex.)var lastIndex = exampleArray.length - 1;
What is a function in JavaScript?
A special object that can run a code block as many times as it is called
Describe the parts of a function definition
includes the keyword function, a name for the function (optional), parameters separated by commas (if any), the code block, and the return statement (optional)
(ex.)
function exampleFunction(word) { var completedPhrase = 'Hello, ' + word; return completedPhrase; }
Describe the parts of a function call
includes the name for the function followed by any arguments in parentheses
*(ex.) exampleFunction(arg1, arg2)
What are the differences between a function call and a function definition
A function call only includes the function name and any arguments instead of parameters
A function definition includes the keyword function and the name, as well as parameters instead of arguments, and a code block to be run
What is the difference between a parameter and an argument
A parameter is used to define a function, while an argument is a real number passed through the function.
6 examples of comparison operators
- < less than
- > greater than
- <= less than or equal to
- > = greater than or equal to
- === strictly equal to
- !== strictly not equal to
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if
statement?
To allow a specific condition to determine whether to run a code block or not
Is else
required in order to use an if
statement?
No, else is optional
Describe the syntax (structure) of an if
statement
if (condition) { }
What are the three logical operators?
- Or (||)
- And (&&)
- Not (!)
How do you compare two different expressions in the same condition?
Using a logical operator
Why do we log things to the console?
Debugging purposes, if we can see what value a variable is at different sections of the code, we can figure out where things went wrong.
What is a method?
A function that is stored as a property in an object
How is a method different from any other function?
It is associated with the object it is a method of
How do you remove the last element from an array?
the .pop() method, which is implicit to arrays
How do you round a number down to the nearest integer?
Math.floor()
The floor method of the Math object will take a value passed as an argument and round it down.
Note: optional parameters allow for changing the exponent and type of adjustment
How do you generate a random number?
Math.random()
The random method of the Math object generates a pseudo-random float value between 0 and 1.
How do you delete an element from an array?
arrayName.pop(); arrayName.shift(); arrayName.splice();
Can be used to delete an element from an array
How do you append an element to an array?
arrayName.push();
How do you break a string up into an array?
.split() - will split a string into an array based on the arguments passed through
Do string methods change the original string? How would you check if you weren’t sure?
No they do not. Strings are immutable. If one wanted to check one could log the string before and after using a method to see if it changed after the method was applied.
Is the return value of a function/value useful in every situation?
No, if what we need is the original data processed and not whatever we are calculating/extracting
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
What is the purpose of a loop?
To re-run a code block.
One use: To run code block for each value in an array
What is the purpose of a condition expression in a loop?
To specify when a loop should stop
What does “iteration” mean in the context of loops?
Each pass of the code block
“This is the nth time the loop has run”
When does the condition expression of a while
loop get evaluated?
It runs before each code block iteration
When does the initialization expression of a for
loop get evaluated?
Before anything and only before the first iteration
When does the condition expression of a for
loop get evaluated?
After the initialization and before each iteration of the code block
When does the final expression of a for
loop get evaluated?
After 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?
Increases value by 1
How do you iterate through the keys of an object?
A for…in loop
What is JSON?
JavaScript Object Notation(JSON) is a string-based format for representing data/data structures
What are serialization and deserialization?
Serialization is the conversion of a data structure into a string of bytes for the sake of storage or transporting it between systems.
Deserialization is the conversion of those strings of bytes back into a copy of the original data structure in the same state.
Why are serialization and deserialization useful?
It allows data structures to be sent between differing systems and formats while still maintaining what that data structure is actually representing.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(exampleDataStructure)
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(exampleJSONString)
How do you store data in localStorage
?
`window.localStorage.setItem(‘key’, ‘value’);
How do you retrieve data from localStorage
?
window.localStorage.getItem('key');
What data type can localStorage
save in the browser?
String
is the data type it can save, specifically in UTF-16
When does the 'beforeunload'
event fire on the window
object?
It fires right before all contents on the page are about to be unloaded. (i.e closing the tab/refreshing)
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 method definition includes the code that makes the function work as intended.
A method call only contains the function name and whatever arguments necessary.
Describe method definition syntax (structure).
A method definition is stored as a property, then its value is the function keyword with any desired parameters then the code block.
Example:
var exampleObject = { exampleMethod: function (parameterA, parameterB) { } };
Describe method definition syntax (structure).
A method definition is stored as a property, then its value is the function keyword with any desired parameters then the code block.
Example:
var exampleObject = { exampleMethod: function () { return 'This is a method of the exampleObject object.'; } };
Describe method call syntax (structure).
A method call is accessed with dot notation. Example:
exampleObject.exampleSum(1 , 2);
How is a method different from any other function?
It exists inside an object. It can only be called with the object.
What is the defining characteristic of Object-Oriented Programming?
It uses objects which store both data and behavior.
What are the four “principles” of Object-Oriented Programming?
What is “abstraction”?
Dealing with complex problems with simple means/simplifying an otherwise complex process into easy steps.
What does API stand for?
Application Programming Interface
What is the purpose of an API?
It gives programmers a way to interact with a system in a simplified way.
What is this
in JavaScript?
this
is an implicit parameter
What does it mean to say that this
is an “implicit parameter”?
It means that even if it is not explicitly stated it implicitly exists.
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); } };
This
refers to nothing.
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It's -a-me, Mario!
will be the result. This is because of the this
keyword being called in the greet method referencing the character.firstName property.
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!
will be the result. This is because the this
keyword cannot reference the character object since it is being assigned to a new variable, the method is not being called off the object. This
is referencing the window object.
How can you tell what the value of this will be for a particular function or method definition?
You can’t since the method/function isn’t being called.
How can you tell what the value of this is for a particular function or method call?
Object to the left of the dot of the method call, if none is present it defaults to the window object.
What kind of inheritance does the JavaScript programming language use?
It uses prototype-based (prototypal) inheritance.
What is a prototype in JavaScript?
It is an object which stores general functionality that can be shared with many different objects that are patterned after it/similar to it.
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?
Each of those methods are stored on the prototype object of that kind of data type.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
Within the prototype object of that data type.
What does the new operator do?
It creates a new instance of an object with the same [[Prototype]] as the specified constructor, executes the function with whatever arguments have been passed through to allow this
to have a context of the new instance of the object, and then returns the object from the constructor function.
What property of JavaScript functions can store shared behavior for instances created with new?
The prototype
property.
What does the instanceof operator do?
It checks if an object is an instance of a constructor function ( does the object use the same prototype )
What is a “callback” function?
A function that is passed as an argument of 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?
setTimeout()
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
They return unique IDs that can be used inside of clearTimeout()
or clearInterval()
What is AJAX?
A group of technologies used to create asynchronous web applications
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?
load
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Both are objects.
What is Node.js?
A set of JavaScript libraries that allows it to be used outside of the browser.
What can Node.js be used for?
It is used for backend such as network clients and servers.
What is a REPL?
It stands for Read-eval-print-loop, it basically takes what is input and evaluates what was input then prints out the result.
When was Node.js created?
2009 by Ryan Dahl
What is a computer process?
A collected list of instructions being executed by the computer. It can be viewed as the environment controlled by the OS in which a program is run, with memory allocated and CPU time allocated.
What is the process object in a Node.js program?
A global object representing the node
process being run.
How do you access the process object in a Node.js program?
One can directly access the process object within their REPL or actual files
What is the data type of process.argv in Node.js?
It is an array
What is a JavaScript module?
A file that has functions/variables that can be exported to be processed in other files
What values are passed into a Node.js module’s local scope?
exports, require, module, __filename, __dirname
What is the JavaScript Event Loop?
It is a mechanism in which asynchronous tasks are added to a queue and is executed if the stack is empty. (Stack is like the “job list” for the synchronous tasks)
What is different between “blocking” and “non-blocking” with respect to how code is executed?
Blocking: Each step must be finished in the order the code is called
Non-blocking: The order in which steps are executed can be deferred/changed
What method is available in the Node.js fs module for writing data to a file?
writeFile()
Are file operations using the fs module synchronous or asynchronous?
Asynchronous
What are the three states a Promise can be in?
- pending (waiting for an async task to complete)
- fulfilled (successfully completed task)
- rejected (failed to complete task)
How do you handle the fulfillment of a Promise?
with .then()
How do you handle the rejection of a Promise?
with .catch()
What is Array.prototype.filter
useful for?
It’s useful for filtering an array to get the values that obey certain conditions. (NOTE: returns a copy instead of changing original)
What is Array.prototype.map
useful for?
It’s useful for modifying every value in the same manner and returning a copy.
What is Array.prototype.reduce
useful for?
Combining related data (ie. account balances)
What does fetch() return?
It returns a promise that resolves to the Response
object representing the response to our request from the specified resource.
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
One would specify it in the method property of the optional options
parameter.