Javascript Flashcards
What is the purpose of variables?
It allows you to store data
How do youdeclarea variable?
var keyword with the variable name
How do you initialize (assign a value to) a variable?
use the assignment operator ( = )
What characters are allowed in variable names?
Can start with $, _, or any letter
Cannot start with a number
Cannot use - or .
Cannot use keywords like var
What does it mean to say that variable names are “case sensitive”?
happy and hAppy will be two different variables
What is the purpose of a string?
To provide data as text
What is the purpose of a number?
To provide numeric value for a variable or for calculations
What is the purpose of a boolean?
Give a true or false value
What does the=operator mean in JavaScript?
Assigns a value
How do you update the value of a variable?
you can assign the variable to a new value
What is the difference betweennullandundefined?
Null is intentionally left empty, Undefined is a variable with no value
var apple;
VS.
var apple = undefined;
Why is it a good habit to include “labels” when you log values to the browser console?
It allows you to see if you code is working properly and provides clarity
Give five examples of JavaScript primitives.
Number, string, boolean, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Joining of strings using + operator
What purpose(s) does the+plus operator serve in JavaScript?
Addition or concatenation
Adds one value to anoter
What data type is returned by comparing two values (,===, etc)?
Boolean
What does the+=”plus-equals” operator do?
It adds the value on the right side of the operator to the value of the variable on the left side and reassigns it to the variable on the left side
Var motto = fullName + ‘ is the GOAT’
Motto += ‘ is the GOAT’
What are objects used for?
Storing multiple functions or properties of an item
What are object properties?
key value pairs relating to the defined variable
Describe object literal notation.
A variable is defined
There is the opening curling brace for the code block
Properties made of key value pairs separated by commas
closing curly brace of the code block
Semicolon
How do you remove a property from an object?
delete variable.property
What are the two ways to get or update the value of a property?
Reassign the value of the variable
Create a var.property and have a value assigned to it
What are arrays used for?
It stores a list of values that are related to each other
Describe array literal notation
Var is defined
opening brace for the code block
string, numbers, or booleans separated by commas
closing brace for the code block
semicolon
How are arrays different from “plain” objects?
Instead of properties with their own unique key value pairs, the key for each value in an array is represented by its index number
What number represents the first index of an array?
0
What is thelengthproperty of an array?
The number of items in the array
How do you calculate the last index of an array?
Array[Array.length - 1]
What are objects used for?
Storing multiple functions or attributes of an item
What are object properties?
Key value pairs
Describe object literal notation.
A variable being defined
Start of the code block
Properties made of key value pairs separated by commas
Closing of the code block
Semicolon
How do you remove a property from an object?
Delete obj.property
What are the two ways to get or update the value of a property?
Dot notation and bracket notation
What is a function in JavaScript?
A block of code that executes a task and can easily be called on
Describe the parts of a functiondefinition.
Function keyword, an optional name for the function, 0 or more parameters separated by commas enclosed by parentheses, opening curly brace for the code block
Optional return statement
End of the code block
Describe the parts of a functioncall.
The functions name, 0 or more arguments separated by commas enclosed in parentheses
When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
There is no function keyword in the call
The parameter is a representation of the actual argument
There is no code block in a function call
What is the difference between aparameterand anargument?
A parameter is part of a function definition and an argument is part of the function call
Why are functionparametersuseful?
It acts as a placeholder until you have the actual values when the function is called
What two effects does areturnstatement have on the behavior of a function?
It produces a value from the function
It ends the function code block from running anything else
Why do we log things to the console?
Using the log method of the console object console.log();
What is a method?
A function inside of an object
How is a method different from any other function?
It is part of an object while other functions are not
How do you remove the last element from an array?
Pop method (shift method to remove first element)
How do you round a number down to the nearest integer?
Math.floor();
How do you round a number down to the nearest integer?
Math.floor();
How do you generate a random number?
Math.random() The random method of the Math object
How do you delete an element from an array?
Splice method (first number is which index to before which index to start at, second number is how many elements to remove)
How do you append an element to an array?
Push method to add to end of array. Unshift method to add to beginning of array
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 they can only split or concatenate the string or capitalize it.
You can log it to the console to check
Roughly how many string methods are there according to the MDN Web docs?
About 50
Is the return value of a function or method useful in every situation?
No because some methods may give a random value
Roughly how many array methods are there according to the MDN Web docs?
About 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.
> , >=, <, <=, !==, ===
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of anifstatement?
To run different blocks of code based on if that block of code meets a condition
Iselserequired in order to use anifstatement?
No, if you only need the code to run if the condition is true
Describe the syntax (structure) of anifstatement.
If keyword, parentheses with the condition inside, curly brace for the code block, code in the code block to execute followed by semi colon, closing curly brace
What are the three logical operators?
&& logical and, || logical or, ! Logical not
How do you compare two different expressions in the same condition?
By using a logical operator in between them
What is the purpose of a loop?
It checks a condition until it returns false
What is the purpose of aconditionexpression in a loop?
It checks to see if the code should run again or to stop
What does “iteration” mean in the context of loops?
It is each time the loop runs
Whendoes theconditionexpression of awhileloop get evaluated?
The beginning of each loop until the condition is not met
Whendoes theinitializationexpression of aforloop get evaluated?
It is evaluated before the code block runs for the first time
Whendoes theconditionexpression of aforloop get evaluated?
It is evaluated at the beginning of each loop iteration
Whendoes thefinalexpression of aforloop get evaluated?
it is evaluated at the end of each loop iteration
Besides areturnstatement, which exits its entire function block, which keyword exits a loop before itsconditionexpression evaluates tofalse?
Keyword break
What does the++increment operator do?
It adds one to the counter every time the loop runs and drives the loop until it no longer runs
How do you iterate through the keys of an object?
using for in loop
What event is fired when a user places their cursor in a form control?
Focus Event
What event is fired when a user’s cursor leaves a form control?
Blur event
What event is fired as a user changes the value of a form control?
input event
What event is fired when a user clicks the”submit”button within a?
Submit event
What does theevent.preventDefault()method do?
It prevents the browser from following through the the events normal actions
What does submitting a form withoutevent.preventDefault()do?
It will reload the page
What property of a form element object contains all of the form’s controls.
Elements property
What property of a form control object gets and sets its value?
Value property
What is one risk of writing a lot of code without checking to see if it works so far?
you will have to backtrack to find your mistake
What is an advantage of having your console open when writing a JavaScript program?
You can check if everything is working properly and debug as you go
What are objects used for?
Storing multiple methods or properties of an item
What are object properties?
Key value pairs that are attributes of that object
Describe object literal notation.
Object name stored as variable
Opening curly brace for start of the code block
Properties made of key value pairs
Closing curly brace for code block
Semicolon
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 and bracket notation
Why do we log things to the console?
check our work and to debug
What is a method?
a function inside of an object
How is a method different from any other function?
It is part of an object while other functions are not
How do you remove the last element from an array?
pop method
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
splice()
How do you append an element to an array?
push()
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?
No they can only split or concatenate the string or capitalize it.
You can log it to the console to check
Roughly how many string methods are there according to the MDN Web docs?
About 50
Is the return value of a function or method useful in every situation?
no because some methods may give a random value?
Roughly how many array methods are there according to the MDN Web docs?
About 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.
> , >=, <, <=, !==, ===
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of anifstatement?
To run different blocks of code based on if that block of code meets a condition
Iselserequired in order to use anifstatement?
No, if statement will always be true
Describe the syntax (structure) of anifstatement.
If keyword, parentheses with the condition inside, curly brace for the code block, code in the code block to execute followed by semi colon, closing curly brace
What are the three logical operators?
&& logical and, || logical or, ! logical not
How do you compare two different expressions in the same condition?
By using a logical operator in between them
What is theevent.target?
It is the element that the target was triggered from
What is the affect of setting an element todisplay: none?
The element will disappear from the webpage
What does theelement.matches()method take as an argument and what does it return?
It takes a css selector, as a string, for the argument and it returns true if the element matches the selector and false if not
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?
Logging e.target to make sure it is only firing when it is supposed to
Logging the nodelists to visualize what we were looping through
Checking to see if the tabs were changing classes properly when being clicked
When checking to see if the attribute values were matching each other
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?
We would need to make a separate event listener for the new tab and view divs.
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?
We would have to write a long list of if else statements and check conditions for each index of the nodelists
What is JSON?
Text format for representing javascript object syntax
What are serialization and deserialization?
Converting an object into bytes that can be saved or transferred over a network
Deserialization is taking the bytes and converting it back to the object
Why are serialization and deserialization useful?
Provides a useful method to transport data from one place to another
How do you serialize a data structure into a JSON string using JavaScript?
The JSON.stringify() method
How do you deserialize a JSON string into a data structure using JavaScript?
The JSON.parse() method
How do you store data inlocalStorage?
setItem()
How do you retrieve data fromlocalStorage?
getItem()
What data type canlocalStoragesave in the browser?
strings
When does the’beforeunload’event fire on thewindowobject?
Before a document and resources are about to be unloaded