JavaScript Flashcards
What is the purpose of variables?
Variables store data!
How do you declare a variable?
Use the var, let, or const keyword
How do you initialize (assign a value to) a variable?
Follow the declaration with an “=“ and the value you’d like to assign
What characters are allowed in variable names?
Start with letter, underscore, or dollarsign. Afterwards — numbers are okay. No keywords! No dashes or periods~!
What does it mean to say that variable names are “case-sensitive”
the two variables lovesDogs and lovesdogs are different due to the D / d.
What is the purpose of a string?
Stores text-data, good for manipulation.
What is the purpose of a number?
Stores number-data, good for mathematics
What is the purpose of a Boolean?
Stores true/false data, good for logic
What does the = operator mean in Javascript?
Assignment operator — assigns right value to the left operand
How do you update the value of a variable?
Use the = operator with the variable on the left, and the new value on the right.
What is the difference between null and undefined?
Null is an intentionally empty value, undefined means no value was assigned
Why is it a good habit to include labels when logging values to the console?
When you add multiple console logs it gets confusing pretty quickly what you logging.
Give five examples of JavaScript primitives.
Number, string, Boolean, null, undefined
What is a JavaScript primitive?
A data that is not an object, and hence has no methods or properties.
What data type is retuned by an arithmetic operation?
Number is returned
What is string concatenation?
Joining together two strings and returning a new string
What purpose(s) does the + operator serve in JavaScript?
Adds two operands (not necessarily nums!)
What data type is returned by comparing two values (<, >, ===, etc.)?
Boolean (t / f)
What do the += “plus-equals’ operator do?
Shorthand — adds the value of right side to the left side variable and returns the result
How are reference data types and primitive data types stored differently?
Reference data types only store the memory pointer to that specific data
The two reference data types are…
Objects and arrays
What are objects used for?
To group together variables and functions in order to model something from the real world
What are object properties?
The variables of the object are its properties (its functions are known methods)
Describe object literal notation.
Declare variable and assign it the value of empty curly braces
How do you remove a property from an object
Use the delete keyword followed by the property and value
What are two ways to get or update the value of a property?
Dot & bracket notation
Dot notation vs bracket notation differences?
Dot notation requires a legal variable name —> bracket notation can also be used with variables
What are arrays used for?
Storing related data of unknown length — sort of like lists
Describe array literal notation.
var array = [data seperated by commas
How are arrays different from plain objects?
array keys are index numbers, object keys are assigned by us
What number represents the first index of an array?
Arrays are 0-indexed
What is the length property of an array?
It holds a number with the amount of items in the array
How do you calculate the last index of an array?
Use array.length - 1
What is a function in JavaScript?
A set of statements that performs a task — often with an input and output.
Describe the parts of a function definition.
Function keyword, function name (opt), (paramteter list), {code body, return(opt)}
Describe the parts of a function call.
Function name, (arguments)
How are function calls and function definitions different?
Definitions are stored in memory, calls look in memory and run the stored function
What is the difference between a parameter and an argument?
Parameters are for definitions, no concrete value —> arguments are for calling, has value
Why are function parameters useful?
They allow us to take advantage of a functions functionality by passing in specific arguments
What two effects does a return statement have on the behavior of a function?
- kicks you out of that execution context
- Specifies a value to be returned to the function caller
Why do we log things to the console?
It helps with debugging —> we can see what is stored in memory at that point
What is a method?
A property on objects with a function stored as the value
How is a method different from any other function?
Methods are linked directly to their related object —> otherwise they’re the exact same
How do you remove the last element from an array?
The pop method (no args)
How do you round a number down to the nearest integer?
The floor method (number to execute on)
How do you generate a random number?
Use the random method to get a num between 0 -> 1 (no args)
How do you delete an element from an array?
The splice method (takes starting index and number of elements to remove)
How do you append an element to an array?
The push method (element to push)
How do you break a string up into an array?
The split method (takes the character to split with)