JavaScript Flashcards
What is the purpose of variables?
To store bits of data that the computer needs in order to do its tasks
How do you declare a variable?
with the keyword “var” followed by the identifier or variable name
How do you initialize (assign a value) to a variable?
with the assignment operator (=)
What characters are allowed in variable names?
letters of the alphabet, dollar sign ($), underscore, numbers (can’t start with number)
What does it mean to say that variable names are “case sensitive”?
e.g. the variables “test” and “Test” are different.
What is the purpose of a string?
sequence of characters that represent text
What is the purpose of a number?
represents a numeric data type that allows us to perform mathematical operations on it
What is the purpose of a boolean?
logical data type that only returns true or false. Useful for conditionals. Lets the computers ultimately decide what to do or not do.
What does the = operator mean in JavaScript
assignment operator. Assigns the value of the right to whatever is on the left.
How do you update the value of a variable?
the assignment operator can update the value of a variable. var keyword not necessary to update value
What is the difference between null and undefined?
undefined: JavaScript’s method of saying “empty”
null: developer’s way of saying “empty”; assigned intentionally
Why is it a good habit to include “labels” when you log values to the browser console?
Shows the user which logs represent which values. Logs without labels can cause confusion
Give five examples of JavaScript primitives
string, number, bigint, boolean, undefined, symbol, null
primitive: data that is not an object and has no methods. primitives can NOT be altered
What data type is returned by an arithmetic operation?
number
What is string concatenation?
using + to join together multiple things into a single string
What purposes does the plus (+) operator serve in JavaScript?
- addition
- string concatenation
What data type is returned by comparing two values (< , > , ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds/concatenates the value on the right with the value on the left and then assigns it to the variable on the left.
What are objects used for?
to group relevant data together
What are object properties?
variables that live within an object
Describe object literal notation
declare a variable and assign it a value of a curly brace block. The contents of the curly brace block can be empty or consist of key, value pairs
How do you remove a property from an object?
Using the delete operator
delete object.property
What are the two ways to get or update the value of a property?
dot notation (object.property = value)
bracket notation (object[‘property’] = value)
What are arrays used for?
Useful when working with lists or groups of similar data
Describe array literal notation
var varName = [list contents (separated by comma)]
How are arrays different from “plain” objects?
values assigned to index numbers rather than keys
order is preserved in arrays
What number represents the first index of an array?
0
What is the length property of an array?
returns how many things are stored in the list
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
A block of code that has a specific purpose and can be reused as many times as needed
Describe the parts of a function definition
keyword ‘function’ followed by optional name, zero to however many parameters, code block, optional return statement
function name(parameters,..) { return (optional) }
Describe the parts of a function call
functionName(arguments);
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function call doesn’t need the code block, just function name followed by any arguments
function definition needs the initial code block and has parameters
What is the difference between a parameter and an argument?
Parameter is for function definition
argument is for function calls
Why are function parameters useful?
Allows the function to be dynamic
Can achieve different results depending on the data passed in
What two effects does a return statement have on the behavior of a function?
exits the code block immediately after executing a return line. (ends the function)
causes function to produce a value we can use in the program
Why do we log things to console?
In order to reduce confusion by keeping track of variables and making sure the data output is what we expected
What is a method?
function that is a property of an object
How is a method different from any other function?
They are properties of an object so they must be accessed as if they were a property (dot notation)
How do you remove the last element from an array?
array.pop( ); returns the removed element from the array
How do you round a number down to the nearest integer?
Math.floor( )
How do you generate a random number?
Math.random( ); Returns a number from 0 (inclusive) to 1 (not inclusive)
How do you delete an element from an array?
array.splice(n, x, y,…);
n: starting index
x: number of elements to remove (optional)
y: elements to add to the array (optional)
returns an array of the deleted elements
How do you append an element to an array?
array. unshift( ); adds to beginning of array
array. push( ); adds to end of an array
How do you break a string up into an array?
string.split(separator);
separator describes where the split should occur
Do string methods change the original string?
How to check if not sure?
No, can test in console by trying to change a string then console log the original string
Roughly how many string methods are there according to MDN Web docs?
37
Is the return value of a function or method useful in every situation?
No
Roughly how many array methods are there according to the MDN Web docs?
34
What 3 letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators
, <=, >=, ===, !==
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement
Lets us choose what to do
Is else required in order to use an if statement?
no
Describe the syntax of if statement
if (conditional) {
code block
}
What are the three logical operators?
&& (and)
|| (or)
! (Logical not; inverts boolean)
How do you compare two different expressions in the same condition?
with logical operators (&& or ||)
What is the purpose of a loop?
When you want to repeat a chunk of code while certain conditions aren’t met
What is the purpose of a condition expression in a loop?
It makes it so loops have an exit point (not infinite loop)
What does “iteration” mean in the context of loops?
Each time the computer passes through the loop’s code block