JavaScript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
variable key word let var or const and variable name operator then value
How do you initialize (assign a value to) a variable?
assignment operator value
What characters are allowed in variable names?
must begin with letter, $, _ cannot use (.) or dash. cannot use key words reserved for words. All variables are case sensitive. Bad practice to use same name using different cases.
What does it mean to say that variable names are “case sensitive”?
It has to match; and you use camelcase to make sure that is the case
What is the purpose of a string?
store and manipulate text
What is the purpose of a number?
store numbers
What is the purpose of a boolean?
Booleans are helpful when
determining which part of a
script should run.
What does the = operator mean in JavaScript?
it assigns a value
How do you update the value of a variable?
You don’t need to use the variable keyword you use the variable name.
What is the difference between null and undefined?
Null is a non-existent or invalid object or address.
whereas undefined has been just assigned
Why is it a good habit to include “labels” when you log values to the browser console?
It describes the variable that is being entered and if you don’t put it it can be very confusing when reading your logs.
Give five examples of JavaScript primitives.
null, string, bigint, boolean, undefined, symbol and null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Process of joining together two or more strings to create one new string
What purpose(s) does the + plus operator serve in JavaScript?
to perform addition or string concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left.
What is an expression
Junk of work that JavaScript needs to do
What are objects used for?
THere used to group together a set of variables and functions.
What are object properties?
it is a variable that is now a part of the object
Describe object literal notation.
variable object = { key: value}
How do you remove a property from an object?
you use the key word delete and then use dot notation to identify the property or method you want to remove from the object.
What are the two ways to get or update the value of a property?
use dot notation or square brackets
What are arrays used for?
to store data thats in a list format
Describe array literal notation.
var variable = [ ‘ ‘ ]
How are arrays different from “plain” objects?
objects are collection of data with individual set of data. arrays are lists with set format or list.
What number represents the first index of an array?
0
What is the length property of an array?
amount of items in an array
How do you calculate the last index of an array?
array length -1
What is a function in JavaScript?
Functions allow you to package up code for use later in your program. Block of code written to perform a task repeatably.
Describe the parts of a function definition.
Function definitions are made of: the function keyword an optional name zero or more parameters a code block an optional return statement
Describe the parts of a function call.
Functions are called with () parentheses and passed zero or more arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
Functions are called with () parentheses and passed zero or more arguments.
When a function is called, the parameters in its definition take on the values of the arguments that were passed.
What is the difference between a parameter and an argument?
When a function is called, the parameters in its definition take on the values of the arguments that were passed.
Why are function parameters useful?
A function can take parameters which are just values you supply to the function so that the function can do something utilising those values
What two effects does a return statement have on the behavior of a function?
it exits the functions code block. And can return values
Why do we log things to the console?
To see what the code we are writing is doing.
What is a method?
function stored in a property in an object
How is a method different from any other function?
a method is associated with an object
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 * number
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?
str.split()
Do string methods change the original string? How would you check if you weren’t sure?
do not change original string. could console or look via documentation.
Roughly how many string methods are there according to the MDN Web docs?
20ish
Is the return value of a function or method useful in every situation?
No, it could be just a placeholder for data that you want to collect further down the road
Roughly how many array methods are there according to the MDN Web docs?
50ish
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.
==, !=, ===(strict equal) !===(strict not equal) > < >= <=
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
Evaluates or checks a condition in order to execute a subsequent code block.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
{
if (operand 1) comparison operator (operand 2)
}
What are the three logical operators?
&& || !
logical and, logical or, logical not
How do you compare two different expressions in the same condition?
logical operators
What is the purpose of a loop?
Is to continuously run a bit of code until all conditions are met.
What is the purpose of a condition expression in a loop?
It allows the loop to end if it is false
what does iteration mean
it means that it is being ran through x many times
When does the condition expression of a while loop get evaluated?
It gets ran through first
When does the initialization expression of a for loop get evaluated?
It gets ran through first
When does the condition expression of a for loop get evaluated?
after it goes through the initialization
When does the final expression of a for loop get evaluated?
after the condition has been ran through and the block underneath has been ran through as well
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 increases the count by 1
How do you iterate through the keys of an object?
you use a for in loop
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?
change
What event is fired when a user clicks the “submit” button within a ?
input
What does the event.preventDefault() method do?
IT clears the data from the inputs after the browser reloads
What does submitting a form without event.preventDefault() do?
It continues to reload with the values still in the inputs
What property of a form element object contains all of the form’s controls.
Elements
What property of form a control object gets and sets its value?
type
What is an advantage of having your console open when writing a JavaScript program?
It allows you to see if you are messing up or if the code is acting the way that you want it to.
What is JSON?
Javascript Object Notation it is a way to pass information between networks. String containing javascript syntax
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?
converting objects into bytes and then converting them back into objects allows for things to be sent over the network. Allows you to get data later on.
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?
using JSON Parse
How do you store data in localStorage?
storage.setitem()
How do you retrieve data from localStorage?
storage.getItem()
What data type can localStorage save in the browser?
string
When does the ‘beforeunload’ event fire on the window object?
When the window document and it’s resources are about to be unloaded. The document is still visible at this point however.
What is a method?
It is a function which is a property of an object.
How can you tell the difference between a method definition and a method call?
There is no function code block in the method call
Describe method definition syntax (structure).
function definition
Describe method call syntax (structure).
object.method()
How is a method different from any other function?
It’s not any different. A method is associated with an object while a function is not.
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data(as properties) and behavior (as methods)