JavaScript Flashcards
What is the purpose of a string?
To store texts that doesn’t make sense to Javascript
as a sequence of characters!
Purpose of a number?
to store numbers, not always mathematical values
ex) zip codes, phone numbers = numbers that are actually a type of IDs
Purpose of a boolean?
to make decisions/logic switches
What is the difference between null and undefined?
null = value is left out intentionally or coming later. Can only be created by coders by assigning null as the value of a variable
ex) var intentionallyEmpty = null;
undefined = Javascript can create this whenever! Causing ambiguity to the code/debugging steps
Why is it a good habit to include “labels” when you log values to the browser console?
It describes the value that is being logged creating more organization and clarity
7 examples of javascript PRIMITIVES vs JS data types?
string, boolean, number, bigint(numbers that are big), null, undefined, symbol
vs. all of the above AND objects
What is string concatenation?
to make multiple strings become one string
not mathematical, but still an expression (aka a chunk of work that javascript must perform)
uses string operator +
What is an object literal notation?
a simplified syntax = aka syntactical sugar of the constructor notation which uses a constructor function and “new” operator
method
properties that store functions
OR functions that are properties of an object
What are objects used for?
to group data/variables that are related
What are object properties?
individual place to store data within an object
basically a variable inside of an object
Describe object literal notation
{
property: value,
property: value
};
How do we remove a property from an object?
How do we clear the value of a property from an object?
delete operator with object name and property name
reassign the value using dot or bracket notation = ‘ ‘ (blank string)
How do we read this (using square bracket to assign a property and value of an object)?
vehicle[‘color’] = ‘white’;
string white is being assigned to the vehicle AT string color
What makes arrays a special type of object?
They hold a related set of key/value pairs, but the key for each value is its index number.
What are arrays used for?
to create a list and group like data with more flexibility in length/amount vs. object
What is the length property of an array?
shows how many items are inside the array
array.length
How are arrays different from “plain” objects?
arrays do not need a property/key name
the automatically generated name is the index number!
arrays have an order and length
requires method to add/delete data
vs
objects need property/key name
objects do not have order or length
objects require assignment operator to add
and delete operator to delete data
How do you calculate the last index of an array?
array.length - 1
What is a function in Javascript?
a special kind of object that is “callable”
repeatable named block of code
Parts of the Function Definition
Key word: function
optional name
optional parameter
{}
*likely a return statement
What is the difference between function parameter and argument?
Parameter = part of function definition (aka placeholders for potential value in the future) only exists inside of the function code block! This is what lets us reuse functions with different values!
Argument = part of function call
the value that we pass
What 2 effects does the return statement have?
- return of the function spits out a value = specifies a value to be returned to the function caller
- stops the function from running
we can use it to create Guard Clauses (used when we can’t prevent something from going wrong lol)
Does every function call have a return value?
Yes! Even if there is nothing specified, JavaScript will return UNDEFINED! If the code block has console.log, it will log whatever is specified.
ex) function greet () {
var greeting = ‘Hello’;
}
greet();
we will have undefined show up in our console
Why do we log things to the console?
to see the result and check = for debugging
console.log does not do anything for the users
DELETE console.logs once project is finished
6 examples of comparison operators
> , < , <=, >=, ===, !==
What data type do comparison expressions evaluate to?
boolean: true or false
Purpose of an if statement?
To create logic
3 logical operators
|| , && , !
(! means to flip the boolean value)
Syntax of an if statement
key word: if
(condition)
{}
What is the purpose of a loop?
To run a job multiple times/repeat!
What is the purpose of a condition expression in a loop?
tells the loop when to stop
What does “iteration” mean in the context of loops?
A single time that the loop’s CODE BLOCK runs!
When does the condition expression of a while loop get evaluated?
before the code block/iteration
When does the initialization expression of a for loop get evaluated?
just ONCE BEFORE ANYTHING in the loop
ALWAYS ONCE and never again
When does the condition expression of a for loop get evaluated?
before each iteration = code block
after the final expression
except the first iteration, which will be after the initialization
When does the final expression of a for loop get evaluated?
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?
add and assign
ex) var num = 1;
5 + ++num
// num = 2
// 5 + 2 = 7
5 + num++
// num = 1
// 5 + 1 = 6
// num = 2
num++(adds first THEN reassign) vs. ++num (reassigns incremented value first THEN adds)
How do you iterate through the keys of an object?
for…in loop
How do you store data in localStorage?
setItem() method of the localStorage object
takes ( string of the key name, string of the value of key)
How do you retrieve data from localStorage?
getItem() method of the localStorage object
takes string of the name of the key as the argument
What data type can localStorage save in the browser?
strings
which is why we use JSON
When does the ‘beforeunload’ event fire on the window object?
before the window unloads everything
ex) when the user refreshes/closes tab/browser
What is a method?
It is a function which is a property of an object.
2 kinds:
- Instance Methods (built-in tasks performed by an object instance)
- Static Methods (tasks called directly on an object constructor)
How can you tell the difference between a method definition and a method call?
definition requires a function definition
call would require object . method name ()
Describe method definition syntax (structure)
if creating an object, {
property name: function
if object already created,
object name.propertyname = function;
Describe method call syntax (structure)
object name.method name (arguments if needed);
How is a method different from any other function?
method is function stored inside an OBJECT
requires NAMING THE OBJECT with dot notation in order to call
When being called:
presence of an object vs. function name
What is the defining characteristic of OOP (Object-Oriented Programming)?
objects can contain both data (as properties) and behaviors (as methods)
What are the four “principles” of Object-Oriented Programming?
abstraction (removing complex details for ease of use)
encapsulation (?)
inheritance (prototypal inheritance/delegation)
polymorphism (?)
What is “abstraction”?
removing/hiding complex details (that is not relevant for current use) to work with complex things in simple ways
ex) light switch hides all the complexity of circuits, breakers, etc.
ex) automatic transmission vs manual (aka stick)
ex) javascript: built upon C (which is more intense)
What does API stand for?
Application Programming Interface
What is the purpose of API?
- give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
- a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software.
ex) DOM: I don’t know how the .createElement() method truly creates a new DOM element, but I know how to use it!
What is “this” in JavaScript?
An implicit parameter that allows us to see
the object where “this” is being invoked from