JavaScript Flashcards
What is the purpose of variables?
Variables let scripts be reusable, changing the values used each time the script is run
Variables let the computer remember a value for future use
According to MDN, variables allow an unpredictable value to be accessed by a predetermined name. (And that variables are named references to values)
How do youdeclarea variable?
Use a keyword like var
(or let
or const
but don’t use them here at LFZ) and then the name of the variable myVar
Name variables with camelCase
Variable type does not need to be specified in JavaScript
How do you initialize (assign a value to) a variable?
Use variable name and the assignment operator followed by the value to assign/initialize with (ending with a semicolon) myVar = 23;
What characters are allowed in variable names?
letters, dollar sign ($), underscores (_), and numbers (but cannot start with a number)
What does it mean to say that variable names are “case sensitive”?
Two variable names that are the same letters but with different capitalization will be considered two different variables
What is the purpose of a string?
Stores text values or a string of characters that wouldn’t make sense to JavaScript
What is the purpose of a number?
Stores numeric values not only for calculations but also numeric properties such as screen size. Stores positive and negative integers and decimals
What is the purpose of a boolean?
Like a light switch, it indicates one of two states (true
or false
)
Allows for logic and decisions
What does the=
operator mean in JavaScript?
=
is the assignment operator, meaning it assigns values to variables (or properties, or other stuff)
How do you update the value of a variable?
Assign it a value again, no keyword at the start
What is the difference betweennull
andundefined
?
null
is usually the intentional absence of a value whereas undefined
is assigned to newly created variables with no assignment yet (a sort of default absence of value)null
is an object
while undefined
has type of undefined
JavaScript can create undefined
(and leave it to JS to do) but only you can create null
Why is it a good habit to include “labels” when you log values to the browser console?
Without labels, the output can quickly become a very confusing screen of seemingly random values
Give five examples of JavaScript primitives.
string
, number
, boolean
, undefined
, null
, bigint
, symbol
What are the boolean and numeric values of null
and undefined
?
Both are considered false
null
will be 0
in numeric contexts and undefined
will be NaN
What are objects used for?
Objects group together related variables and functions into a single object
What are object properties?
Properties are like variables that are part of an object, they store values like numbers, strings, booleans, arrays, and objects
Describe object literal notation.
Object literal notation is a way to create an object
Begins with curly braces, then has property/method name followed by a :
, then the value/function for that property/method, and commas to separate these key-value pairs (all within the curly braces). Usually this is assigned to a variable as usual
How do you remove a property from an object?
delete myObj.propertyToRemove
or delete myObj['propertyToRemove']
What are the two ways to get or update the value of a property?
Dot notation (myObj.propertyName
) or bracket notation (myObj['propertyName']
)
What are arrays used for?
Stores list-like information, a collection of values that are all related to each other in some way
Describe array literal notation.
Square brackets with values inside delineated by commas
How are arrays different from “plain” objects?
- The keys are incrementing numbers (indexes)
- Objects can do dot notation
- Objects don’t have order
- Empty arrays have a method built in already,
length
- Adding to objects is different from adding to arrays
What number represents the first index of an array?
- In JavaScript, indexes start at
0
- Originally in lower level languages, index meant how many memory spaces to move from the beginning to get the value you want (move 0 spaces to get the first item)
What is thelength
property of an array?
- It contains the number of items in the array, built into arrays
- Number of indexes in there
How do you calculate the last index of an array?
myArray.length - 1
What is a function in JavaScript?
Packaging code up into a reusable and easily referenced form
Describe the parts of a functiondefinition.
function optionalName(param1, param2, ...) { code definition; optional return value }
Describe the parts of a functioncall.
optionalName(arg1, arg2, ...)
When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
Calls do not have the keyword function
in front, nor do they have the {}
to indicate what will happen within the function. Calls also take arguments, while functions have parameters.
What is the difference between aparameterand anargument?
Parameters are part of the function definition. Arguments are passed into a function call.
Why are functionparametersuseful?
They make the code within the function reusable for multiple variables so the work can be repeatedly done on similar things
What two effects does areturn
statement have on the behavior of a function?
Stops the execution of the function, meaning no code beyond the first return
is run.
Return replaces a function call with the return value
Why do we log things to the console?
So that we can see what our code is doing throughout the script and check that it is working as intended
What is a method?
A function which is a property of an object
How is a method different from any other function?
They are specific to the object that they belong to and must be called with reference to its object
How do you remove the last element from an array?
myArray.pop()
removes and returns the last element from an array
How do you round a number down to the nearest integer?
Math.floor(myNumber)
How do you generate a random number?
Math.random()
generates a pseudo-random number between 0 and 1 (inclusive of 0, not inclusive of 1). Multiply this by the range needed to get a random number between x and y.
How do you delete an element from an array?
myArray.splice(startIndex, numOfItemsToRemove)
to remove from specific locationsmyArray.pop()
deletes from the endmyArray.shift()
deletes from the beginning
How do you append an element to an array?
myArray.push()
adds to the end (to append)myArray.splice(startIndex, 0, elementToAdd, nextElementToAdd, ...)
for adding elements at a specific location in the arraymyArray.unshift()
adds to the beginning
How do you break a string up into an array?
myString.split('separator')
breaks up a string along the separator supplied
Do string methods change the original string? How would you check if you weren’t sure?
No, I would try using a few string methods on a string and then log the value of the string to see if it had undergone any transformations
Give 6 examples of comparison operators.
===
, !==
, >
, >=
, `
What data type do comparison expressions evaluate to?
boolean
What is the purpose of anif
statement?
Allows our programs to make decisions, executing some code sometimes and other code (or no code) at other times based on some condition
Iselse
required in order to use anif
statement?
No, it will simply not execute the code in the code block of the if
statement and then continue on with the rest of the code
Describe the syntax (structure) of anif
statement.
if (expression that evaluates to boolean) {code} else {code}
What are the three logical operators?
&&
, ||
, !
How do you compare two different expressions in the same condition?
Wrap each expression in parentheses, then use a comparison operator between them to have the entire expression evaluate down to a single boolean
Also, use logical operators
What is the purpose of a loop?
Do a block of code a repeated number of times
What is the purpose of aconditionexpression in a loop?
To check if another loop should be executed (and to have a way to stop looping at some point)
What does “iteration” mean in the context of loops?
One execution of the code block
_When_does theconditionexpression of awhile
loop get evaluated?
At the beginning of each iteration, including the first
_When_does theinitializationexpression of afor
loop get evaluated?
At the beginning of the loop (just once)
_When_does theconditionexpression of afor
loop get evaluated?
At the beginning of each iteration, including the first
_When_does thefinalexpression of afor
loop get evaluated?
At the end of each iteration
Besides areturn
statement, which exits its entire function block, which keyword exits a loop before itsconditionexpression evaluates tofalse
?
break
What does the++
increment operator do?
Increments the operand that comes before the ++
by 1
and returns the current value
- Both incrementation and substitution
How do you iterate through the keys of an object?
Use a for ... in
loop
- for (var key in object) {code here}
What is a “model”?
A model is a representation, usually imperfect, of some other thing. Usually used to better understand that other thing, or make some sort of work with that thing easier.
Which “document” is being referred to in the phrase Document Object Model?
The web page as a whole
What is the word “object” referring to in the phrase Document Object Model?
The objects of the document, in this case the nodes
JavaScript object data type, storing key-value pairs