javaScript Flashcards
What is the purpose of variables?
Storing data and going back to use it
How do you declare a variable?
Using keyword “var”
How do you initialize (assign a value to) a variable?
=;
What characters are allowed in variable names?
Must begin with a letter, _underscore, or $ dollar-sign
can’t start with number
What does it mean to say that variable names are “case sensitive”?
Uppercase and lowercases matter
Must be consistent with captializations
What is the purpose of a string?
String data types are in quotes
Used to add new content into a page
What is the purpose of a number?
Used to mathematical tasks
What is the purpose of a boolean?
To declare it true or false
What does the = operator mean in JavaScript?
Assigning a value to the variable
How do you update the value of a variable?
Assigning different values to the variable
What is the difference between null and undefined?
Null - represents a reference point for nonexistent or invalid objects (space for value) (purposeful)
Undefined - how JS tells you nothing (not purposeful) not purposed result of a declared object
Why is it a good habit to include “labels” when you log values to the browser console?
Describes the description of the value, in order to debug
ex:
var debug = letsgo;
console.log(“so that you can debug:”, debug);
Give five examples of JavaScript primitives.
Boolean, numbers, undefined, string, null
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
It’s adding 2 strings together
Strings are textual content
What purpose(s) does the + plus operator serve in JavaScript?
Addition of numbers
And also concatenation of strings
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
The Addition-assignment operator adds value to the variable.
What are objects used for?
To hold its properties and functions in order to create a model
What are object properties?
Properties are tells us specific characteristics of the object that relate to each other
Properties Variables glued to objects
Describe object literal notation.
Declares the variable equating to curly braces
How do you remove a property from an object?
Use “delete” operator
What are the two ways to get or update the value of a property?
Using the dot notation to the object
Using square bracket notation with the property inside
What are arrays used for?
Container for lists
Special type of objects.
Describe array literal notation.
Square objects
How are arrays different from “plain” objects?
They hold a related set of key/value, but the key for each value is its numerical indexes (not alpha numeric)
In an array, order of properties are important
-Square brackets instead of curly braces
What number represents the first index of an array?
0 index
What is the length property of an array?
Holds the number of items in the array
How do you calculate the last index of an array?
.length - 1;
What is a function in JavaScript?
A JavaScript function is a block of code designed to perform a particular task
Describe the parts of a function definition.
Function keyword, function name, parentheses, curly braces for definition block
Describe the parts of a function call.
Function keyword, function name, parentheses to pass arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call - function keyword, name of function and arguments, doesn’t have curly braces after
What is the difference between a parameter and an argument?
Parameters are empty variables - placeholders (function def)
Arguments are actual values - actual values (function call)
Why are function parameters useful?
Useful to store and run future data.
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. (return a result)
Prevents any more code in the function’s code block from being run (stops the code)
Why do we log things to the console?
In order to debug and check your progress
What is a method?
Function which is a property of an object
Built-in tasks that operate
How is a method different from any other function?
Can be Property of an object
How do you remove the last element from an array?
.pop() method
How do you round a number down to the nearest integer?
Using Math.floor method (which is a static method using . dot notation)
How do you generate a random number?
Use Math.random method
How do you delete an element from an array?
.splice( ) method
How do you append an element to an array?
.push ( ) method
How do you break a string up into an array?
split ( ) method
Do string methods change the original string? How would you check if you weren’t sure?
no
Is the return value of a function or method useful in every situation?
no
Give 6 examples of comparison operators.
== is equal to ! = is not equal to === strictly equal to ! == strictly not equal to > greater than < less than
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
Declares and evaluates a conditional statement in order to make a decision
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
If - keyword
(score > 0 ) - condition expression
{ opening curly brace for statement 1
} curly brace for statement 1
What are the three logical operators?
&& logical and
| | logical or
! logical not
How do you compare two different expressions in the same condition?
Place logical operators in between
What is the purpose of a loop?
To check the conditions until limit of false or the count of the condition
What is the purpose of a condition expression in a loop?
It serves as a counter, stops the loop
What does “iteration (repetition)” mean in the context of loops?
It repeats the condition expression
When does the condition expression of a while loop get evaluated?
After declaring the counter
When does the initialization expression of a for loop get evaluated?
At statement 1, before the condition ONCE
When does the condition expression of a for loop get evaluated?
final expression and before the code block, before each iteration
When does the final expression of a for loop get evaluated?
Running after the code block
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 1 to the variable
How do you iterate through the keys of an object?
It takes the object that you want to loop over as an argument and returns an array containing all properties names (or keys)
For-in loops
Navigate over objects
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a ?
Submit (event occurs)
What does the event.preventDefault() method do?
Default prevents a form (but event still occurs)
What does submitting a form without event.preventDefault() do?
If you don’t prevent, the page refreshes
What property of a form element object contains all of the form’s controls.
Element’s property (use the elements property to get the elements
What property of form a control object gets and sets its value?
The value of property retrieve or give value
What is the event.target?
The target property of the Event interface is a reference to the object onto which the event was dispatched
What is the affect of setting an element to display: none?
it will not display on the viewpage
What does the element.matches() method take as an argument and what does it return?
argument 1 - selectorString is a string representing the selector to test.
argument 2 - result is a boolean value.
How can you retrieve the value of an element’s attribute?
getAttribute() method
At what steps of the solution would it be helpful to log things to the console?
console.log at every step
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
You would have to add event-listener to all
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
Use conditional statements
How to you store data in localStorage?
localStorage.setItem(keyName, keyValue)
How to you retrieve data from localStorage?
localStorage.getItem
What data type can localStorage save in the browser?
JSON Strings
When does the ‘beforeunload’ event fire on the window object?
beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.