Javascript Flashcards
What is the purpose of variables?
to store data/information
How do you declare a variable?
with a variable’s keyword and a variable name (ex. var fullName)
How do you initialize (assign a value to) a variable?
by giving the variable a value using an assignment operator (usually =)
What characters are allowed in variable names?
Variable names cannot contain space
names must begin with a letter, underscore, or dollar sign
variable names can only contain letters, numbers, underscores, or dollar signs
variable names are case sensitive
What does it mean to say that variable names are case sensitive?
variables can store different values if using different casing even with the same word(s).
What is the purpose of a string?
store text data
What is the purpose of a number?
store numeric data
What is the purpose of a boolean?
act as an on/off switch to determine whether a script should run
aka makes decisions
What does the “=” operator mean in Javascript?
assignment operator
assigns a value to a variable
How do you update the value of a variable?
variableName = new value
you don’t need the variable keyword.
What is the difference between “null” and “undefined”?
undefined is the computer saying “nothing”
null is the programmer/human saying “nothing”
vacant lot versus parking lot
Why is it a good habit to include “labels” when you log values to the browser console?
provides clarity to other programmers as to where the values are coming from
good reminder for yourself as well
Give 7 examples of javascript primitives:
strings numbers booleans null undefined bigint symbols
What data type is returned by an arithmetic operation?
numeric
What is string concatenation?
combining two or more strings together to make a new longer string
strings are immutable
What purpose(s) does the “+” operator serve in Javascript?
concatenate strings
add numbers together
What data type is returned by comparing two values (, ===, etc)?
boolean
what does the += operator do?
variable = variable + value
What are objects used for?
Helps us to store and collect data in pairs or groupings.
What are object properties?
key:value pairs
Describe object literal notation:
{}
How do you remove a property from an object?
use the delete keyword
delete object.property;
What are the two ways to get or update the value of a property of an object?
Dot notation object.property = “new value”;
Square bracket notation object[“property”] = “new value”
What are arrays used for?
storing list data
Describe array literal notation:
[ ]
How are arrays different from ‘plain’ objects?
arrays don’t have individually named indexes, only numeric indexes.
arrays have a property length that is constantly changing based on the number of values in an array
changing data in arrays is different than objects due to the multiple methods/functions we are able to use.
What number represents the first index of an array?
0
What is the length property of an array?
It stores the total number values in an array.
How do you calculate the last index of an array?
array.length - 1
what is a function in javascript?
a set of code that performs a task and is reusable.
Describe the parts of a function definition:
- keyword function
- name (optional)
- a comma separated list of parameters (optional), between ( )
- { indicating start of code block
- return statement (optional)
- } end of the code block for the function
Describe the parts of a function call:
- functions name
2. comma separated list of arguments (optional) inside ( ).
When comparing them side-by-side, what are the differences between a function call and a function definition?
In a function call there is no code block or function keyword. There is also no return statement in a function call.
functionName( ); is a function call
What is the difference between a parameter and an argument?
we declare parameters
we pass arguments
parameters are essentially placeholders for arguments
Why are function parameters useful?
We can vary the results of the function by using parameters. Without parameters the function will return the same result each time it is called.
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 check to see if our output is correct. It is a debugging tool.
It is also a way to inspect our variables
What is a method?
A method is a function, which is technically a property of an object.
How is a method different from any other function?
a function can be called directly by its name
a method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation
How do you remove the last element of an array?
the pop( ) method allows you to remove the last element from an array and returns that element
How do you round a number down to the nearest integer?
Math.floor( ) function returns the largest integer less than or equal to a given number.
How do you generate a random number?
Math.random( ) function returns a floating-point, pseudo-random number in the range between 0 to less than 1. (it can be 0 but it can’t be 1)
How do you delete an element from an array?
The splice ( ) method changes the contents of an array by removing or replacing existing elements.
arr.splice(0, 1, ‘feb’)
in the example above, we remove 1 index starting at the 0 index and replace it with the string ‘feb’.
How do you append an element to an array?
You can use the push ( ) method.
Append = add to the end
How do you break a string up into an array?
The split ( ) method divides a string into an ordered list of substrings and puts the substrings into an array, and returns the array
var.split(‘ ‘);
Do string methods change the original string? How would you check?
strings are immutable so you cannot modify the original string
inspect your values to see if it has changed
How many string methods are there roughly according to MDN?
45-50
Is the return value of a function or method useful in every situation?
No, a good example would be the push ( ) method as it returns the new length of the array. Not always is that useful information.
Roughly how many array methods are there according to MDN?
35 ish
Give 6 examples of comparison operators:
>< === !== >= <=
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
to help make decisions when conditions are either met or not met
Is else required to use an if statement?
no
Describe the syntax structure of an if statement:
if (condition) {
statement1
}
condition = an expression that is considered to either be truthy or falsy statement1 = statement that is executed if the condition is truthy
can have more than one statement
What are three logical operators?
logical AND (&&) logical OR (||) logical NOT (!)
How do you compare two different expressions in the same condition?
Using the logical AND or OR operators
What is the purpose of a loop?
it is a way to repeat something more than once without having to type it out again and again
What is the purpose of the condition expression in a loop?
it tells the loop when to stop when the condition is no longer true
What does iteration mean in the context of loops?
each iteration is a single time that the code block runs
When does the condition expression of the while loop get evaluated?
before each iteration to decide whether the loop should run again
When does the initialization expression of the for loop get evaluated?
at the beginning of the first iteration and not after. first step in the loop.
When does the condition expression of a for loop get evaluated?
before each iteration of the loop and after the initialization is evaluated
When does the final expression of a for loop get executed?
after the code block runs and before the condition for the next iteration is evaluated
Besides a return statement, which keyword exits a loop before its condition evaluates to false?
break statement
What does the ++ increment operator do?
adds one to its operand and returns a value
How do we iterate through the keys of an object?
use a for in loop
What is a “model”?
a replica of something
Which “document” is being referred to in the phrase Document Object Model?
the html document
What is the word “object” referring to in the phrase Document Object Model?
the data type object in javascript
javascript object that is modeling the html document
the DOM is not literally the html doc
What is the DOM tree?
a relationship tree of a parent element (node) and all of it child nodes and content within
Give 2 examples of document methods that retrieve a single element from the dom:
getElementById
querySelector
Give 1 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?
so you can access the value quickly later on if needed
What console method allows you to inspect the properties of a DOM element object?
console.dir() method
Why would the script tag need to be placed at the bottom of the HTML content instead of the top?
So we can let the html document load first
the elements need to exist before we apply and JS code to the content
What does document.querySelector() take as its argument and what does it return?
a css selector and returns only the first of the matching elements
What does document.querySelectorAll() take as its argument and what does it return?
a css selector in string form and returns a node list of all elements with the CSS selector that matches
Why do people use the “$” in javascript variables with DOM?
It is not a rule but is common to store dom values with a $ at the beginning of the variable.
What is the purpose of events and event handling?
to update the webpage and help the web page feel more interactive for the user