JavaScript Flashcards
What is the purpose of variables?
Data storage for scripts to call back to
How do you declare a variable?
They are declared with
- var
- let
- const
How do you initialize (**assign a value to) a variable?
Variables are initialized by including the assignment operator ‘=’
example: var example = true
What characters are allowed in variable names?
- letters
- numbers (CANNOT be the first character of a variable name)
- underscores(_)
- dollar signs
What does it mean to say that variable names are “case sensitive”?
Variables names must follow the same casing when used later in a script, so the variables “fullName” and “FullName” would be detected as two different variables
What is the purpose of a string?
To store text (a sequence of characters)
What is the purpose of a number?
To store a piece of numerical data (for math/calculations)
What is the purpose of a boolean?
To store a state of something is or is not
What does the = operator mean in JavaScript?
This is the assignment operator, it initializes variables and can re-assign their values.
How do you update the value of a variable?
Re-assign it with the assignment operator
What is the difference between null and undefined?
Null
is a value explicitly assigned to not existUndefined
is a default value set to variables that have not been assigned
Give five examples of JavaScript primitives.
- Number
- Boolean
- String
- Undefined
- Null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
The adding of a string to the end of another string
The result is always a string.
What purpose(s) does the + plus operator serve in JavaScript?
It tells values to combine by either concatenating or adding
What data type is returned by comparing two values (<, >, ===, etc)?
Boolean (true/false)
What does the += “plus-equals” operator do?
Addition assignment operator: adds the value of the right operand to a variable and assigns the result to the variable. Can be used to add or concatenate.
What are objects used for?
They are used to group related variables and functions
What are object properties?
Related variables/functions stored in an object become a property/method of the object
Describe object literal notation.
list out the properties and methods within curly braces and assign it to a variable
{ hasProperty: true, name: 'example', };
What are the two ways to get or update the value of a property?
Dot notation and bracket notationexampleObject.name'
-dot notationexampleObject['name']
-bracket notation
What are arrays used for?
They are used to store lists of data where the order is important or unimportant
Note: Unlike objects the key that the variables are paired to are numbers as they are indexed.
Describe array literal notation.
A list contained in two square-brackets ([ ]) with each value separated with commas
How are arrays different from “plain” objects?
The key that the variables are paired to are numbers and not properties. They are accessed with bracket-notation unless using a method.
(ex.)console.log(exampleArray[0])
What number represents the first index of an array?
0
What is the length
property of an array?
A property that returns the length of the array/object
How do you calculate the last index of an array?
Subtract the array length by 1
This is due to the array indexing the variables stored within using zero-based counting
(ex.)var lastIndex = exampleArray.length - 1;
What is a function in JavaScript?
A special object that can run a code block as many times as it is called
Describe the parts of a function definition
includes the keyword function, a name for the function (optional), parameters separated by commas (if any), the code block, and the return statement (optional)
(ex.)
function exampleFunction(word) { var completedPhrase = 'Hello, ' + word; return completedPhrase; }
Describe the parts of a function call
includes the name for the function followed by any arguments in parentheses
*(ex.) exampleFunction(arg1, arg2)
What are the differences between a function call and a function definition
A function call only includes the function name and any arguments instead of parameters
A function definition includes the keyword function and the name, as well as parameters instead of arguments, and a code block to be run
What is the difference between a parameter and an argument
A parameter is used to define a function, while an argument is a real number passed through the function.
6 examples of comparison operators
- < less than
- > greater than
- <= less than or equal to
- > = greater than or equal to
- === strictly equal to
- !== strictly not equal to
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if
statement?
To allow a specific condition to determine whether to run a code block or not
Is else
required in order to use an if
statement?
No, else is optional
Describe the syntax (structure) of an if
statement
if (condition) { }
What are the three logical operators?
- Or (||)
- And (&&)
- Not (!)
How do you compare two different expressions in the same condition?
Using a logical operator
Why do we log things to the console?
Debugging purposes, if we can see what value a variable is at different sections of the code, we can figure out where things went wrong.
What is a method?
A function that is stored as a property in an object
How is a method different from any other function?
It is associated with the object it is a method of
How do you remove the last element from an array?
the .pop() method, which is implicit to arrays
How do you round a number down to the nearest integer?
Math.floor()
The floor method of the Math object will take a value passed as an argument and round it down.
Note: optional parameters allow for changing the exponent and type of adjustment
How do you generate a random number?
Math.random()
The random method of the Math object generates a pseudo-random float value between 0 and 1.
How do you delete an element from an array?
arrayName.pop(); arrayName.shift(); arrayName.splice();
Can be used to delete an element from an array
How do you append an element to an array?
arrayName.push();
How do you break a string up into an array?
.split() - will split a string into an array based on the arguments passed through
Do string methods change the original string? How would you check if you weren’t sure?
No they do not. Strings are immutable. If one wanted to check one could log the string before and after using a method to see if it changed after the method was applied.
Is the return value of a function/value useful in every situation?
No, if what we need is the original data processed and not whatever we are calculating/extracting
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN