JavaScript Flashcards
What is the purpose of variables?
Represents and stores the values in our data
How do you declare a variable?
By using var, let, or const before naming the variable
Ex. var variableName
How do you initialize (assign a value to) a variable?
By using var to declare our variable, then an equal sign, and then our value
What characters are allowed in variable names?
$, letters, numbers, underscore
What does it mean to say that variable names are “case sensitive”?
A capital A is not the same as a lowercase a. Uppercase and lowercase letters are different values in JS.
What is the purpose of a string?
Allows us to store and manipulate text
What is the purpose of a number?
Allows us to calculate sums, count, and perform other tasks (i.e determining size, moving position of an element, etc.)
What is the purpose of a boolean?
Gives us a true or false value that tells our program which script to run
What does the = operator mean in JavaScript?
This is the assignment operator. It assigns the value on the right to the variable on the left
How do you update the value of a variable?
Put the variable on a new line with a different value assigned to it
What is the difference between null and undefined?
Null - an intentional absence of a value
Cannot be created by javascript only by humans
Undefined - accident; no one plans for this to have a value
Why is it a good habit to include “labels” when you log values to the browser console?
The console does not log the variable name in the output so it is good practice to include it ourselves so that we see what the purpose of our value is (helps other people understand your data as well)
Give five examples of JavaScript primitives.
Undefined , null , boolean , string and number
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
The process of adding two strings together to create a new string
What purpose(s) does the + plus operator serve in JavaScript?
Addition - math purposes
Concatenation - adding strings together
What data type is returned by comparing two values (, ===, etc)?
A boolean true or false
What does the += “plus-equals” operator do?
Adds the value of the right operand to a variable and assigns the result to the variable.
What are objects used for?
An object is a collection of properties, and a property is an association between a name (or key) and a value.
A data type that allows you to store and manipulate data.
What are object properties?
Object properties are variables/key that gives us information about an object
Describe object literal notation.
Var declaration, then we have the object, within the curly opening and closing curly brace we have the keys: which are the property names and its value & the method
How do you remove a property from an object?
By using the delete operator and then object.property
Ex. delete hotel.name;
What are the two ways to get or update the value of a property?
Dot notation
Put the object name with a dot and then assign the new variable on the right
Ex. hotel.name = ‘new value’
Square brackets
Ex. hotel[‘name’] = ‘new value’
What are arrays used for?
They are used to store a list of values
Describe array literal notation.
Var declaration, variable name, and [ ]
How are arrays different from “plain” objects?
Objects represent things in real life while arrays hold a list of values;
the key for each value in an array represents a number; all arrays come with a property which is length that is always being updated
What number represents the first index of an array?
zero
What is the length property of an array?
Length property counts how many items are in our array
How do you calculate the last index of an array?
n-1
What is a function in JavaScript?
a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.
A reusable block of code
Describe the parts of a function definition.
Also called a function declaration:
- Function keyword
- Optional name
- Comma-separated list of zero or more parameters
- Opening curly brace to indicate start of our function
- Optional return statement
- Closing curly brace to indicate end of function code block
Describe the parts of a function call.
- Function name
- A comma-separated list of zero or more arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function definition defines what needs to be done in order to achieve our goal; the call function helps executes those needs to achieve that goal
Function definition:
Has function keyword and code block
Function call:
Has arguments
What is the difference between a parameter and an argument?
Parameters:
- Have a value that is not known
- Placeholders for arguments
Arguments:
- Have a value that is known
- Replaces the parameter once it is called in
Why are function parameters useful?
- It holds our value until an argument gets called into it
- Helps us get reusable behaviors
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.
Why do we log things to the console?
To ensure that our code is working and correct (debugging purposes)
What is a method?
A method is a function that belongs to an object and executed with that object as a context.
or
A function that is a property of an object
How is a method different from any other function?
A method is associated with an object while a function is not
How do you remove the last element from an array?
By using pop method
How do you round a number down to the nearest integer?
With Math.floor
How do you generate a random number?
Math.random( ) function
How do you delete an element from an array?
Splice ( )
How do you append an element to an array?
Append ( )
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?
Strings are immutable; therefore they cannot be changed - only replaced. We can check by logging it through the console.
Roughly how many string methods are there according to the MDN Web docs?
45-60; there’s a lot lol
Is the return value of a function or method useful in every situation?
No, because it is optional. Sometimes the value of a return is not useful while some are
Roughly how many array methods are there according to the MDN Web docs?
40-50
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.
== - is equal to
!= - is not equal to
=== - strict equal to
>
- greater than
< - less than
> = - greater than or equal to
What data type do comparison expressions evaluate to?
A boolean true or false
What is the purpose of an if statement?
Evaluates our condition to see if it is true or false
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (condition) {
statement1
} else {
statement2
What are the three logical operators?
&& - logical and
| | - logical or
! - logical not
What is the purpose of a loop?
It checks a condition repeatedly
What is the purpose of a condition expression in a loop?
- Keeps our loop running as long as the condition is true
- Responsible for stopping
What does “iteration” mean in the context of loops?
- a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met
- Each time a loop runs
When does the condition expression of a while loop get evaluated?
-An expression is evaluated before each pass through the loop.
When does the initialization expression of a ‘for loop’ get evaluated?
It’s executed once at the beginning of the loop
or
Evaluated once before the loop begins
When does the condition expression of a for loop get evaluated?
Before each loop iteration
aka if the condition is true, it shall execute
When does the final expression of a for loop get evaluated?
At the end of each loop iteration and before the condition runs
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, reassign, and substitutes one to our operand
How do you iterate through the keys of an object?
Using the for in statement.
Why do we log things to the console?
To label our values to make it easier for others and ourselves to understand
Good for debugging
What is a “model”?
A representation of an object, thing, person, etc.
Which “document” is being referred to in the phrase Document Object Model?
The browser page/HTML page
What is the word “object” referring to in the phrase Document Object Model?
The properties, methods, and events available for manipulating and creating web pages
Data object in JS
What is a DOM Tree?
Also known as a node tree; it is a structure that represents the parent node and the child node branches of the parent nodes
Give two examples of document methods that retrieve a single element from the DOM.
getElementById( )
querySelector( )
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll( )
Why might you want to assign the return value of a DOM query to a variable?
Assigning a variable to our element will allow us to work with it more than once
Stores location of the node
What console method allows you to inspect the properties of a DOM element object?
console.dir ( )