JavaScript Flashcards
What is the purpose of variables?
It is to store bits of information to use later
How do you declare a variable?
You have to use a variable keyword like ‘var’ and then follow it with a variable name. From there, you use the assignment operator ‘=’ and assign a value to it
How do you initialize (assign a value to) a variable?
[variable name] = [variable value]
What characters are allowed in variable names?
The name must begin with a letter, dollar sign, or an underscore
Can contain letters, numbers, dollar signs, or underscores
What does it mean to say that variable names are “case sensitive”?
Score and Score would be different values
What is the purpose of a string?
Used for working with any kind of text
Frequently used for adding new content to a page
What is the purpose of a number?
Counting and calculating
What is the purpose of a boolean?
Serves as an on/off switch
Helpful in determining which part of the script should run
What does the = operator mean in JavaScript?
Assignment operator
Gives value to something
How do you update the value of a variable?
To update you would re-assign a value to the variable
What is the difference between null and undefined?
a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.
Every Object is derived from null value, and therefore typeof operator returns object for it:
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
Why is it a good habit to include “labels” when you log values to the browser console?
So that there is context to the output
Helps with debugging
Give five examples of JavaScript primitives.
data that is not an object and has no methods. There are 7 primitive data types: string, number,, boolean, undefined,, and null.
We can disregard bigInt and symbol for the time being
What data type is returned by an arithmetic operation?
number
What is string concatenation?
The process of joining together two or more strings to create one new string
What purpose(s) does the + plus operator serve in JavaScript?
To add or concatenate
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
It adds then re-assigns the variable
What are objects used for?
They are used for storing different variables and values of different data types
What are object properties?
A variable that is in an object.
Individual key in an object that correlates with a value
Describe object literal notation.
Create a variable and assign it to a value with curly braces. In the curly braces have names that indicate properties or methods and assign them value by using a colon
How do you remove a property from an object?
To delete a property, you use the keyword delete and then use the dot notation to identify the property or method you want to remove from the object
What are the two ways to get or update the value of a property?
Using dot notation or using square brackets
What are arrays used for?
When working with a list or a set of values related to each other
Especially helpful when you do not know how many items a list will contain
Representing lists of data
Describe array literal notation.
Var array = []
How are arrays different from “plain” objects?
Each value in the array doesnt need a variable name/key
All arrays come with length property
Other ways to add to an array compared to plain objects
What number represents the first index of an array?
array[0]
What is the length property of an array?
Array.length gives you the length of an array.
How do you calculate the last index of an array?
Take the length and subtract by 1, then use that as an index
What is a function in JavaScript?
Functions are a special kind of object that is “callable”.
A function is a code that you can re-use
packing up code for reuse throughout a program
giving a name to a handful of code statements to
make it code easier to read
making code “dynamic”, meaning that it can be written once to handle many (or even infinite!) situations
Describe the parts of a function definition.
The function keyword to begin the creation of a new function.
An optional name. (Our function’s name is sayHello.)
A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)
The start of the function’s code block, as indicated by an { opening curly brace.
An optional return statement. (Our sayHello function doesn’t have a return statement.)
The end of the function’s code block, as indicated by a } closing curly brace.
Describe the parts of a function call.
simply writing the name of the function and placing () parentheses next to it.
The function’s name. Again, our function’s name is sayHello.
A comma-separated list of zero or more arguments surrounded by () parentheses.
Our sayHello function does not take any arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call uses the already defined function by including the function name and parentheses. In parentheses there might be arguments.
Function definition first states that it is a function by using function keyword, then defines the function name, sets up parameters (optional), followed by curly braces, inside the curly braces is code block.
Calling function gives an output while defining a function does not
What is the difference between a parameter and an argument?
Parameter
as a placeholder.
A variable whose value is not known until we call the function and pass in an argument
Parameters are declared when we define the function
Arguments
When we call a function, we pass in the arguments
Is the actual name of what the placeholder was
Why are function parameters useful?
They serve as a placeholder, they tell you what kind of value will be need to be placed there when calling the function
Give us the ability to have varying results based on the arguments we give
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.
What is an expression?
A chunk of code that needs to be evaluated to lead to a value
Why do we log things to the console?
console.log()
Debugging mechanism to understand what output we are getting
What is a method?
A method is a function which is a property of an object.
How is a method different from any other function?
It is a function that is stored in a property of an object
How do you remove the last element from an array?
array.pop()
How do you round a number down to the nearest integer?
Math.floor()