JavaScript Flashcards
What is the purpose of variables?
to store or remember bits of information for the script.
How do you declare a variable?
variable keyword plus variable name (var, let, const)
How do you initialize (assign a value to) a variable?
with the assignment operator (=)
What characters are allowed in variable names?
letters, numbers, dollar sign or underscore
What does it mean to say that variable names are “case sensitive”?
that uppercase/lowercase letters are distinguished. score is not same as Score.
What is the purpose of a string?
to represent or manipulate text
What is the purpose of a number?
to represent or manipulate numbers for calculation.
What is the purpose of a boolean?
they allow us to make decisions in code. to determine if something is true or false.
What does the = operator mean in JavaScript?
assignment operator. assigns value to a variable.
How do you update the value of a variable?
variable name, equal sign, new value.
What is the difference between null and undefined?
null is purposeful emptiness; undefined is accidental emptiness.
Why is it a good habit to include “labels” when you log values to the browser console?
to make it clear which variables are being logged.
Give five examples of JavaScript primitives.
string, number, boolean, undefined, null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
joining two or more strings together to create a single value
What purpose(s) does the + plus operator serve in JavaScript?
addition for arithmetic and string concatenation for string.
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable.
What are objects used for?
to group together a set of variables and function to create a model. to store multiple pieces of information that are related to each other.
What are object properties?
it’s the variable of the object. tells us about the object (name, number of rooms)
Describe object literal notation.
keyword var, object variable name, property key and value pairs.
How do you remove a property from an object?
use the operator ‘delete’ followed by dot or bracket notation.
What are the two ways to get or update the value of a property?
dot notation or square bracket notation
What are arrays used for?
to store a list or set of values that are related to each other
Describe array literal notation.
keyword var, name of array, equal sign, open square bracket, values separated by a comma, closing square bracket.
How are arrays different from “plain” objects?
the property (key) for each value is its index number. values in arrays are usually the same data type. there’s a length property.
What number represents the first index of an array?
0
What is the length property of an array?
it holds the number of items in an array.
How do you calculate the last index of an array?
array.length minus 1.
What is a function in JavaScript?
set of statements that performs a task or calculates a value.
Describe the parts of a function definition.
function keyword, function name with parameters, code block in curly braces.
Describe the parts of a function call.
The function’s name.
A comma-separated list of zero or more arguments surrounded by () parentheses.
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition is to declare a function;
function call is to invoke the function (to run the code).
What is the difference between a parameter and an argument?
parameter acts as a placeholder (declared during function definition); argument is the actual value we want to run through the function. (passed through during function call).
Why are function parameters useful?
used as a placeholder so we can use it to run multiple arguments.
What two effects does a return statement have on the behavior of a function?
- Causes the function to produce a value we can use in our program.
- Prevents any more code in the function’s code block from being run.
Why do we log things to the console?
to spot check that our code works and debug it, verification, landmark to see code execute.
What is a method?
function stored as the property of a object.
How is a method different from any other function?
there’s a dot notation for method. (object.method)
How do you remove the last element from an array?
pop() method ( array.pop() )
How do you round a number down to the nearest integer?
Math.floor() method. ( Math.floor(x) )
How do you generate a random number?
Math.random() method. ( Math.random() )
How do you delete an element from an array?
splice() method
How do you append an element to an array?
push() and unshift()
How do you break a string up into an array?
split() method.
Do string methods change the original string? How would you check if you weren’t sure?
No. You can check by console.logging the original variable for the string.