JS Flashcards
Escaping characters
Code Output \' single quote \" double quote \\ backslash \n newline \r carriage return \t tab \b word boundary \f form feed
Length of a string
“some text”.length
String values are…
Immutable. Which means that they cannot be altered once created. (this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed)
multi-dimensional array
[[some content],[other content]]
Array values are…
Mutable. myArray[0] = 45;
.push() does…
appends data to an array
.pop() does…
pops and assigns the last element from an array to anything
.shift() does…
removes the first element of an array and returns it
.unshift() is…
like push but to the beginning of an array
structure of the function
function functionName() { console.log(something); }
Variables which are used without the var keyword…
…are automatically created in the global scope.
Variables which are declared within a function, as well as the function parameters…
… have local scope
It is ___ to have both local and global variables with the same name.
possible!
When you have both local and global variables with the same name…
… the local variable takes precedence over the global variable.
what is hoisting and what gets hoisted
adding vars and functions to memory when executing. they get added to the top of the file, so your function can be at the bottom but you can call it at the top
vars get partially hoisted (only var but not its value)
functions get fully hoisted
Variables which are defined outside of a function block have
Global scope. This means, they can be seen everywhere in your JavaScript code.
In JavaScript, scope refers to…
the visibility of variables
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.
It is possible to have both local and global variables with the same name. When you do this,
the local variable takes precedence over the global variable.
You can use a _ statement to send a value back out of a function.
return
A function can include the return statement but it does not have to. In the case that the function doesn’t have a return statement, when you call it,
the function processes the inner code but the returned value is undefined.
=== vs ==
unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.
determine the type of a variable or a value
typeof 3 // number
typeof ‘3’ // string
using logical AND
&&
the same effect: nest if statements
logical OR
||
returns true if either of the operands is true
case statements for multiple if statements
switch (value){ case a: do something; break; case b: do something; break; }
switch statements equivalent to final “else”
default:
do something;
break;
(should be the last case, just like else)
If the break statement is omitted from a switch statement’s case,
the following case statement(s) are executed until a break is encountered.
If you have multiple inputs with the same output, you can represent them in a switch statement like this:
switch(val) { case 1: case 2: case 3: result = "1, 2, or 3"; break; case 4: result = "4 alone"; }
When a return statement is reached,
the execution of the current function stops and control returns to the calling location.
Objects are similar to arrays, except that instead of using indexes to access and modify their data,
you access the data in objects through what are called properties.
In an object, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows:
var anotherObject = { make: "Ford", 5: "five", "model": "focus" };
if your object has any non-string properties
JavaScript will automatically typecast them as strings.
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.
Access object property through a variable
var playerNumber = 1;
myPlayer[playerNumber] // returns value for 1:
add properties to an object
myDog.newProperty = someValue
delete properties from an object
delete myDog.newProperty
If you have tabular data, you can use an object to “lookup” values rather than a switch statement or an if/else chain.
This is most useful when you know that your input data is limited to a certain range.
Testing Objects for Properties
We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.
The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
ourStorage.cabinet[“top drawer”].folder2; // “secrets”
ourStorage.desk.drawer; // “stapler”