JavaScript Flashcards
What is the purpose of variables?
Allows you to store data and access them later.
How do you declare a variable?
- variable keyword
- variable name
- assignment operator (=)
- variable value
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
letters, digits, _, $, and it cannot start with a number
(ex: cat1 is okay but not 1cat)
What does it mean to say that variable names are “case sensitive”?
uppercase and lowercase matters in JavaScript.
(ex. JavaScript != javascript)
precision is mandatory
What is the purpose of a string?
Storing some sequence of characters
What is the purpose of a number?
Stored when we need to manipulate mathematical information
What is the purpose of a boolean?
It is important for conditions. Able to represent if something is or isn’t.
Their purpose is to make decisions.
What does the = operator mean in JavaScript?
It is the assignment operator.
Used to assign a value to a variable.
How do you update the value of a variable?
When updating, you do not need the variable keyword, but still need the assignment operator.
+=
What is the difference between null and undefined?
undefined ==> a value that is not actually there. Means empty
null ==> null is a value that is assigned. It is done on purpose.
Why is it a good habit to include “labels” when you log values to the browser console?
Having labels makes it clear what we are working with.
Sometimes, there are multiple console.logs and you would need to differentiate.
Give five examples of JavaScript primitives.
- String
- Numbers
- Booleans
- Null
- Undefined
What data type is returned by an arithmetic operation?
a number
What is string concatenation?
The process of joining together two or more strings to create one new string. Uses the + (plus operator)
What purpose(s) does the + plus operator serve in JavaScript?
The plus operator can be used for string concatenation and as an arithmetic operation (adding).
What data type is returned by comparing two values (<, >, ===, etc)?
A boolean
(true or false)
What does the += “plus-equals” operator do?
The += is an assignment operator that sums up the left and right operand values then assign the obtaining result to the left operand.
It will increment your sum variable with the amount next to it.
What are objects used for?
Objects group together a set of variables and functions to create a model of something you would recognize from the real world
What are object properties?
If a variable is part of an object, it is called a property. Properties tell us about the object, such as the name of a hotel or the number of rooms it has.
Describe object literal notation.
The object is the curly braces and their contents. Separate each key from its value using a colon. Separate each property and method with a comma (but not after the last value).
How do you remove a property from an object?
Use the delete operator and assign the property using either the dot or bracket notation.
What are the two ways to get or update the value of a property?
Dot notation and bracket notation.
Ex. student.firstName || student[‘firstName’]
What are arrays used for?
Arrays store a list of values.
You should consider using an array whenever you are working with a list or a set of values that are related to each other.
Describe array literal notation.
The values are assigned to the array inside a pair of square brackets[ ], and each value is separated by a comma.
The values in the array do not need to be the same data type, so you can store a string, a number and a Boolean all in the sam array.
How are arrays different from “plain” objects?
Arrays are a special type of object. They hold a related set of key/value pairs (like all objects), but the key for each value of an array is its index number.
They tell you the length. To add data, you would need a method (push). Arrays have an order.
What number represents the first index of an array?
0
What is the length property of an array?
Each array has a property called length, which holds the number of items in the array.
( ex. array.length)
How do you calculate the last index of an array?
You take the length property of the array and subtract by 1.
What is a function in JavaScript?
Packed up code that can be reused throughout a program.
A set of statements that performs a task or calculates a value.
Describe the parts of a function definition.
- The function keyword to begin the creation of the function
- An optional name
- A comma-separated list of zero or more parameters, surrounded by ( )
- The start of the function’s code block, indicated by an { opening curly
brace - An optional return statement.
- The end of the function’s code block, as indicated by a } closing curly brace
Describe the parts of a function call.
- The function’s name
- A comma-separated list of zero or more arguments surrounded by ( )
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call is just the function name with the argument.
Function definition has the function keyword and the code block.
What is the difference between a parameter and an argument?
A parameter is a placeholder. It is a variable whose value is not known until we call the function and pass an argument. When the function’s code block is run, the parameter will be holding the value of the argument.
When we define a function, we declare parameters
When we call a function, we pass it arguments
Why are function parameters useful?
It can be used as a placeholder for when the arguments will eventually be passed.
Also, the parameter can be used within the code block when making the function
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?
The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code.
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?
Methods are associated with an object, while a function is not.
How do you remove the last element from an array?
Using the pop() method
Array.prototype.pop()
How do you round a number down to the nearest integer?
using the floor method of the Math object.
Math.floor()
How do you generate a random number?
by calling the random() method of the Math object.
Math.random()
How do you delete an element from an array?
By calling the splice() method
Array.prototype.splice()
splice(start, deleteCount)
How do you append an element to an array?
By calling the push() methods.
Array.prototype.push()
append = Put it on the end
How do you break a string up into an array?
By calling the split() method
String.prototype.split()
Do string methods change the original string? How would you check if you weren’t sure?
No. Strings are immutable.
You would check logging the value to the console.
Roughly how many string methods are there according to the MDN Web docs?
50 string methods
Is the return value of a function or method useful in every situation?
No. functions and methods are only useful in specific situations
Roughly how many array methods are there according to the MDN Web docs?
41 array methods
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
- Less than (<)
- Greater than (>)
- Less than or equal too (<=)
- Greater than or equal too (>=)
- Strict equality operator (===)
- Strict inequality operator (!==)
What data type do comparison expressions evaluate too?
Booleans
What is the purpose of an if statement?
To make decisions in the code
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
- if keyword
- condition
- opening curly brace
- code to execute if value is true
- closing curly brace
What are the three logical operators?
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)