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