Javascript Flashcards
<p>+</p>
<p>Returns the sum or 2 numbers or concatenates 2 strings</p>
<p>%</p>
<p>Modulo Returns the remainder of the first value divided by the second</p>
Javascript Primitive Data Types(5)
String, Number, Boolean, Null, Undefined
<p>2 + "2"</p>
<p>Concatenation = 22</p>
10 > 2
returns true
typeof()
tells us what the type is typeOf(10<2) returns boolean
Javascript Null
A value does not exist
Javascript Undefined
The variable has yet to be defined
Javascript prompt
Creates and alert box that accepts an input; var x = prompt(“what is x”);
Random number between 0 and 5
var _randNum = Math.round(Math.random() * 5);
Check if number is divisible by 3
if (x%3 === 0)
Property
Values of an object Dog.breed breed is property; noun
Method
A function that is associated with an object; verb
object literal
Define and create an instance at the same time var person = {Name: “Bill”, Age: 5}
object constructor
function Person (name, age) {this.name = name; this.age = age;}
Host objects
Objects defined by the environment; console is a common host object used in a browser
Core objects
Objects defined by Javascript examples: Math, String, Boolean, Array
global object
The window is the global object when the host environment is the web; window.alert() === alert()
Local Scope
Variables created inside a function can’t be accessed from outside the function. (Variables created within a function that do not use the var keyword are added to global scope)
Function scope
Every time we create a new function the scope changes
Closure dictionary definition
A combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)
Closure laymans definition
A closure gives you access to an outer functions scope from an inner function. In JS closures are created every time a function is created at function creation time.
Why are closures used
To give objects data privacy; variables can be manipulated in a different scope
Closure benfites
Keep dataprivate;
var output = (function(x) { delete x; return x; })(0);
console.log(output);
Above code will output 0 as output. delete operator is used to delete a property from an object. Here x is not an object it’s local variable. delete operator doesn’t affect local variable.
What is “closure” in javascript? Provide an example?
A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in parent function scope.
difference between let and var
var is scoped to the nearest function block; let is scoped to the nearest enclosing block
If i already have an array and i want to do the exact same operation on each of the elements in the array and return the same amount of items in the array
Map
If i already have an array but i only want to have items in the array that match certain criteria
Filter
If i already have an array, but i want to use the values in that array to create something completely new
Reduce