JavaScript Flashcards
What is the purpose of variables?
to store data
How do you declare a variable?
variable keyword, variable name, assignment operator, and value
ex: var name = ‘Ashley’;
How do you initialize (assign a value) to a variable?
use the assignment operator (=)
variable keyword variable name = value
var greeting = ‘hello’;
What characters are allowed in variable names?
letters, numbers, $ or underscore
** however, variables cannot start with a number
What does it mean to say that variable names are “case sensitive”?
Capitalization is taken into consideration when naming or calling variables
*means that two variables with the same name but different capitalization would be considered two variables
ex: var potato; vs var Potato;
What is the purpose of a string?
store/manipulate text that js won’t interpret as code
What is the purpose of a number?
represent and manipulate quantities/mathematical operations
What is the purpose of a boolean?
represents true/false, for decision making
What does the = operator mean in JavaScript?
a value is being assigned to a variable
How do you update the value of a variable?
use the variable name with assignment operator (=) and the new value
What is the difference between null and undefined?
Null indicates an intentional absence of a value, like a placeholder
*when you see null, that tells you that someone wanted it to be empty on purpose
undefined means there hasn’t been anything assigned to the variable
*JS way of communicating that there’s nothing there.
Why is it a good habit to include ‘labels’ when you log values to the browser console?
it provides you with more clarity and can help with debugging
Give 5 examples of JavaScript primitives
string, number, boolean, null, undefined
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
joins two or more strings together
What purpose(s) does the + operator serve in JS?
adds values together
*arithmetically if numbers
*concatenation if strings
What data type is returned by comparing two values (> < ===, etc)?
boolean
what does the += operator do?
takes the value to the right of the operator and adds/concatenates it to the variable on the left and assigns the result of that expression to the variable
What are objects used for?
to group together variables and functions to create a model of something
Gives you ability to store multiple pieces of data under 1 name
What are object properties?
variables and their values
describe object literal notation
variable keyword name of object assignment operator
curly brace followed by properties and methods and their values
end curly brace
ex: var person {
name: ‘ashley’,
age: 29,
ethnicity: ‘vietnamese’
};
How do you remove a property from an object?
delete operator followed by objectname.propertyname;
or
delete operator followed by objectname[‘property.name’];
What are two ways to get or update the value of a property?
dot notation
ex: objectname.propertyname;
bracket notation
ex: objectname.[‘propertyname];
if you want to update the value, follow it with an assignment operator and the new value
What are arrays used for?
To store a list of values that are related to each other
Describe array literal notation
pair of brackets, inside the brackets are values separated by commas
How are arrays different from “plain” objects?
objects store properties and their values while arrays have indexes and values
to add to an object, use dot or bracket notation
to add to an array, use the push method
arrays also have length property while objects do not
What number represents the first index of an array?
0
What is the length property of an array?
it tells you the numbers of items in the array.
How do you calculate the last index of the array?
array.length - 1
What is a function in JavaScript?
A block of code designed to do a specific task
Describe the parts of a function definition
- Function keyword
- Optional name
- Parameters ( 0 or more separated by commas)
- Code block
- Optional return statement
Describe the parts of a function’s call.
- Name of function
- Any arguments being passed in between parentheses
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definitions are longer and can have parameters and a keyword
Function calls are much simpler and can pass arguments
What is the difference between a parameter and an argument?
Parameters are like placeholders in the function definition, arguments are what’s being passed when you call a function
Why are function parameters useful?
They act as placeholders that allow us to pass arguments when you call the function
What two effects does a return statement have on the behavior of a function?
exits the code block/prevents any more code in the block from being run
allows the function to produce a value that you can utilize
Why do we log things to our console?
to observe and communicate with our code
to check our work
What is a method?
a function that is associated/a property of an object
How is a method different from any other function?
you need to state the object you’re calling it off on.
ex: object.method() vs functionName()
How do you remove the last element of an array?
pop() method
How do you round a number down to the nearest integer?
floor() method
How do you generate a random number?
random() method –> gives you a value between 0 and 1 which you multiply by the string length
How do you delete an element from an array?
splice() method
parameters are (start, deletecount, item1, itemN)
how do you append an element to an array?
push() method –> adds to the end of an array.
How do you preppend(add at the beginning) an element from an array?
unshift() method
how do you break a string up into an array?
split(separator, limit) method –> takes pattern, divides string into ordered list of substrings, puts strings into array, returns array