Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Types
- Number
- String
- Boolean
- Symbol (new in ES2015)
- Object
- Function
- Array
- Date
- RegExp
- null
- undefined
Number
Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec. This has some interesting consequences. There's no such thing as an integer in JavaScript
You can convert a string to an integer using the built-in parseInt() function. This takes the base for the conversion as an optional second argument, which you should always provide
parseInt('010'); // 8
parseInt('0x10'); // 16
You can also use the unary + operator to convert values to numbers:
+ '42'; // 42
+ '010'; // 10
+ '0x10'; // 16
A special value called NaN (short for "Not a Number") is returned if the string is non-numeric:
parseInt('hello', 10); // NaN
You can test for NaN using the built-in isNaN() function:
isNaN(NaN); // true
JavaScript also has the special values Infinity and -Infinity:
1 / 0; // Infinity
-1 / 0; // -Infinity
You can test for Infinity, -Infinity and NaN values using the built-in isFinite() function:
isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false
String
Strings in JavaScript are sequences of Unicode characters.
To find the length of a string (in code units), access its length property:
'hello'.length; // 5
Other types
- null - which is a value that indicates a deliberate non-value and is only accessible through the null keyword
- undefined - which is a value of type undefined that indicates an uninitialized value, a value hasn't even been assigned yet
In JavaScript it is possible to declare a variable without assigning a value to it. If you do this, the variable's type is undefined. undefined is actually a constant.
Any value can be converted to a boolean according to the following rules:
- false, 0, empty strings (""), NaN, null, and undefined all become false.
- All other values become true.
Objects
JavaScript objects can be thought of as simple collections of name-value pairs. As such, they are similar to hash tables in C and C++.
// There are two basic ways to create an empty object:
var obj = new Object();
// Object literal syntax
var obj = {};
var obj = {
name: 'Carrot',
for: 'Max', // 'for' is a reserved word, use '_for' instead.
details: {
color: 'orange',
size: 12
}
};
Functions
If no return statement is used (or an empty return with no value), JavaScript returns undefined.
Functions have access to an additional variable inside their body called arguments, which is an array-like object holding all of the values passed to the function. Let's re-write the add function to take as many values as we want:
function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum;
}
add(2, 3, 4, 5); // 14
this Keyword
First, know that all functions in JavaScript have properties, just as objects have properties. And when a function executes, it gets the this property—a variable with the value of the object that invokes the function where this is used.
when we use this in a global function, it refers to (and has the value of) the global window object (not in strict mode though, as noted earlier) that is the main container of the entire JavaScript application or web page.
Defining classes
Classes are in fact "specialfunctions", and just as you can definefunction expressionsandfunction declarations, the class syntax has two components:class expressionsandclass declarations.
Optimization
Server side rendering is one of the most important optimizations you can make to speed up a Javascript application
Spread syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction.apply(null, args);
// With spread syntax the above can be written as:
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);