JavaScript Flashcards
What is the purpose of variables?
to store data/ information that the script will use later
How do you declare a variable?
var, let, const
How do you initialize (assign a value to) a variable?
var variableName = value;
What rules are allowed in naming variable names?
must begin with letter, $, or _ and must NOT start with a number
can contain characters above but not - or .
cannot use existing keywords like var
case sensitive
camel case
What does it mean to say that variable names are “case sensitive”?
score != Score
What is the purpose of a string?
to include text data such as names… etc characters
What is the purpose of a number?
to store numerical data, integers…. decimals..
What is the purpose of a boolean?
to store logical data, true false, 1 and 0,
What does the = operator mean in JavaScript?
assignment operator, assigns value to variables
How do you update the value of a variable?
by recalling the variable name and assigning with a new value after the first assignment line
var number = 1;
number = 2;
console.log(number)
>2
What is the difference between null and undefined?
difference is that null is derived from an object and points to non-existent/ invalid object
undefined is a value assigned to variables that have not been assigned a value
null says the there is no box in existence, undefined says the box is empty
null is assigned to be empty on purpose –> “empty”
never assign anything with undefined
Why is it a good habit to include “labels” when you log values to the browser console?
to specify what the varible the input is coming from
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?
combining string values using + arithmetic operator
What purpose(s) does the + plus operator serve in JavaScript?
addition operator and string concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
addition assignment, adds left side to value to variable current value.
What are objects used for?
objects are used to provide properties or characteristics to one ‘object’
What are object properties?
variables of that object that describe a value
Describe object literal notation.
{ property: value, …. }
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?
object.property = new value object['property'] = new value
What are arrays used for?
to store a list of items or objects
Describe array literal notation.
[value, value, value]
How are arrays different from “plain” objects?
ordered, the property of arrays is its index
What number represents the first index of an array?
0
What is the length property of an array?
How do you calculate the last index of an array?
array.length gives length of array
array[array.length - 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
allow you to package up code for use later in your program
Describe the parts of a function definition.
Describe the parts of a function call.
function functionName (parameters) { code block }
functionName(argument)
When comparing them side-by-side, what are the differences between a function call and a function definition?
call does not have code blocks just an argument
What is the difference between a parameter and an argument?
when defining a function you call parameters but when you call a function you define arguments.
Why are function parameters useful?
because it is like a placeholder until a variable is passed through an argument and the function 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 print value of a variable to the console and is used as sa debuggin tool
What is a method?
a function that is a property of an object
How is a method different from any other function?
it is a property that comes from that object/array constructor so that any object/array created will have that method used
How do you remove the last element from an array?
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.splice( start_index, how_many_removed, replace_values … )
How do you append an element to an array?
array.push( )
How do you break a string up into an array?
array.split( string that you want to separate the values with )
’’ -> each character
‘ ‘ -> space
‘, ‘ -> comma
Do string methods change the original string? How would you check if you weren’t sure?
no, call the variable to the console
Is the return value of a function or method useful in every situation?
no, sometimes you just need to remove elements in an array for example
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
mdn lol
Give 6 examples of comparison operators.
<= >= === !== < >
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
Is else required in order to use an if statement?
for conditional logic in code
no
Describe the syntax (structure) of an if statement.
if ( conditional ) {code block } else if ( conditional) {code block} else {code block}
What are the three logical operators?
< > =
How do you compare two different expressions in the same condition?
&& ||
What is the purpose of a loop?
to repeat a set of instruction to a different data point
What is the purpose of a condition expression in a loop?
to specify how much to iterate the code block
What does “iteration” mean in the context of loops?
how many times the code block will be run
When does the initialization expression of a for loop get evaluated?
When does the condition expression of a for loop get evaluated?
When does the final expression of a for loop get evaluated?
before an iteration happens once
before an iteration
after an iteration
When does the condition expression of a while loop get evaluated?
before an 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?
How do you iterate through the keys of an object?
adds one to the variable
for (key in object)
How can you retrieve the value of an element’s attribute?
.getAttribute( html class name )
What does the element.matches() method take as an argument and what does it return?
a css selector and returns a boolean
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?
Write a lot more eventlisteners
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?
If conditionals
What is JSON?
Javascript string object notation. it
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
can take something like json and be able to go from human language to machine data language and reverse
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( )
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( )
How to you store data in localStorage?
localStorage.setItem(‘keyValue’ , ‘value’)
How to you retrieve data from localStorage?
localStorage.getItem(‘keyValue’)
returns valiue of the key value pair
What data type can localStorage save in the browser?
JSON
When does the ‘beforeunload’ event fire on the window object?
when you refresh and “unload” data
What is a method?
Describe method definition syntax (structure).
Describe method call syntax (structure).
function that is a property of an object
object = {
method: function () { }
}
object.method(arguments
How can you tell the difference between a method definition and a method call?
method definition is making the method as a property of an object and method call is using dot notation to call on the method
How is a method different from any other function?
it is a property of an object
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction:
Anytime you see a simple interface covering a more complex system
Encapsulation:
Inheritance
Polymorphism
What does API stand for?
What is the purpose of an API?
application programming interface (API)
connects computers or software to each other
What does it mean to say that this is an “implicit parameter”?
implicit parameter, meaning that it is available in a function’s code block even though it was never included in the function’s parameter list or declared with var
What is this in JavaScript?
implicit parameter of function where it is being called from
When is the value of this determined in a function; call time or definition time?
call time
How can you tell what the value of this will be for a particular function or method definition?
cant tell bc –> this is not defined yet
How can you tell what the value of this is for a particular function or method call?
object to left of dot when method is called
and window in function
What kind of inheritance does the JavaScript programming language use?
prototype based inheritnANCE
What is a prototype in JavaScript?
a model in which something is patterened, in terms of javascript a prototype includes inherited properties / methods
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
because all of those are prototypes of string objects array objects numbers objects that is already preset in JavaScript
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
the prototype
What does the new operator do?
creates a new ‘object’ or whatever typeof based on the constructor that comes after it. It includes all prorotype properties and methods
- Creates a blank, plain JavaScript object.
- Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
- Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now
refer to the object created in the first step). - Returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
.prototype
What does the instanceof operator do?
checks to see if an object is part of a prototype chain of a constructor. returns a boolean if it is or isn’t
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setTimeout()
How can you set up a function to be called repeatedly without using a loop?
setInterval( some function to be run, time interval)
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
What do setTimeout() and setInterval() return?
0ms
returns a intervalID that is a numeric, non-zero value which identifies the timer created by the call to setInterval();
this value can be passed to clearInterval() to cancel the interval.
What event is fired when a user places their cursor in a form control?
What event is fired when a user’s cursor leaves a form control?
What event is fired as a user changes the value of a form control?
What event is fired when a user clicks the “submit” button within a ?
focus
blur
input
submit
What does the event.preventDefault() method do?
If put inside function it prevents it from doing what is supposed to happen happen
What does submitting a form without event.preventDefault() do?
reloads the page with the form’s values in the URL?
What property of a form element object contains all of the form’s controls.
.elements
What property of a form control object gets and sets its value?
form.elements.namevalue.value
What is one risk of writing a lot of code without checking to see if it works so far?
will run into errors
What is an advantage of having your console open when writing a JavaScript program?
see where error occurs in line of code