JavaScript Flashcards
What is the purpose of variables?
to store data that can later be used when called upon.
create permanence in data
How do you declare a variable?
var variableName
How do you initialize (assign a value to) a variable?
variableName = value;
What characters are allowed in variable names?
letters
numbers (can’t start the variable name)
underscore
dollar sign
What does it mean to say that variable names are “case sensitive”?
fullName and FullName are two different variables
What is the purpose of a string?
used when working with any kind of text
a set of characters in a row that javascript won’t care about
What is the purpose of a number?
mathematical operations
What is the purpose of a boolean?
hold true/false data types
helpful when determining which part of a script should run
What does the = operator mean in JavaScript?
assignment operator
stores value to a variable
How do you update the value of a variable?
variableName = new value;
What is the difference between null and undefined?
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments. (automatic assignment from the browser)
null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. (null only exists because its been ASSIGNED–> purposeful)
Why is it a good habit to include “labels” when you log values to the browser console?
So you know what data you’re looking at.
Sometimes, it’s helpful to put more than one console.log() in your code to help you debug.
Give five 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
What purpose(s) 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?
The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable.
What are objects used for?
objects group together a set of variables and functions that are related to each other
What are object properties?
variables that are part of objects. They tell us about the object
Describe object literal notation.
you create an object by declaring the variable and assigning its properties and their corresponding values inside of curly brackets or instead use empty curly brackets and add properties later
ex)
var hotel = {
name: ‘Quay’,
rooms: 40,
booked: 25,
}
How do you remove a property from an object?
delete object.property;
What are the two ways to get or update the value of a property?
dot notation
object.propertyName = propertyValue;
square bracket notation
object[‘propertyName’] = propertyValue;
What are arrays used for?
group similar data types together by storing values in a list
Describe array literal notation.
var arrayName = [‘item 1’, item2, ‘item 3’, ‘item 4’, item5];
How are arrays different from “plain” objects?
The key for each value is its index number (starting at 0)
Don’t have individually named pieces of data, but rather are numerically indexed.
You can determine the number of items in an array (length) but not in a plain object.
Use a method (push) to add stuff to an array, use dot notation or bracket notation with assignment operator to add stuff to a plain object.
Arrays have an order, objects do not
What number represents the first index of an array?
0
What is the length property of an array?
arrayName.length
returns the number of items stored in the array
How do you calculate the last index of an array?
subtract the length of the array by 1
lastIndex = arrayName.length - 1;
What is a function in JavaScript?
a block of code that gives step by step instructions to accomplish a specific task
Describe the parts of a function definition.
// [1] [2] [3]
function example(parameter1, parameter2, parameter3, …) { // [4]
// …more JavaScript code…
// [5]
return;
} // [6]
- The function keyword to begin the creation of a new function.
- An optional name. (Our function’s name is sayHello.)
- A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)
- The start of the function’s code block, as indicated by an { opening curly brace.
- An optional return statement. (Our sayHello function doesn’t have a return statement.)
The end of the function’s code block, as indicated by a } closing curly brace.
Describe the parts of a function call.
// [1] [2]
example(arg1, arg2, arg3);
- The function’s name. Again, our function’s name is sayHello.
- A comma-separated list of zero or more arguments surrounded by () parentheses.
Our sayHello function does not take any arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function definition contains the steps on what to do with the data (function code block) (parameter)
the function call invokes the function and passes through the data that’s to be worked on (argument)
What is the difference between a parameter and an argument?
parameter –> placeholder (function definition)
argument –> actual data (function call)
Why are function parameters useful?
give us the ability to have mutability in what occurs when we call a function (diff parameters allow for the return of different values)
What two effects does a return statement 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.
What is a method?
A function that’s assigned to the property of an object
Why do we log things to the console?
So we can check our code along the way to make sure it’s doing what it’s supposed to do. helps with debugging
What is a method?
A method is a function stored in the property of an object.
How is a method different from any other function?
located within an object
called with the object
How do you remove the last element from an array?
pop() method
array.pop()
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?
array.pop() removes last element from the array
array.shift() removes first element of the array
array.splice() removes indicated item(s) from the array
How do you append an element to an array?
array.push() appends an element to the end of the array
How do you break a string up into an array?
element.split()
Do string methods change the original string? How would you check if you weren’t sure?
No, strings are immutable
Use console.log on the original variable
Roughly how many string methods are there according to the MDN Web docs?
50
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?
40
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.
=== strictly equals to
!== strictly is 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?
boolean (true / false)
What is the purpose of an if statement?
to set up a condition to compare to. If the condition evaluates to true, any statements in the subsequent code block are executed
Is else required in order to use an if statement?
nope
Describe the syntax (structure) of an if statement.
keyword + condition + opening curly brace + code to execute if value is true + closing curly brace
if (score >=50) { congratulate(); }
What are the three logical operators?
&& and
|| or
! not (inverts boolean value)
How do you compare two different expressions in the same condition?
Use the logical and operator (&&)
What is the purpose of a loop?
offer the ability to repeat functionality
What is the purpose of a condition expression in a loop?
Evaluate a condition to determine whether or not the loop should run
What does “iteration” mean in the context of loops?
one time that the loop code block gets run
When does the condition expression of a while loop get evaluated?
before each iteration of the code block executes
When does the initialization expression of a for loop get evaluated?
Before anything runs (just once)
When does the condition expression of a for loop get evaluated?
After initialization on the first iteration
before the loops code block and after the final expression
When does the final expression of a for loop get evaluated?
after the code block runs, before the next iteration
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 the counter variable and saves it to that variable
What is a “model”?
A representation of something
a copy of the webpage loaded in the browser
Which “document” is being referred to in the phrase Document Object Model?
The HTML code / the webpage that’s loaded in the browser
What is the word “object” referring to in the phrase Document Object Model?
The node objects within the HTML code/ webpage
What is a DOM Tree?
Is a kind of tree whose nodes represent an HTML document’s contents.
All the necessary info to give the characteristics of a single element
Give two examples of document methods that retrieve a single element from the DOM.
document.querySelector()
document.getElementById()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
If your script needs to use the same element(s) more than once
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
Argument: A CSS selector as a string
Returns: the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
Argument: a CSS Selector as a string
Returns: All matching elements in a node list