Basic Javascript Flashcards
(38 cards)
arr.unshift(element1[, …[, elementN]])
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
arr.shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
arr.push(element1[, …[, elementN]])
The push() method adds one or more elements to the end of an array and returns the new length of the array.
arr.pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
undefined
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
null
The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values and is treated as falsy for boolean operations.
falsy
A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.
if (false) if (null) if (undefined) if (0) if (-0) if (0n) if (NaN) if ("")
NaN
The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.
parameter
Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.
argument
An argument is a value (primitive or object) passed as input to a function.
differences between parameter & argument
Function parameters are the names listed in the function’s definition.
Function arguments are the real values passed to the function.
Parameters are initialized to the values of the arguments supplied.
let
The let statement declares a block scope local variable, optionally initializing it to a value.
const
Constants are block-scoped, much like variables defined using the let keyword. The value of a constant can’t be changed through reassignment, and it can’t be redeclared.
var
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
Math.random()
The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Math.random() * (max - min) + min;
global scope
In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.
Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.
local scope
Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function.
!==
The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns false where strict equality would return true and vice versa.
Strict inequality will not convert data types like !=
break;
The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
break [label];
label Optional
Identifier associated with the label of the statement. If the statement is not a loop or switch, this is required.
switch () {
case (value):
…
The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.
switch (expression) { case value1: //Statements executed when the //result of expression matches value1 [break;] case value2: //Statements executed when the //result of expression matches value2 [break;] ... case valueN: //Statements executed when the //result of expression matches valueN [break;] [default: //Statements executed when none of //the values match the value of the expression [break;]] }
Object.keys(obj)
The Object.keys() method returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would.
how to access the properties of an object
There are two ways to access the properties of an object: dot notation (.) and bracket notation ([””]), similar to an array.
Dot notation is what you use when you know the name of the property you’re trying to access ahead of time.
updating or adding new object properties is the same method
object.key = "value" object["key"] = "value"
delete properties from an object
delete object.key