JavaScript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
Using the ‘var’ keyword
How do you initialize (assign a value to) a variable?
Using the “=” sign
What characters are allowed in variable names?
Must begin with a letter, ($), or (_)
Can’t start with numbers or use (-) or (.)
What does it mean to say that variable names are “case sensitive”?
value and Value are different variable names
What is the purpose of a string?
To store a string of characters
What is the purpose of a number?
To store a numeric value like amount
What is the purpose of a boolean?
To determine if something should be done; should code run
What does the = operator mean in JavaScript?
To assign, store a value to a variable
How do you update the value of a variable?
You re-assign a variable; don’t need to use the var keyword
What is the difference between null and undefined?
Undefined when variable is not assigned a value, null is purposeful emptiness;
Null has to be assigned
Why is it a good habit to include “labels” when you log values to the browser console?
Clarity for what is being logged
What are objects used for?
Group together a set of variables and functions to create a model of something.
What are object properties?
a variable that is part of an object
Describe object literal notation.
var object = { key: 'value', key: 40 }
How do you remove a property from an object?
Using the delete keyword;
delete object.propertyName
What are the two ways to get or update the value of a property?
Dot notation: object.popertyName;
Bracket notation: object[‘propertyName’];
Give five examples of JavaScript primitives.
strings, numbers, booleans, undefined, null
What data type is returned by an arithmetic operation?
numbers
What is string concatenation?
Combine 2 strings into a new string
What purpose(s) does the + plus operator serve in JavaScript?
Add numbers or concatenate strings
What data type is returned by comparing two values (greater than, less than, ===, etc.)
Booleans
What does the += “plus-equals” operator do?
Combines the variable and the value and assigns the new value to the variable
What are arrays used for?
To store a list of values
Describe array literal notation.
var array = [‘value1’, ‘value2’, ‘value3’];
How are arrays different from “plain” objects?
Arrays uses index numbers and objects use property names.
What number represents the first index of an array?
0
What is the length property of an array?
the number of items in the array
array.length
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
Functions allow you to package up code for use later in your program.
Describe the parts of a function definition.
Function definitions are made of:
- the function keyword
- an optional name
- zero or more parameters
- a code block
- an optional return statement
Describe the parts of a function call.
- The function’s name.
- A comma-separated list of zero or more arguments surrounded by () parentheses.
example(arg1, arg2, arg3);
When comparing them side-by-side, what are the differences between a function call and a function definition?
Definition will have function keyword and { }
What is the difference between a parameter and an argument?
Parameter is place holder of unknown value until function is called and argument is passed. When a function is called, the parameters in its definition take on the values of the arguments that were passed.
when we define a function, we declare parameters and that when we call a function, we pass it arguments.
Why are function parameters useful?
Way to give data to functions, way to provide data to get different results of the function
What two effects does a return statement have on the behavior of a function?
- A return statement causes the function to produce a value.
- A return statement also exits the function; no code after the return statement is executed.
Why do we log things to the console?
For code to communicate with us and see if feature is working.
So we know what is being returned from a function or method.
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 attached to an object; Not a free floating function; potential to use data attached to object
How do you remove the last element from an array?
The pop( ) method removes the last element from an array and returns that element. This method changes the length of the array.
How do you round a number down to the nearest integer?
The Math.floor( ) function returns the largest integer less than or equal to a given number.
How do you generate a random number?
The Math.random( ) function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.
How do you delete an element from an array?
splice( ) method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
How do you append an element to an array?
push() method adds one or more elements to the end of an array and returns the new length of the array.
How do you break a string up into an array?
split( ) method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.
Do string methods change the original string? How would you check if you weren’t sure?
No. You can console.log or check MDN.
Is the return value of a function or method useful in every situation?
No, sometimes the return isn’t needed EX: push method returns the length but I didn’t need it for the exercise.
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.
=== strict equal to !== strict not equal to > greater than < less than >= greater than or equal to <= less than or equal to
What data type do comparison expressions evaluate to?
booleans
What is the purpose of an ‘if’ statement?
the ‘if’ statment evaluates (or checks) a condition. if condition evaluates to ‘true’, any statements in the subsequent code block are executed.
Is ‘else’ required in order to use an ‘if’ statement?
No
Describe the syntax (structure) of an ‘if’ statement.
if (condition) {
code block
}
What are the three logical operators?
&& logical and
|| logical or
! logical not
How do you compare two different expressions in the same condition?
Use logical operator && or ||