JavaScript Flashcards
What is the purpose of variables?
They allow us to store values or data to use for functions and other things
How do you declare a variable?
Var variableName = value, need var
How do you initialize (assign a value to) a variable?
Set the variable = to a value with the assignment operator
What characters are allowed in variable names?
Letters, $, _ and numbers, but they can’t start with a number
What does it mean to say that variable names are “case sensitive”?
Varname is a different variable than varname
What is the purpose of a string?
To store and interact with text content
What is the purpose of a number?
To store and interact with numbers
What is the purpose of a boolean?
To store and interact with boolean values. Often used as a switch or to make decisions
What does the = operator mean in JavaScript?
It means ‘assigned the value of’
How do you update the value of a variable?
You reassign it a new value
What is the difference between null and undefined?
Null is an object with no value, undefined has no value or type.
Undefined comes from javascript engine, it helps js tell us if something isn’t there, null has to be defined by the human.
Why is it a good habit to include “labels” when you log values to the browser console?
To know what you are logging
Give five examples of JavaScript primitives.
String, number, boolean, null, undefined
What data type is returned by an arithmetic operation?
A number
What is string concatenation?
It is when you use the + symbol to add two strings together, called concatenation
What purpose(s) does the + plus operator serve in JavaScript?
It can and numbers and concatenate strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
It adds the value with the variable and assigns that value to the variable
What is an expression
It is a chuck of code in JS.
What are objects used for?
Used to store related data of all different data types (variables and functions)
What are object properties?
They are the individual data types within an object literal
Describe object literal notation.
Object literal notation declares a variable as an object with the assignment operator with the property, value pairs within curly brackets separated by commas.
How do you remove a property from an object?
Use the delete key word and then the property you want to delete in dot notation
What are the two ways to get or update the value of a property?
Dot notation, or bracket notation
What are arrays used for?
To store lists of data, generally better for large sums of data
Describe array literal notation.
Var arrayName = [‘index1’, ‘index2’, ‘index3’]
How are arrays different from “plain” objects?
They have index numbers paired with each value instead of property, arrays have a set order, objects don’t have a set order
What number represents the first index of an array?
0
What is the length property of an array?
The length property of an array is the number of index values in the array
How do you calculate the last index of an array?
You can use the length property to find the length of the array, and then subtract that number by one since the first index is 0.
lastIndex = array.length - 1;
What is a function in JavaScript?
Is a block of code that executes when called. Arguments can be imputed and values can be outputted by the function
Describe the parts of a function definition.
Function keyword, function name with parenthesis (ex. name()) (name is optional), arguments in parenthesis, curly braces that hold the code to be executed, and an optional return key word with the value you want to be returned
Function fxnName(parameter1, parameter2) { code here; return outputValue };
Describe the parts of a function call.
fxnName(argument1, argument2);
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition has the definition block and the function keyword, calling it just requires the name and necessary arguments/parameters
What is the difference between a parameter and an argument?
Parameters are only used as variables to store the inputted arguments, arguments are what are inputted and are already defined data types.
Why are function parameters useful?
They are useful by making it easier for a function to be repeatable and to take values from other data in our program
What two effects does a return statement have on the behavior of a function?
It returns a value from the function and also ends the function’s execution.
Why do we log things to the console?
To check or code to see if functions and methods work properly, arrays and variables are filled and assigned.
What is a method?
A method is a function that is a property of an object.
How is a method different from any other function?
Methods are attached to objects, functions aren’t necessarily properties of objects.
How do you remove the last element from an array?
.pop() method
How do you round a number down to the nearest integer?
.floor() method of the Math object
How do you generate a random number?
.random() method of the Math object
How do you delete an element from an array?
.splice(starting index, number of indexes to delete) method
How do you append an element to an array?
Push or unshift 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?
They don’t, console.log would check it, you can also check documentation
Roughly how many string methods are there according to the MDN Web docs?
Over 20
Is the return value of a function or method useful in every situation?
No, some are just useful for changing an object, the return value is useful in some cases.
Roughly how many array methods are there according to the MDN Web docs?
Over 20
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 true or false
What is the purpose of an if statement?
To help make decisions based on the comparison expression.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
If (operand1 > operand 2) {
Return something;
}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
Separate the expressions with a logical operator
What is the purpose of a loop?
To repeat a junk of code, or a process.
What is the purpose of a condition expression in a loop?
To give an end to a loop.
What does “iteration” mean in the context of loops?
The loop be executed once
When does the condition expression of a while loop get evaluated?
Before the conditional block is executed, and before each iteration after that
When does the initialization expression of a for loop get evaluated?
Before anything, only once.
When does the condition expression of a for loop get evaluated?
Before each iteration
When does the final expression of a for loop get evaluated?
At the end of the code block before the condition expression is evaluated. And for each 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?
It increments and changes the variable by 1. 1++ is equal to 2.
How do you iterate through the keys of an object?
By using a “for in” loop
For (var keyParameter in objectParameter) {}
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 form?
‘submit’
What does the event.preventDefault() method do?
The event interface’s preventDefault() method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be
What does submitting a form without event.preventDefault() do?
It would refresh the page after the form is submitted
What property of a form element object contains all of the form’s controls.
The ‘elements’ property
What property of form a 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?
The more code you write without checking, when you get a bug, you’ll have more code to check to find the bug.
What is an advantage of having your console open when writing a JavaScript program?
One advantage is being able to use console.log to see what values are in real time. Another one is being able to check the contents of functions and variables
What is the event.target?
The targeted element that triggered the event listener
What is the affect of setting an element to display: none?
The document doesn’t display it as if it doesn’t exist. Removes it from the document flow
What does the element.matches() method take as an argument and what does it return?
The matches() method checks to see if the Element would be selected by the provided selectorString – in other words – checks if the element “is” the selector.
How can you retrieve the value of an element’s attribute?
.getAttribute(‘attribute name’)
At what steps of the solution would it be helpful to log things to the console?
As many steps as possible
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?
Have an event listener on each element
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?
You would have to write an if statement for each option.
What is JSON?
The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON. It can’t be called or constructed, and aside from its two method properties, it has no interesting functionality of its own.
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?
It allows for the web application to save data without relying on the host computer.
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
How do you retrieve data from localStorage?
localStorage.getItem
What data type can localStorage save in the browser?
string
When does the ‘beforeunload’ event fire on the window object?
It happens before a screen unloads.
What is a method?
A function that is a property of an object
How can you tell the difference between a method definition and a method call?
Method definition is defined with the object definition with a function key word, and a method call is written as object.method()
Describe method definition syntax (structure).
Object {
propertyName: function (parameters) {
Function definition…
}
}
Describe method call syntax (structure).
object.method()
How is a method different from any other function (trick question)?
It’s not. methods and functions do the same thing.
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, encapsulation, inheritance, polymorphism
What is “abstraction”?
Allows to execute (possibly) complex things in simple ways.
What does API stand for?
application programming interface
What is the purpose of an API?
is to give programmers a way to interact with a system in a simplified, consistent fashion
What is “this” in JavaScript?
this is an implicit parameter of all JavaScript functions
What does it mean to say that this is an “implicit parameter”?
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
When is the value of this determined in a function; call time or definition time?
Call time
What does THIS refer to in the following code snippet?
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
nothing, because this gets a value when it is called
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
‘It’s-a-me, Mario! Is logged to the console.
Because the greet method of the character object logs a string to the console with a value of this.firstName which is the same as character.firstName
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
‘It’s-a-me, undefined! Because hello is not a method of an object, it is just a function. And this.firstName is the same as window.firstName and window does not have a firstName property so it is undefined
How can you tell what the value of this will be for a particular function or method definition?
You can’t
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot when the function is called (as a method).
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance.
What is a prototype in JavaScript?
A new object produced by cloning another object
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
prototype-based (or prototypal) inheritance allows for javascript objects to give certain behaviors (methods) or data (properties) to other objects.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
In the prototype Object property