Javascript Flashcards
What is the purpose of variables?
variables are used to store data that can be accessed later
How do you declare a variable?
a keyword (like var) then the name of the variable
How do you initialize (assign a value to) a variable?
variable name = (assignment operator) value
What characters are allowed in variable names?
letters, numbers, $, _
What does it mean to say that variable names are “case sensitive”?
variables spelled with the same characters but have differences in capitalization are considered different
What is the purpose of a string?
stores data containing a series of characters
What is the purpose of a number?
used for calculations, determining the size, movement, or time associated with something
What is the purpose of a boolean?
defines whether something is true or false, for decisions
What does the = operator mean in JavaScript?
assignment operator
How do you update the value of a variable?
variableName = new value
What is the difference between null and undefined?
null is the intentional absence of value
Why is it a good habit to include “labels” when you log values to the browser console?
labels provide context to whatever you log in the console
Give five examples of JavaScript primitives.
strings, numbers, booleans, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combining a string with another value to create a new string
What purpose(s) does the + plus operator serve in JavaScript?
adds numbers and concatenates strings
What data type is returned by comparing two values ( > , < , ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the value to the right of the operator to the value on the left and assigns the result to the variable on the left
What are objects used for?
group together a set of related variables and functions
What are object properties?
variables that are part of an object
Describe object literal notation.
objectName = { property : value, … } ;
How do you remove a property from an object?
delete keyword followed by the objectName.propertyName
What are the two ways to get or update the value of a property?
dot or bracket notation
What are arrays used for?
arrays store a list of values that are related to each other
Describe array literal notation.
arrayName = [ value, … } ;
How are arrays different from “plain” objects?
the keys are index numbers, so there’s a specific order to the values
What number represents the first index of an array?
[ 0 ]
What is the length property of an array?
the number of entries in an array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
section of code that performs a task and can be reused
Describe the parts of a function definition.
function keyword, function name, ( ) with 0 or more parameters, { } code block, and and optional return statement
Describe the parts of a function call.
function name, ( ) containing the argument(s)
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition has a function keyword, parameters, { } containing a code block. function call only has the function name and arguments in ( )
What is the difference between a parameter and an argument?
parameters are for definition, arguments are for calls
Why are function parameters useful?
they allow different arguments to be passed through a function
What two effects does a return statement have on the behavior of a function?
return statements stop the code block from running and make the function produce a value
Why do we log things to the console?
to make sure our code is working properly and producing the expected results
What is a method?
a function that is a property of an object
How is a method different from any other function?
methods are a part of an object, they must be called with the object
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( ) * (range + 1) + start
How do you delete an element from an array?
.splice(index, # of items deleted)
How do you append an element to an array?
.push( )
How do you break a string up into an array?
.split( ‘ ‘ )
Do string methods change the original string? How would you check if you weren’t sure?
no, console.log(string) to check
Roughly how many string methods are there according to the MDN Web docs?
about 36
Is the return value of a function or method useful in every situation?
no, sometimes you don’t need a return value because it won’t be useful
Roughly how many array methods are there according to the MDN Web docs?
about 36
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?
booleans
What is the purpose of an if statement?
to run a code block if a condition is met
Is else required in order to use an if statement?
no, sometimes nothing needs to happen if a condition isn’t met
Describe the syntax (structure) of an if statement.
if (condition) { code block;}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
put a pair of ( ) around each individual expression and use a logical operator in between
What is the purpose of a loop?
loops run a block of code a certain amount of times while a condition is met
What is the purpose of a condition expression in a loop?
conditions determine whether a code loop is run, otherwise infinite loops occur
What does “iteration” mean in the context of loops?
each time a loops runs through a code block
When does the condition expression of a while loop get evaluated?
before each iteration of a loop
When does the initialization expression of a for loop get evaluated?
once before the loop starts
When does the condition expression of a for loop get evaluated?
before each iteration of a loop
When does the final expression of a for loop get evaluated?
after each iteration of a loop
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?
adds one to a value and either returns the updated value or returns the value then increments it
How do you iterate through the keys of an object?
for … in loops
What is JSON?
a text-based format following JS object syntax, used for transmitting data across a network
What are serialization and deserialization?
serialization turns an object to be turned into a format that can be sent across a network. deserialization converts it back into an object
Why are serialization and deserialization useful?
they allow objects to be turned into transferrable data and converted back
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?
.setItem(keyName, keyValue)
How to you retrieve data from localStorage?
.getItem(keyName)
What data type can localStorage save in the browser?
objects
When does the ‘beforeunload’ event fire on the window object?
fired when the window, document, and its resources are about to be unloaded
What is a method?
a function which is a property of an object
How can you tell the difference between a method definition and a method call?
a method definition has the function keyword, and a code block
Describe method definition syntax (structure).
function functionName(params) { code block};
Describe method call syntax (structure).
objectName.methodName(argument)
How is a method different from any other function?
methods are part of objects
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”?
being able to work with complex things in simple ways
What does API stand for?
application programming interface
What is the purpose of an API?
it is an abstraction, a set of tools that people can use
What is this in JavaScript?
this is an implicit parameter in all JS functions
What does it mean to say that this is an “implicit parameter”?
it’s 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? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
nothing, the function hasn’t been called yet
Given the above character object, what is the result of the following code snippet? Why? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; character.greet( );
it’s-a-me, Mario! this.firstName is Mario in this context
Given the above character object, what is the result of the following code snippet? Why? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } }; var hello = character.greet; hello( );
it’s-a-me, undefined! this is the window object, which doesn’t have a firstName property
How can you tell what the value of this will be for a particular function or method definition?
You can’t tell until call time
How can you tell what the value of this is for a particular function or method call?
It’s the object that the method is being called on
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
What is a prototype in JavaScript?
an object that contains properties and 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 objects, arrays, and numbers?
they get the method from its prototype object
If an object does not have its own property or method by a given key, where does JavaScript look for it?
the prototype object
What does the new operator do?
creates a new empty JS object, sets the __proto__ property of the object as the constructor function’s prototype, makes new object this, new returns object
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype
What does the instanceof operator do?
it tests to see if the prototype property of a. constructor appears anywhere in the prototype chain of an object
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( )
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, which executes immediately, or the next event cycle
What do setTimeout( ) and setInterval( ) return?
an id number that can be passed to clearInterval( ) or clearTimeout( ) to cancel the timer
What must the return value of myFunction be if the following expression is possible?
myFunction( )( );
another function
What does this code do? const wrap = value => ( ) => value;
returns a function that returns the value variable
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
when it is defined
What allows JavaScript functions to “remember” values from their surroundings?
closures