JavaScript Flashcards
What is the purpose of variables?
The purpose of variables is to store value.
Why does this help? Variables can change & it exists for a longer time.(data permanence)
How do you declare a variable?
You declare a variable by using the var keyword and giving the variable a name.
How do you initialize (assign a value to) a variable?
You initialize (assign a value to) a variable with the = operator.
What characters are allowed in variable names?
Letters, numbers, dollar sign($), and underscore(_).
Note: dash(-) or period( . ) or question-mark(?) are not allowed & Numbers cannot go first.
What does it mean to say that variable names are “case sensitive”?
When it is said that variable names are “case sensitive” it means that capitalization matters.
Ex: String and string are not the same.
What is the purpose of a string?
The purpose of a string is to store text.
What is the purpose of a number?
The purpose of a number is to use it for math and calculations.
What is the purpose of a boolean?
The purpose of a boolean is to store if something is true or false. Booleans exist for decision making.
What does the = operator mean in JavaScript?
The = operator in JavaScript means that we are assigning a value to something.
How do you update the value of a variable?
You update the value of a variable by assigning a new value to the variable.
Note: You don’t need the var keyword to assign a new value to an existing variable.
What is the difference between null and undefined?
Null: Primitive value that represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. Null always need to be assigned to something.
Undefined: Primitive value automatically assigned to variables that have only been declared. Also assigned to formal arguments where there are no actual arguments.
What data-type is returned by an arithmetic operation?
A Number value is returned by an arithmetic operation.
What is string concatenation?
String concatenation is adding two or more strings to make a longer string.
What purpose(s) does the + plus operator serve in JavaScript?
The + plus operator adds numbers and concatenates strings in JavaScript.
What data type is returned by comparing two values (< , >, ===, etc)?
A Boolean value is returned by comparing two values (< , >, ===, etc).
What does the += “plus-equals” operator do?
A value on the right-side will be added to the variable, and the variable will then be updated to that new value.
What are objects used for?
Objects are used to group together a set of properties and methods to create a model of something.
What are object properties?
Object properties are variables glued to that object.
Describe object literal notation.
var hotel = { name: 'Cosmopolitan', rooms: 500, booked: 300, checkAvailability: function( ) { return this.rooms - this.booked; } };
How do you remove a property from an object?
Removing a property from an object using the dot notation:
delete objectname.propertyname.
Removing a property from an object using the bracket notation:
delete objectname[‘propertyname’].
What are the two ways to get or update the value of a property?
Updating/getting a property from an object using the dot notation: variablename.propertyname.
Updating/getting a property from an object using the bracket notation: variablename[‘propertyname’].
What are arrays used for?
Arrays are used to store a list of data with number indexes.
Describe array literal notation.
colors = [‘white’, ‘blue’, ‘red’]
How are arrays different from “plain” objects?
Arrays have orders to them unlike “plain” objects. Arrays will repair itself if a data is deleted.
What number represents the first index of an array?
0 represents the first index of an array.
What is the length property of an array?
The length property of an array tells us the number of data stored in an array.
How do you calculate the last index of an array?
We calculate the last index of an array by:
arrayname.length - 1
What is a function in JavaScript?
A function in JavaScript is giving a name to some amount of code so that it can be reused and also read easier.
Describe the parts of a function definition.
function functionName (parameters) { return }
Describe the parts of a function call.
functionName (arguments)
Note: Arguments don’t have to be there.
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition - name of data get passed as parameters.
function call - actual values get passed as arguments.
Why are function parameters useful?
It functions as a placeholder.
Why are function parameters useful?
Parameters are useful because they function as placeholders.
Adds reusability and adaptability.
What two effects does a return statement have on the behavior of a function?
- Causes the function to return 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?
We log things to the console for debugging purposes.
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?
A function becomes a method once it is passed in as a property for an object. Method has to say where it is coming from.
How do you remove the last element from an array?
You can remove the last element from an array by using the pop( ) method.
How do you round a number down to the nearest integer?
You can round a number down to the nearest integer by using the Math.floor( ) method.
How do you generate a random number?
You can generate a random number in a range by:
Ex: Math.floor(Math.random( ) * 10);
How do you delete an element from an array?
You can delete an element from an array by using the: pop( ) method, shift( ) method, and/or splice( ) method.
How do you append an element to an array?
You can add an element to an array by using the: push( ) method, unshift( ) method, and or splice( ).
How do you break a string up into an array?
You can break a string up into an array by using the split( ) method.
Do string methods change the original string? How would you check if you weren’t sure?
No. You can check by using console.log ( ).
Roughly how many string methods are there according to the MDN Web docs?
A lot. Learn it when you need it.
Is the return value of a function or method useful in every situation?
No.
The push ( ), unshift ( ), pop( ), shift( ), and splice( ) are good examples for when you are solely just trying to manipulate the array.
Roughly how many array methods are there according to the MDN Web docs?
A lot. Learn it when you need it.
Give 6 examples of comparison operators.
==, !=, ===, !==, < , <= , >, and >=
What data type do comparison expressions evaluate to?
Comparison expressions evaluate to Booleans.
What is the purpose of an if statement?
The purpose of an if statement is for decision making.
Is else required in order to use an if statement?
No.
Describe the syntax (structure) of an if statement.
if ( condition ) {
}
What are the three logical operators?
&&, | |, and !.
How do you compare two different expressions in the same condition?
You compare two different expressions in the same condition by using logical && or logical | | operator.
What is the purpose of a loop?
The purpose of a loop is to offer a quick and easy way to do something repeatedly.
What is the purpose of a condition expression in a loop?
The purpose of a condition expression in a loop is to test if the loop-code-block should be executed or not. (To keep going or not)
What does “iteration” mean in the context of loops?
“Iteration” in the context of loops means a cycle of the loop.
When does the condition expression of a while loop get evaluated?
The condition expression of a while loop gets evaluated in the beginning.
When does the initialization expression of a for loop get evaluated?
The initialization expression of a for loop gets evaluated in the beginning.
(initialization = before anything)
When does the condition expression of a for loop get evaluated?
The condition expression of a for loop gets evaluated before each iteration.
(condition = before each iteration)