JavaScript Flashcards
What is the purpose of variables?
Store data, information in variables
Data can change (vary) each time a script runs
How do youdeclarea variable?
var variableName;
Keywords: var, let, const
How do you initialize (assign a value to) a variable?
variableName = value;
= : assignment operator
What characters are allowed in variable names?
Must begin with a: letter, $, or _
can contain numbers (numbers CANNOT start variable name)
What does it mean to say that variable names are “case sensitive”?
Declare different variables depending on upper or lower case usage:
score vs. Score
What is the purpose of a string?
To store text, letters and other characters
Enclosed within a pair of quotes
Frequently used to add new content into a page and they can contain HTML markup
What is the purpose of a number?
To store numerical data
Calculate, determine size of screen, move position of an element on a page, set the amount of time an element should take to fade in
What is the purpose of a boolean?
Boolean data types store one of two values: true or false
Purpose: Make decisions
Used like a switch: on or off
Helpful when determining which part of a script should run
What does the=operator mean in JavaScript?
Assignment operator: used to assign a value to a variable
How do you update the value of a variable?
variable = newValue;
Keyword is only necessary when declaring a new variable
What is the difference betweennullandundefined?
Null is an assigned value, purposefully designated empty by developer while Undefined is returned by the browser
Null: represents a reference that points, generally intentionally, to a nonexistent or invalid object or address
Undefined: automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments
Why is it a good habit to include “labels” when you log values to the browser console?
Helpful to identify what you are logging to the console
Give five examples of JavaScript primitives.
string, number, boolean, null, undefined
bigint, symbol
(7 total)
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Process of joining together two or more strings to create one new string
What purpose(s) does the+plus operator serve in JavaScript?
Adds one value to another
Sums numerical data or string concatenation
What data type is returned by comparing two values (<,>,===, etc)?
Boolean
What does the+=”plus-equals” operator do?
Addition assignment: Adds the value of the right operand to a variable and assigns the result to the variable
What are objects used for?
Group together a set of variables and functions to create a model of something
What are object properties?
Variables that are part of an object
Describe object literal notation.
var object = {
key: value,
propertyName: value,
key: function() {
}
};
How do you remove a property from an object?
Delete operator
delete object.property;
What are the two ways to get or update the value of a property?
Dot notation using member operator (.)
object.propertyName = newValue;
Square bracket syntax
object[‘propertyName’] = newValue;
What are arrays used for?
Store a list of variables and set of values that are related to each other
Group together like data
Describe array literal notation.
var array = [item1, item2, item3,… lastItem];
How are arrays different from “plain” objects?
Special type of object, hold a relates set of key/value pairs (like all objects), but the key for each value is its index number
Arrays do not have individually names pieces of data, ordered in numeric values, 0 index
.length is applicable to array, don’t know how many data is in an object
.push() is applicable to array, dot notation or square bracket syntax to assign properties to objects
What number represents the first index of an array?
0
What is thelengthproperty of an array?
Returns the number of items in the array
array.length
How do you calculate the last index of an array?
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 functiondefinition.
function example(parameter1, parameter2, parameter3, …) {
//…code…
return;
}
function keyword, (optional) name, parameters surrounded by () and separated by ,’s, opening curly brace for the code block, (optional) return statement, closing curly brace to end the code block
Describe the parts of a functioncall.
example(arg1, arg2, arg3);
name of the function, arguments surrounded by () and separated by ,’s
When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
Function definition has a function keyword!, parameters, code block!, and curly braces
Function call has name and arguments
What is the difference between aparameterand anargument?
Parameter is a placeholder, a variable whose value is not known until we call the function and pass an argument
Define functions use parameter, Call functions use arguments
Why are functionparametersuseful?
Placeholders
Allows us to write reusable code
Mutability to the code
What two effects does areturnstatement 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?
Powerful tool for debugging and an easy way to inspect your variables in the browser
What is a method?
A function which is a property of an object
Instance methods - built-in tasks performed by an object instance
Static methods - tasks that are called directly on an object constructor
How is a method different from any other function?
Method is associated with an object, while a function is not
How do you remove the last element from an array?
.pop()
How do you round a number down to the nearest integer?
Math.floor(x)
How do you generate a random number?
Math.random()
range 0 to less than 1 (inclusive of 0, but not 1)
How do you delete an element from an array?
Math.splice(start, delCount)
How do you append an element to an array?
Math.push(element0, element1, … ,elementN)
How do you break a string up into an array?
.split()
Do string methods change the original string? How would you check if you weren’t sure?
No, string methods return the result of the method call as an array of substrings
Strings are immutable
console.log(string)
Is the return value of a function or method useful in every situation?
No, some functions don’t return any value
Example: displayMessage(), no specific value is returned when the function is invoked, it just makes a box appear somewhere on the screen
Generally, a return value is used where the function is an intermediate step in a calculation of some kind. You want to get to a final result, which involves some values that need to be calculated by a function. After the function calculates the value, it can return the result so it can be stored in a variable; and you can use this variable in the next stage of the calculation.
Give 6 examples of comparison operators.
<, >, <=, >=,
==, === (strict equal to: both type and value), !
=, !== (strict not equal to)
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of anifstatement?
Evaluates (or checks) a condition. If the condition evaluates to true, any statement in the subsequent code blocks are executed.