JavaScript Flashcards
What is the purpose of variables?
a container used to store data so that you can use it later
How do you declare a variable?
using let, const, or var keyword and then giving it a name in camelCase
How do you initialize (assign a value to) a variable?
using = e.g.(var x = 2)
What characters are allowed in variable names?
letters, numbers, $ sign, and underscore
What does it mean to say that variable names are “case sensitive”?
two variables can have the same name but different values if their first letter is capitalized and uncapitalized
What is the purpose of a string?
used to store and represent text information
What is the purpose of a number?
used to store and represent numerical values and do math
What is the purpose of a boolean?
used for conditional statements since booleans can only return two values, either true or false. Helps the program make a decision.
What does the = operator mean in JavaScript?
assignment, usually when assigning a value to a variable
How do you update the value of a variable?
using the assignment operator again, don’t need to use a var keyword the second time
What is the difference between null and undefined?
null is purposeful emptiness, undefined is not purposeful emptiness. Null is declared by the developer, whereas undefined is declared by the computer.
Why is it a good habit to include “labels” when you log values to the browser console?
to make it clearer what you’re logging exactly in that line of code
Give five examples of JavaScript primitives.
string, number, boolean, null, and undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
to join two or more strings together
What purpose(s) does the + plus operator serve in JavaScript?
add numbers or concatenate strings
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
takes the variable and add something to it and then reassigns that value to the same variable.
Are strings immutable? and what does that mean?
yes - you cannot change the content of a string once it’s been created. like changing the middle letter of “cat” from a to o. it will always be a.
What are objects used for?
used to store multiple pieces of information that are related to eachother
What are object properties?
individual piece of named data within an object
Describe object literal notation.
declare object with name, assignment operator, opening curly braces for object then key value pairs separated by a comma, then closing curly braces.
How do you remove a property from an object?
delete operator
What are the two ways to get or update the value of a property?
dot notation e.g - obect.text or bracket notation - e.g. object[‘property’] = new value
What are arrays used for?
to store items in a list of information, the order of the list of information is either extremely important or unimportant
Describe array literal notation.
variable with the name for the array, assignment operator, opening square bracket, list of items in order by index separated by commas, closing square bracket.
How are arrays different from “plain” objects?
they have indexes which give their content numerical order
What number represents the first index of an array?
0
What is the length property of an array?
.length, measures the amount of items there are in an array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
a process or set of actions that have been given a name, that you can re-use by using that name.
Describe the parts of a function definition.
function keyword, name of the function, set of parentheses for parameters, curly brace, lines of code, and return statement.
Describe the parts of a function call.
function name, parentheses, arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function definition has a code-block and function keyword, whereas a function call doesn’t have either.
What is the difference between a parameter and an argument?
Parameter acts as a placeholder during the function definition, and the argument is the actual value during the function call.
Why are function parameters useful?
Allows you to have variance in how your code runs, apply the function to different arguments
What two effects does a return statement have on the behavior of a function?
returns back a value, as well as stop the function entirely.
Why do we log things to the console?
for debugging, verification (making sure data is what you think it is)
What is a method?
function stored as a property of an object
How is a method different from any other function?
SYNTAX! methods always have . and an object before it, functions do not.
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(indexStart, deleteCount)
How do you append or prepend an element to an array?
push or unshift
How do you break a string up into an array?
.split(where to separate or separator)
Do string methods change the original string? How would you check if you weren’t sure?
strings are immutable and can never be mutated or changed so no. But you can check with console log.
Roughly how many string methods are there according to the MDN Web docs?
36
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?
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?
Boolean
What is the purpose of an if statement?
allows us to make decisions in our code based on the result of the value of the boolean
Is else required in order to use an if statement?
Not required, but if not included - it would come back as undefined.
Describe the syntax (structure) of an if statement.
keyword if, parentheses that contains a condition, curly braces w/ code inside
What are the three logical operators?
&& (and), || (or), ! (not)
How do you compare two different expressions in the same condition?
using logical operators
What is the purpose of a loop?
a tool that allows you to run the same process over and over again
What is the purpose of a condition expression in a loop?
to check if the loop should keep going. it stops the loops.
What does “iteration” mean in the context of loops?
a single repetition of the loop
When does the condition expression of a while loop get evaluated?
Before the code block is ran
When does the initialization expression of a for loop get evaluated?
it runs one time before anything
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 and before the condition
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 by 1
How do you iterate through the keys of an object?
use a for-in loop
What is JSON?
text-based format data following javascript object syntax, data interchange format
What are serialization and deserialization?
taking spread out memory, like object, and putting into text like a string. Deserialization is the opposite - taking a string and parsing it into an object. (process of taking things in order, or taking them out of order)
Why are serialization and deserialization useful?
serialization is useful because it helps put things in order and make it easier to transmit, and deserialization makes it easier to access the data
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?
localStorage.setItem(‘keyName’, value you want to assign)
How do you retrieve data from localStorage?
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 page closes
What is a method?
a function assigned to the property of an object
How can you tell the difference between a method definition and a method call?
when calling a method you use dot notation, but when defining it is within an object, and has the following structure. methodname: function ()
Describe method definition syntax (structure).
an object literal, function name then colon which assigns the function definition
Describe method call syntax (structure).
object.method(arguments)
How is a method different from any other function?
theres dot notation on methods, you need to specify which object it’s being called on or pulled from.
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data, and behavior.
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
making very complicated things seemingly simple. simplify interaction with a procedurally complex problem
What does API stand for?
Application program interface
What is the purpose of an API?
allows application to exchange data and functionality easily and securely, using a limited set of tools.
What is ‘this’ in JavaScript?
the object that you’re currently working with, the code block you’re within
What does it mean to say that this is an “implicit parameter”?
meaning 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, unless the function is currently being used - ‘this’ does not exist.
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 yet, because a function has not yet been called
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
the string It’s a-me, Mario! and this is because .greet was called on character and we see that it uses the firstName property, which character has.
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
undefined, because there’s no object
How can you tell what the value of this will be for a particular function or method definition?
we don’t know, because there is no value until its being used or called
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot object.something
What kind of inheritance does the JavaScript programming language use?
prototype-based
What is a prototype in JavaScript?
an object with shared behavior or data that can be stored in one place and shared amongst different instances
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?
if they have a prototype
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
it goes and checks for it in each prototype object
What does the new operator do?
creates a blank plain javascript object, takes the constructor functions prototype property and puts it on the new object, then we say the newobject within the function when it runs is ‘this’. if nothing is returned - the object created in step one is returned.
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype
What does the instanceof operator do?
gives you the ability to check if a specific object is a variant of a larger type of object. newObject.instanceof(originalObject)