JavaScript Flashcards
What is the purpose of variables?
Variables can hold any data value and can changed anytime
How do you declare a variable?
You declare using var (keyword) and variable name
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
Letter, dollar sign, and underscore to start and numbers
What does it mean to say that variable names are “case sensitive”?
First letter shouldn’t be capitalize and every letter capitalized after is different string value than the non-capitalized string values.
What is the purpose of a string?
To hold values as letters/text and change
What is the purpose of a number?
To hold values as numbers for arithmetic
What is the purpose of a boolean?
To hold values of true or false for decision making
What does the = operator mean in JavaScript?
Putting a value into something
How do you update the value of a variable?
Assign a new value
What is the difference between null and undefined?
They both represent empty value but null is empty on purpose - it has to be assigned to something while undefined is not assigned a value.
Why is it a good habit to include “labels” when you log values to the browser console?
Gives us point of reference
Give five examples of JavaScript primitives
String, boolean, number, null, undefined, symbol
What data type is returned by an arithmetic operation?
numeric data type
What is string concatenation?
Process of joining together two or more strings
What purpose(s) does the + plus operator serve in JavaScript?
Addition and string concatenation
What data type is returned by comparing two values (, ===, etc)?
Boolean data type
What does the += “plus-equals” operator do?
adds the value of the additional data value to a variable and assigns the result to the variable
What are objects used for?
Object groups a set of variables(properties) and functions(methods) to create a model.
What are object properties?
Variables that are part of the object
Describe object literal notation.
{} with whatever properties : method name
How do you remove a property from an object?
with the delete operator
What are the two ways to get or update the value of a property?
Dot or bracket notations
What are arrays used for?
Array is used to store list of values and indexed with set order
Describe array literal notation.
Array literal notation is using [] to list with data separated by commas
How are arrays different from “plain” objects?
Arrays are list with set order
What number represents the first index of an array?
[0]
What is the length property of an array?
It shows the total number of items in the array
How do you calculate the last index of an array?
[length - 1]
What is a function in JavaScript?
Set of statement grouped to perform a specific task and be repeatable
Describe the parts of a function definition.
Function keyword is used to create a new function followed by optional name and () then {} and optional return statement
Describe the parts of a function call.
Name of function you want to call with parentheses and arguments if needed
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition does not allow the code block to be run while function call allows function to be run. Definition requires {} for code block while call requires () for possible argument
Why are function parameters useful?
Function parameters are useful when replacing values to a function
Why do we log things to the console?
To check our mistakes in javascript by checking the console output and help debug
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?
function can be called directly while method uses dot notation with object name and method name
How do you remove the last element from an 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?
splice() method
How do you append an element to an array?
push()
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?
String method cannot be changed. You check by trying it and look for online source like MDN
Roughly how many string methods are there according to the MDN Web docs?
a lot
Is the return value of a function or method useful in every situation?
No, it is not necessary when a return is already given
Roughly how many array methods are there according to the MDN Web docs?
thirty
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.
=== (strictly equal to), != (is not equal to), >(greater than), !==, >=, <= (less than or equal to)
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
It helps in decision-making
Is else required in order to use an if statement?
No, else is alternative, but not everything needs alternative
Describe the syntax of an if statement.
if () {}
What are the three logical operators?
&& (logical and), || (logical or), ! (logical not)
How do you compare two different expressions in the same conditions?
using logical operator
What is the purpose of a loop?
Purpose of a loop is to repeat the same code number of times until it is false
What is the purpose of a condition expression in a loop?
to determine to keep going or stop the loop
What does “iteration” mean in the context of loops?
every time the code block runs
When does the condition expression of a while loop get evaluated?
beginning of the loop and each iteration
When does the initialization expression of a for loop get evaluated?
at the start of the loop once
When does the condition expression of a for loop get evaluated?
before each iteration
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 the value of the variable
How do you iterate through the keys of an object?
for in loops
What is the difference between a parameter and an argument?
look it up
What two effects does a return statement have on the behavior of a function?
look it up
What is the event.target?
stores where the object originated from
What is the affect of setting an element to display: none?
The element disappears and no longer take space on the web page
What does the element.matches()
a
How can you retrieve the value of an element’s attribute?
getAttribute()
At what steps of the solution would it be helpful to log things to the console?
verify the values of the variables, logging the values of the attributes
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?
we’d have to add an event listener to every element
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?
write a conditional instead for each view
What is JSON?
JSON stands for JavaScript Object Notation and is a format to store and transport data. Data exchange format that is a string.
What are serialization and deserialization?
Serialization is the process of of turning an object in memory into a stream of bytes so you can do store on hardware or send it over the network. Deserialization is the opposite by turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
They are useful in exchanging data: you can send objects over a network and it can be parsed by the reciever
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify - converts a JavaScript object or value to a JSON string
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse - data becomes a JavaScript object
How do you store data in localStorage?
Storage.setItem() with key: string; value: any data type
How do you retrieve data from localStorage?
Storage.getItem() with key as argument
What data type can localStorage save in the browser?
string
When does the ‘beforeunload’ event fire on the window object?
It is fired when the window, document, and its resources are about to be unloaded.
What is a method?
A method is a function which is a property of an object function stored in a property
How can you tell the difference between a method definition and a method call?
property: function() for definition
object. method()
Describe method definition syntax (structure).
property: function() for definition
Describe method call syntax (structure).
object.method()
What is the defining characteristic of Object-Oriented Programming?
OOP allows you to pair data and behavior
What are the four “principles” of Object-Oriented Programming?
Encapsulation, Abstraction, Inheritance, and Polymorphism
What is “abstraction”?
Working with complex things in simple ways by focusing on details of greater importance.
What does API stand for?
Application Programming Interface
What is the purpose of an API?
A set of functions that allows applications to access data and interact with external software components, operating systems, or microservices. An API delivers a user response to a system and sends the system’s response back to a user.
How is a method different from any other function?
Methods are functions attached to objects
What is “this” in JavaScript?
It’s the object that is acting on
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 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
What does “this” refer to in the following code snippet?
Nothing but act as a placeholder
Given the above character object, what is the result of the following code snippet? Why?
Mario
Given the above character object, what is the result of the following code snippet? Why?
undefined because we’re calling window.hello() and does not have property
How can you tell what the value of “this” will be for a particular function or method definition?
You can’t know
How can you tell what the value of “this” is for a particular function or method call?
left of the dot
What kind of inheritance does the JavaScript programming language use?
prototypal inheritance
What is a prototype in JavaScript?
Mechanism that allows JavaScript objects to inherit features from one another
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?
setPrototypeOf()
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
proto
What does the new operator do?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
to test if an object is of a given type
What is a “callback” function?
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 lister 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() and setInterval() return?
0
What do setTimeout() and setInterval() return?
a timeoutID which is a positive integer identifying the timer created as a result of calling the method. IntervalID which will be an integer value
What is AJAX?
AJAX allows you to make asynchronous calls to a web server - this allows client browser to avoid waiting for all data to arrive before allowing the user to act once more.
The main purpose of AJAX is to increase speed, performance, usability of a web application.
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?
load event
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
they share the same prototype property (event.target.prototype)
What is the JavaScript Event Loop?
event loop checks if the stack is empty. it takes the first item on the queue and pushes to the stack. Checks to see if JavaScript is not busy
What is different between “blocking” and “non-blocking” with respect to how code is executed?
blocking would be synchronous and blocking is code that is currently blocking the call stack and non-blocking would be non-synchronous
What is Array.prototype.filter useful for?
When you only want some of the data
What is Array.prototype.map useful for?
t is useful when you want to update or change the values inside the array list
What is Array.prototype.reduce useful for?
It is useful when we want single output from array
What must the return value of myFunction be if the following expression is possible? myFunction()();
myFunction returns another function and that returned function is what’s getting called -first () invokes first function and second () invokes second function
What does this code do? const wrap = value => () => value;
the value of inner function
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
When it is defined because when function is declared by code block the scope is determined
What allows JavaScript functions to “remember” values from their surroundings?
lexical environment: closure