Javascript Flashcards
What is the purpose of variables?
Used to store data
How do you declare a variable?
var keyword + variable name
How do you initialize a variable?
By using the assignment operator ( = ) and giving it value
What characters are allowed in variable names?
Letters, numbers, dollar signs, and underscores
What does it mean to say that variable names are “case-sensitive”
Uppercase and lowercase variables with the same name will have different values
What is the purpose of a string?
Used to store data of letters and words.
Used to add new content into a page
Can contain HTML markup
What is the purpose of a number?
Used for calculations or tasks such as determining size of screen, moving the position of an element on a page, or setting the amount of time an element should take to fade it
What is the purpose of a boolean?
Helpful in determining which part of a script should run
What does the = operator mean in javascript?
the assignment operator.
states that you are going to assign value to a variable
How do you update the value of a variable?
Reassign it.
Variable name + equal sign +new value
What is the difference between null and undefined?
Null is an assigned value (means nothing)
Undefined means a variable has been declared but not yet defined
Null is an object, undefined is undefined
Why is it a good habit to include labels when you log values to the browser console?
For clarity - to keep your code clean and help you fix issues
Give 5 examples of javascript primitives:
- String
- Number
- Boolean
- Undefined
- Null
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
Adding two strings together with the + symbol
What purpose does the + operator serve in javascript
Addition with numbers, concatenation with strings or strings & numbers
What data type is returned by comparing two values ( ‘ > ‘, ‘ < ‘ , === ,etc)?
Boolean
What does the += “plus equal” operator do
assigns the value of the right operand to a variable and assigns the result to the variable
x+=5 is the same as x= x+5
What are objects for?
Grouping together a set of variables and functions to create a model of something
Objects store related data
What are object properties?
The variables of the object
collection of key: value pairs
Describe object literal notation
Key value pairs that are separated from each other with a colon and separated from other properties and methods with a comma and then stored in a variable inside curly braces
How do you remove a property from an object?
delete keyword followed by object name period and property name
delete object.property
What are the two ways to get or update the value of a property?
dot notation: var name = object.newproperty bracket: var name = object['property']
What are arrays used for?
storing lists or sets of values
Describe the array literal notation
var keyword followed by the name of the array, equal sign, then the lists of values within square brackets.
each value is separated by a comma
How are arrays different from ‘plain’ objects?
arrays can store a series of objects and remember their order
What number represents the first index of an array?
0
What is the length property of an array?
holds the number of items in the array
How do you calculate the last index of an array?
array.length -1
the length of the array - 1
What is a function in Javascript?
collection of code that is reusable and helps make the code easier to read
allow you to package up code for use later in your program
Describe the parts of a function definition
- function keyword
- an optional name
- zero or more parameters
- a code block
- an optional return statement
Describe the parts of a function call
function named followed by ( ) parentheses with zero or more arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
- calling causes the functions code block to be executed
- definition has parameters, calling has arguments with ( )
- definition includes the function keyword, code block & return statement
What is the difference between a parameter and an argument?
parameters occur in the definition, while arguments occur when called
parameters are placeholders, arguments are values
Why are function parameters useful?
when a function is called, the parameters in its definition take on the values of the arguments that were passed
What two effects does a return statement have on the behavior of a function?
causes the function to produce a value
exits the function
Why do we log things to the console?
as a debugging tool - easy way to inspect your variables in the browser
What is a method?
a function which is a property of an object
How is a method different from any other function?
methods are associated with an object
How do you remove the last element from an array?
pop ( ) method
How do you round a number down to the nearest integer?
Math.floor ( )
How do you generate a random number?
Math.random ( )
How do you delete an element from an array?
splice( )
How do you append an element to an array?
push( )
How do you break a string up into an array?
split ( )
Do string methods change the original string?
How do you check if you werent sure?
no
MDN or the console
Roughly how many string methods are there according to the MDN web docs?
3 static methods
34 instance methods
Is the return value of a function or method useful in every situation?
no - sometimes return isn’t needed
Roughly how many array methods are there according to the MDN web docs?
3 static methods
31 instance 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
- ==
- !=
- ===
- !==
- > , >=
6.
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
to evaluate a condition and if true, to execute the code block
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement
- if keyword
- condition in parentheses ( )
- code block to execute within curly brace { }
What are three logical operators?
&& logical and
|| logical or
! logical not
How do you compare two different expressions in the same condition?
using logical operators and (&&) or (||)
What is the purpose of a loop?
to repeat an action or a code to check a condition
What is the purpose of a condition expression in a loop?
if returns true a code block will run, if false it will stop
What does ‘iteration’ mean in the context of loops?
each time the loop runs through
When does the condition expression of a while loop get evaluated?
before each pass through the loop
- if evaluates true, statement is executed
When does the initialization expression of a for loop get evaluated?
once before the loop begins
When does the condition expression of a for loop get evaluated?
before each loop iteration
When does the final expression of a for loop get evaluated?
at the end of each loop iteration but before the condition runs a second time
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
adds one to its operand and returns a value
How do you iterate through the keys of an object?
for..in loops
Why do we log things to the console?
to check for errors/debugging/double checking your values
What is a model?
a recreation or representation of something