---
title: Variables
chapter: Variables
---
Variables are declared with the `var` keyword. JavaScript is
_dynamically typed_ so every variable can hold a value of any data type.
Variables can be declared _without an initial value_.
Some example declarations:
```javascript
var foo;
var bar = 42;
var foo, bar, baz;
var foo = 42,
bar = 'baz',
z;
```
Variables that don't explicitly get assigned an initial value have the value
`undefined`.
**ES2015**
Since ES2015, `let` and `const` can be used in addition to `var`. We will learn
how they differ from `var` later. For now, lets have a look how `const` differs
from `var` or `let`: `const` can be assigned a value only _once_ (*const*ant).
Reassigning a value will either throw an error (in strict mode, see below) or
is silently ignored:
```js
const foo = 42;
foo = 21; // error or ignored
```
`const`s _must_ be initialized with a value:
```js
const foo; // error
foo = 42;
```
---
## Variable names
Valid characters for variable names include [a wide range of _unicode
characters_](http://mathiasbynens.be/notes/javascript-identifiers).
However, the name _must_ start with a letter, `_` or `$`. Not doing so will
result in a syntax error.
Examples:
```javascript
var π = 3.141;
var _foo = π;
var 0_bar = '...'; // Syntax error
```
---
## Variable access
Trying to _read_ an _undeclared variable_ results in a runtime error:
```javascript
var foo;
console.log(bar); // ReferenceError: bar is not defined.
```
However, _writing_ to an undeclared variable is valid by default. It will
create an _implicit global variable_ and should thus be avoided:
```javascript
function foo() {
bar = 42;
}
foo();
console.log(bar); // no error
```
If code runs in _[strict mode][]_, assigning to an undeclared variable throws
an _error_.
### Strict mode
[Strict mode][] is a mode of evaluating JavaScript that enforces stricter
rules. It was introduced to "deprecate" certain patterns/behaviors that are
considered bad or confusing.
Strict mode can be enabled for a JavaScript or a function by putting
```js
'use strict';
```
at the beginning of it.
[strict mode]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode
---
title: Scope
chapter: Scope
---
Unlike other programming languages, JavaScript only has **function scope**, not
block scope. In the following example, all variables are visible throughout the
function:
```javascript
function foo() {
var bar = 42;
// loop
for (var i = 0; i < 10; i++) {
var j = i;
}
console.log(bar); // 42
console.log(i); // 10
console.log(j); // 9
}
```
In other languages, like Java, the variables `i` or `j` would not be available
where the above code tries to access them.
**ES2015**
The big difference between `let`, `const`, and `var` is that `let` and `const`
are _[block scoped][block scope]_. If we would use `let` instead of `var` in
the above example, we would get the following result:
```javascript
function foo() {
let bar = 42; // or var or const, doesn't matter
// loop
for (let i = 0; i < 10; i++) {
// i is scoped to the loop body block
let j = i; // j is scoped to the loop body block
}
console.log(bar); // 42
console.log(i); // ReferenceError
console.log(j); // ReferneceError
}
```
---
toc: What is this?
chapter: this
style: |
h1 {
text-align: center
}
---
# What is `this`?
[**`this`**][mdn] is a special "variable" which implicitly exists in every
function. It can be thought of being similar to Java's `this` and Python's
`self`, but it's much more flexible than that.
**Important**: The value of `this` is determined when the
function is **called**, not when the function is
_defined_.
Given the following function:
```javascript
function foo() {
console.log(this);
}
```
these would be the values of `this` if called in those specific ways:
```javascript
// "normal call": global object / window in browsers
// undefined in strict mode
foo();
// as object "method": to the object
var obj = { method: foo };
obj.method();
// via .call / .apply: To the value passed as first argument
foo.call(bar);
```
[mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
---
title: JavaScript Basics
toc: Title
chapter: Intro
layout: Center
style: |
.exerslide-slide .Center-wrapper {
text-align: left;
}
.exerslide-slide h1 {
color: #444;
font-size: 400%;
margin-bottom: 50px;
text-align: center;
}
scale:
content_width: 39
column_width: 0.6
---
A quick introduction to basic and important concepts of JavaScript
Use the arrow keys (**←, →**) to navigate through the page.
You should use Google Chrome and make yourself
familiar with the [developer tools][], especially with the console.
You will need it for the exercises.
---
## Contribute
Found a problem with style or content? Or do you have suggestions for improving
the content? Please [file an issue on GitHub][jsbasics-issues].
---
title: Booleans, numbers and strings
---
The **Boolean** data type has two values, `true` and `false`.
```js
var foo = true;
var bar = false;
```
---
**Numbers** are [double precision floating point][float] numbers, following the
[IEEE 754 standard][ieee754]
This makes it very easy to work with them, since you don't have to
differentiate between integer values and floating point values.
There are various ways that numeric value can be expressed:
```javascript
var x = 5; // "integer"
var y = -4.2; // "float"
var z = 5e3; // = 5 * 10^3
```
An issue with floating point numbers is the loss of precision, which of course
occurs in JavaScript as well:
```javascript
0.1 + 0.2; // 0.30000000000000004
```
The maximum integer value that can be used without loss of precision is
253.
```javascript
Math.pow(2, 53); // 9007199254740992
Math.pow(2, 53) + 1; // 9007199254740992
```
---
**Strings** are sequences of unicode characters and can either be delimited with
a **single** or **double** quotation mark. Unlike in other languages, such as PHP,
both are interpreted in the exact same way.
Example:
```javascript
var foo = 'bar';
var bar = 'baz';
```
[float]: http://en.wikipedia.org/wiki/Double-precision_floating-point_format
[ieee754]: http://en.wikipedia.org/wiki/IEEE_floating_point
---
title: Demo
layout_data:
description: Run the code and have a look at the output. Is it what you expect?
Try some other values / operators and look at the output (remember to open
the console).
---
var a = 10;
var b = 5;
var c = "1";
log(a + b);
log(a + c);
log(b + Number(c));
---
title: Constructor functions
chapter: Constructors/Classes
---
JavaScript doesn't have classes like class-based OOP languages have, but it has
something similar: _constructor functions_.
Constructor functions are functions which _construct objects_. Technically
_every_ function can be used as a constructor function, it just has to be
called with the [`new` operator][new]:
```javascript
function Person(name) {
this.name = name;
}
var felix = new Person('Felix');
console.log(felix.name);
```
Inside the constructor function, `this` refers to a new, empty object. The
result of the whole `new` expression (`new Person(...)`) is that object. You can
think about it as if the function would implicitly `return this;`.
Calling a function with `new` has another effect: The prototype of the new object
is the object referred to by the function's `prototype` property.
Example:
```javascript
function Person(name) {
this.name = name;
}
Person.prototype.sayName = function () {
return this.name;
};
var felix = new Person('Felix');
console.log(felix.sayName());
```
Given the example above, use `console.dir(felix);` to get a better understanding
of the structure of the object (including it's prototype chain).
[new]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
---
title: Exercise
layout_data:
description: |
Create a local variable with name `foo` and value `42`.
Use `log(foo)` to log the value of `foo`.
Remember to open your browser's *developer tools* to view the log output.
assertion: |
assert(
/var foo\s*=.+;?$/m.test(source),
"It doesn't look like you have declared a variable (hint: var)."
);
assert(output[0] === 42, "Don't forget to log the value");
---
// Create variable
//
log(foo);
---
title: Hoisting
---
This behavior becomes more understandable after we introduce **hoisting**.
Before a JavaScript function is even executed, the engine finds all variable and
function declarations and creates a binding for them in the functions scope.
Thus, the example of the previous slide is equivalent to the following:
```javascript
function foo() {
var bar, i, j;
bar = 42;
// loop
for (i = 0; i < 10; i++) {
j = i;
}
console.log(bar); // 42
console.log(i); // 10
console.log(j); // 9
}
```
Note how all variable declarations are put at the top of the function. The
_value_ will still be assigned only when the execution reaches the line of the
assignment expression.
One of the practical implications of the hoisting concept is _mutually recursive
functions_. Consider the following example:
```javascript
function isEven(n) {
if (n == 0) {
return true;
}
return isOdd(n - 1);
}
// Normally may call `isEven` even though it's mutually
// dependent on the `isOdd` which is defined below.
console.log(isEven(2)); // true
function isOdd(n) {
if (n == 0) {
return false;
}
return isEven(n - 1);
}
```
To get more details on hoisting see [this article][hoisting].
[hoisting]: http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
---
toc: this - an example
style: |
h1 {
text-align: center
}
---
# `this` - an example
```javascript
function say() {
console.log('My name is ' + this.name);
}
var felix = {
name: 'Felix',
sayName: say
};
var sarah = {
name: 'Sarah',
sayName: say
};
felix.sayName(); // My name is Felix
sarah.sayName(); // My name is Sarah
say.call({ name: 'Anonymous' }); // My name is Anonymous
say(); // My names is undefined
```
In this example we define a single function that uses `this` internally. The
function is then assigned to different objects as property. The output the
function produces depends on which object the function is called (_how_ it is
called).
---
title: What this presentation is not about
layout: Center
style: |
.exerslide-slide .Center-wrapper {
text-align: left;
}
.exerslide-slide h1 {
text-align: center;
}
---
JavaScript is predominantly run in browsers to make websites interactive. In
order to do that, the browser provides access to other technologies, such as
[CSS][] and the [DOM][].
This presentation is exclusively about JavaScript, the _language_, not the
environment in which the JavaScript code runs!
---
title: Exercise
layout_data:
description: Create three variables `x`, `y`, `z`, where `x` contains a string value,
`y` a number and `z` a boolean value. Log the values with `log`.
assertion: |
assert(
output.some(function(x) { return typeof x === 'string' }),
'Your log must contain a string.'
);
assert(
output.some(function(x) { return typeof x === 'number' }),
'Your log must contain a number.'
);
assert(
output.some(function(x) { return typeof x === 'boolean' }),
'Your log must contain a boolean.'
);
---
// Hint: You can pass multiple arguments to log:
// log(a, b)
---
toc: "ES6's class declarations"
---
# Syntactic sugar: ES6's `class` declarations
Setting up more complex constructor functions with their prototypes can be
cumbersome. For that reason ES2015 [introduced a new syntax][classes].
With the syntax, the example from the previous slide would look like
```javascript
class Person {
constructor(name) {
this.name = name;
}
sayName() {
return name;
}
}
var felix = new Person('Felix');
```
I want to be very clear that this is mostly just _syntactic
sugar_. While there are some differences between `class` declarations
and constructor functions + prototype, the underlying paradigm (prototypes)
does not change.
[classes]: http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
---
title: Comparison operators
---
As already established at the beginning, JavaScript is _dynamically typed_. It
also performs _type conversion_, if a specific data type is expected and not
provided. For example in `a * b`, the values of `a` and `b` will be converted
to numbers first.
Even though there are [well defined rules][conversion rules] for converting one
data type into another, these rules can be quite surprising.
[conversion rules]: http://www.ecma-international.org/ecma-262/5.1/#sec-9
What does this have to do with comparison? JavaScript has two kind of comparison
operators:
- **Loose comparison** (`a == b`, `a != b`)
- **Strict comparison** (`a === b`, `a !== b`)
The difference is that _loose_ comparison will _convert both values_ to the same
data type if they are of different data types. _Strict_ comparison immediately
returns `false` if both values _don't have the same type_.
Examples:
```javascript
'42' == 42; // true
(('42' ===
(42)[ // false
// Objects are compared by reference
(1, 2)
]) ==
[1, 2][(1, 2)]) === // false
[1, 2]; // false
```
The following tool visualizes the steps of the _[abstract equality
comparison](http://www.ecma-international.org/ecma-262/7.0/#sec-abstract-equality-comparison)_
algorithm, which is used for loose comparison.
You can select some predefined examples and see which steps are performed
during the comparison. The results will probably surprise you. You can also
provide your own values.
```react comparison
[
["[1,2]", "'1,2'"],
["[0]", "false"],
["'\\n'", "false"],
["'0XA19'", "2585"]
]
```
Also have a look at [this table][comparison table] to get a quick overview of
the differences between `==` and `===`.
The above examples hopefully showed you that loose comparison isn't that
"simple" and it's not always clear what ends up being compared in the end. For
that reason you should follow this advice:
**You should _always_ use strict comparison**, unless you
explicitly want to make use of the type conversion (i.e. you know what you are
doing).
If you write an API, make it clear which data type it expects (e.g. through
comments).
[comparison table]: https://dorey.github.io/JavaScript-Equality-Table/
---
title: ES5, ES2015 and beyond
layout: Center
style: |
.exerslide-slide .Center-wrapper {
text-align: left;
}
.exerslide-slide h1 {
text-align: center;
}
---
This tutorial primarily focuses on JavaScript following the [ECMAScript 5][es5]
(ES5) specification. [ES2015][] (also known as ES6) was released in 2015 and
brings many new features to the language, including new syntax constructs.
While this tutorial won't go into detail about new features in ES2015 and newer
versions, it will point differences to/alternatives for certain ES5 features if
they exist.
---
title: Demo
layout_data:
description: Which value does this code log and why? Think about the answer *before* you
run the code.
---
var bar = 42;
function foo() {
log(bar);
var bar = 21;
log(bar);
}
foo();
---
title: Exercise
layout_data:
description: Which value does the code log and why? How can it be changed to log the value
of `obj.foo`?
assertion: |
assert(output[0] === 42, 'The code should only log 42');
assert(/log\s*\(\s*this\.foo\s*\)/.test(source), "Looks like you tried to be clever. Don't modify log(this.foo).");
---
var obj = {
foo: 42,
bar: function () {
log(this.foo);
}
};
var foo = obj.bar;
foo();
---
title: Closures
---
[Wikipedia describes closures as][closures]:
> In programming languages, a closure (also lexical closure or function closure)
> is a function or reference to a function together with a referencing environment
> — a table storing a reference to each of the non-local variables (also called
> free variables or upvalues) of that function.
According to this definition, **every** function in JavaScript is a
closure, because [every function has an (internal) reference to the
environment][functions] it was created in. The simplest example is:
```javascript
var foo = 42;
function bar() {
console.log(foo);
}
```
Here the function `bar` has access to `foo`, which is defined outside of it.
**Important**: The value of a free variable is determined when the
function is **executed**, not when the function is
_defined_. Read the previous sentence three times to make sure you really
understand what it implies.
[closures]: http://en.wikipedia.org/wiki/Closure_%28computer_programming%29
[functions]: http://www.ecma-international.org/ecma-262/5.1/#sec-13
---
title: Exercise
layout_data:
description: Run the code and have a look at the output. Is it what you expect?
What are reasons for this output? Modify the code, so that it prints
the result you would expect.
assertion: |
var values = [" ", "0", 0];
var c = 1;
for (var i = 0, l = values.length; i < l; i++) {
for (var j = i; j < l; j++) {
var expectedResult = values[i] === values[j];
assert(
expectedResult === output[c],
'Comparing ' + JSON.stringify(values[i]) + ' and ' +
JSON.stringify(values[j]) + ' should yield ' + expectedResult.toString() +
' not ' + output[c]
);
c += 2;
}
}
---
var values = [" ", "0", 0];
for (var i = 0, l = values.length; i < l; i++) {
for (var j = i; j < l; j++) {
log(
JSON.stringify(values[i]) + ' == ' + JSON.stringify(values[j]) + ': ',
values[i] == values[j]
);
}
}
---
title: null and undefined
---
JavaScript has two data types to express the **absence of a value**, null and
undefined.
**null** has the only value `null` and **undefined** has the only value
`undefined`.
The difference between those two is subtle and is best explained by how to use
them:
- `undefined` is the value JavaScript itself uses to indicate the absence of a
value.
- `null` is the value the engineer should use to indicate the absence of a
value.
Examples:
```javascript
var foo; // no value is assigned, foo has the value undefined
var bar = null; // bar is explicitly set to null
console.log(foo); // logs "undefined"
console.log(bar); // logs "null"
```
There are other native occurrence of `undefined` which we will mention later.
**Remember:** A variable that doesn't exist cannot be accessed at all (it will
throw a reference error). Instead of saying "the variable is undefined" we
rather say "the variable is not _declared_" to avoid confusion.
```js
var foo;
console.log(foo); // logs `undefined`
console.log(bar); // reference error
```
---
title: Demo
layout_data:
description: Which value does this code log and why? Think about the solution *before* you
run the code.
---
var foo = 42;
function bar() {
log(foo);
}
foo = 21;
bar();
---
title: Exercise
layout_data:
description: Which values does this code log and why? Modify the code so that it logs the
values as "expected".
assertion: assert(
output.every(function(x, i) {
return x === i;
}),
'The code should output the loop variable of each iteration, i.e. 0, 1, 2'
);
---
var functionArray = [];
for (var i = 0; i < 3; i++) {
functionArray[i] = function() {
log(i);
};
}
for (var j = 0; j < 3; j++) {
functionArray[j]();
}
---
title: Exercise
layout_data:
description: Log the values `null` and `undefined`.
assertion: |
assert(
output.some(function(x) { return x === null }),
'Your log must contain null.'
);
assert(
output.some(function(x) { return x === void 0; }),
'Your log must contain undefined.'
);
assert(
source.indexOf('undefined') === -1,
'Try to log undefined without writing it in the source code.'
);
---
---
title: Property access
---
Properties of objects can be accessed in two ways:
- **Dot notation** (`obj.prop`)
- **Bracket notation** (`obj["prop"]`)
You should always prefer dot notation, unless you _have_ to use bracket notation.
This could be if the property name is not a valid identifier or if it comes from
a variable. You can use any _expression_ inside the brackets.
Examples:
```javascript
obj['test-field']; // test-field is not a valid identifier
var field = 'test';
obj[field];
obj['example' + i];
```
Because you can only use dot notation if the property name is a valid identifier
name, array objects can only be accessed via bracket notation, `arr[0]`, not dot
notation, `a.0`.
---
You can **assign to properties** by putting the member expression on the left
hand side of an assignment expression:
```javascript
obj.prop = value;
```
---
If you have nested objects/arrays, you simply use a valid property accessor
repeatedly:
```javascript
var obj = { foo: { bar: [42, 21] } };
console.log(obj.foo.bar[0]); // which is evaluated as ((obj.foo).bar)[0]
```
---
Accessing a non existing property does not throw an error, it returns
`undefined`:
```javascript
var obj = {};
console.log(obj.foo);
```
---
title: Objects
---
Everything else besides primitive data type values is an _object_.
Objects are _key-value_ stores, more specifically _stringkey-value_ stores. The
"keys" of an object are called _properties_.
The syntax to create a plain object is `{key: value, ...}`, which is called an
object literal. For example:
```javascript
var obj = {
foo: 'bar',
baz: 42
};
```
Note that the above example doesn't use _quotation marks_ around the property
names. In an object literal, quotation marks can be be omitted if the property
name would also be a _valid variable name_. If not, they need to be quoted.
_Number literals_ are valid an object literal as well.
Here are some more examples of valid and invalid property names in object
literals:
```js
var obj = {
foo: 0, // valid, could be variable name
'bar': 0, // string literals are always valid
123: 0, // number literals are always valid
1.5: 0, // ^
foo-bar: 0, // invalid, would not be a valid variable name
'foo-bar': 0, // string literals are alwaus valid
};
```
**Important:** No matter which value or syntax you use for a property name, the
value will always be converted to a **string**.
**ES2015**
ES2015 adds two extensions to object values and object literals:
- _Symbols_ are can be used as property names. They are not converted to
strings.
- Object literals can contain _[computed property names][computed properties]_:
```js
var foo = 42;
var obj = {
[foo]: 0
};
// creates {42: 0}
```
## References
Just like in Java and other object-oriented programming languages, objects are
represented as _references_. That means if a variable has an object as a value,
it really has a reference to that object.
```js
var user = {name: 'Tom'}:
```
:::ascii
```
┌──────────────┐
┌─────┬──────────┐ │ Object#123 │
│user │ ref:123 ◆┼──────▶├──────┬───────┤
└─────┴──────────┘ │ name │ "Tom" │
└──────┴───────┘
```
:::
Assigning the value to another variable makes both variables point to the same
object:
```js
var owner = user;
```
:::ascii
```
┌─────┬──────────┐ ┌──────────────┐
│user │ ref:123 ◆┼──┐ │ Object#123 │
├─────┼──────────┤ ├───▶├──────┬───────┤
│owner│ ref:123 ◆┼──┘ │ name │ "Tom" │
└─────┴──────────┘ └──────┴───────┘
```
:::
Assigning to `user.name` will therefore also "change" `owner.name`:
```js
user.name = 'Joe';
console.log(user.name, owner.name);
// Joe, Joe
```
:::ascii
```
┌─────┬──────────┐ ┌──────────────┐
│user │ ref:123 ◆┼──┐ │ Object#123 │
├─────┼──────────┤ ├───▶├──────┬───────┤
│owner│ ref:123 ◆┼──┘ │ name │ "Joe" │
└─────┴──────────┘ └──────┴───────┘
```
:::
But assigning a new value to either `user` or `owner` will result in only that
variable referring to the new value. The other variable will still refer to the
same value.
```js
owner = { name: 'Kim' };
```
:::ascii
```
┌──────────────┐
│ Object#123 │
┌───▶├──────┬───────┤
┌─────┬──────────┐ │ │ name │ "Joe" │
│user │ ref:123 ◆┼──┘ └──────┴───────┘
├─────┼──────────┤
│owner│ ref:456 ◆┼──┐ ┌──────────────┐
└─────┴──────────┘ │ │ Object#456 │
└───▶├──────┬───────┤
│ name │ "Kim" │
└──────┴───────┘
```
:::
---
The JavaScript standard defines a couple of [built-in objects][] with additional
properties and special internal behavior, must notably _arrays_ and
_functions_, which are explained in the next slides.
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
---
title: Exercise
layout_data:
description: Log the two properties of the object using dot and bracket notation.
assertion: assert(
source.indexOf('obj.foo') > -1,
'Access property foo with dot notation'
);
assert(
/obj\[(42|'42'|"42")\]/.test(source),
'How exactly did you want to access property 42?'
);
---
var obj = {foo: 'bar', 42: 'answer'};
---
title: Prototypes (1)
---
You may have heard that JavaScript is a _"[prototype-based
language][prototype]"_, unlike other languages, such as Java, which are
_"[class-based languages][class]"_.
## What exactly is a prototype?
In short: A prototype is just another object. If an object `A` has this special
connection to object `B`, then we say that "`B` is the prototype of `A`".
In addition to having "external" properties that can be accessed from code,
objects also have _internal/private_ properties/state. These cannot be accessed
from code and their concrete implementation depends on the JavaScript engine.
Every object has an _internal_ property `[[Prototype]]` (internal properties
are usually denoted with `[[...]]` around the name). This property points to
another object.
:::ascii
```
┌───────────────────────┐ ┌────────────────────────┐
│ A │ │ B │
├───────────────┬───────┤ ├───────────────┬────────┤
│ name │ "Tom" │ ┌──▶│ toString │ │
├───────────────┼───────┤ │ ├───────────────┼────────┤
│ [[Prototype]] │ ◆───┼───┘ │ [[Prototype]] │ null │
└───────────────┴───────┘ └───────────────┴────────┘
```
:::
Multiple objects can have the same prototype.
:::ascii
```
┌───────────────────────┐ ┌────────────────────────┐
│ A │ │ B │
├───────────────┬───────┤ ├───────────────┬────────┤
│ name │ "Tom" │ ┌──▶│ toString │ │
├───────────────┼───────┤ │ ├───────────────┼────────┤
│ [[Prototype]] │ ◆───┼───┘ │ [[Prototype]] │ null │
└───────────────┴───────┘ └───────────────┴────────┘
▲
┌───────────────────────┐ │
│ C │ │
├───────────────┬───────┤ │
│ time │ "day" │ │
├───────────────┼───────┤ │
│ [[Prototype]] │ ◆───┼────────────────────┘
└───────────────┴───────┘
```
:::
Since a prototype is an object, it might itself have a prototype, which may have
a prototype, and so forth.
:::ascii
```
┌───────────────────┐
│ A │
├───────────────┬───┤
│ [[Prototype]] │ ◆─┼────┐
└───────────────┴───┘ │
▼
┌───────────────────┐
│ B │
├───────────────┬───┤
│ [[Prototype]] │ ◆─┼────┐
└───────────────┴───┘ │
▼
┌───────────────────┐
│ C │
├───────────────┬───┤
│ [[Prototype]] │ ◆─│─ ─ ─ ▷
└───────────────┴───┘
```
:::
This is called the _prototype chain_. Almost all objects have the same object
at the end of the prototype chain, which doesn't have a prototype itself.
An object created using literal notation will have the object
`Object.prototype` as its prototype. You can verify this using the following
commands:
```js
var testObject = {};
Object.getPrototypeOf(testObject) === Object.prototype; // true
```
:::ascii
```
┌────────────────────────┐
┌───────────────────┐ │ Object.prototype │
│ testObject │ ├───────────────┬────────┤
├───────────────┬───┤ │ toString │ │
│ [[Prototype]] │ ◆─┼──────▶├───────────────┼────────┤
└───────────────┴───┘ │ hasOwnProperty│ │
├───────────────┼────────┤
│ [[Prototype]] │ null │
└───────────────┴────────┘
```
:::
In order to create an object with an object other than `Object.prototype` as
prototype, one can use [`Object.create`][object.create]:
```js
var a = {};
var b = Object.create(a);
Object.getPrototypeOf(b) === a; // true
```
[prototype]: https://en.wikipedia.org/wiki/Prototype-based_programming
[class]: https://en.wikipedia.org/wiki/Class-based_programming
[object.create]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
---
title: Prototypes (2)
---
Now we know what prototypes are, but not what they _do_ or which problem they
solve.
## What does a prototype do?
Prototypes come in play when we are accessing the property of an object.
Whenver an object property is accessed, the object and its prototype chain are
traversed until the property is found. If the end of the prototype chain is
reached without finding the property, `undefined` is returned.
Consider the following structure:
:::ascii
```
┌───────────────────────┐
│ a │
├───────────────┬───────┤
│ name │ "Tom" │
├───────────────┼───────┤
│ [[Prototype]] │ ◆───┼───┐
└───────────────┴───────┘ │
▼
┌───────────────────────┐
│ b │
├───────────────┬───────┤
│ name │ "Joe" │
├───────────────┼───────┤
│ age │ 42 │
├───────────────┼───────┤
│ [[Prototype]] │ ◆───┼───┐
└───────────────┴───────┘ │
▼
┌───────────────────────┐
│ c │
├───────────────┬───────┤
│ height │ 180 │
├───────────────┼───────┤
│ [[Prototype]] │ null │
└───────────────┴───────┘
```
:::
These are the results for accessing different properties on A:
```js
a.name; // Tom `a` itself has this property, it shadows `b.name`
a.age; // 42 `a`'s prototype has this property
a.height; // 180 `a`'s prototype's prototype has this property
a.eyeColor; // undefined this property doesn't exist
```
---
This is also the reason why we can access `.toString()` on almost every object:
It is defined in `Object.prototype`, which sits at the end of every prototype
chain.
```js
var user = { name: 'Tom' };
user.toString();
// "[object Object]"
```
**Note**: _Assignments_ to properties will (almost) always
create or update a property on object itself, even if a property with the same
name already exists in the prototype chain. The property in the prototype
chain is then _shadowed_, similar to variable shadowing in scopes.
---
title: 'Built-in objects: Arrays and functions'
---
## Arrays
Arrays are objects, which treat properties with numeric keys (i.e. `0`,
`1`, `2`, ...) in a special way. For all purposes, they behave like arrays in
other languages.
JavaScript has a special syntax for creating arrays, `[value, value, ...]`:
```javascript
var arr = [1, 2];
```
If you run `console.dir([1, 2])` in your browser's console, you can inspect the
structure of the array object in more detail.
Unlike "plain" objects, array objects have `Array.prototype` as prototype,
which provides all the array methods, such as `.push`, `.map`, etc.
:::ascii
```
┌──────────┐ [[Prototype]] ┌──────────────────┐
│ obj {} │───────────────────────────▶│ Object.prototype │
└──────────┘ └──────────────────┘
▲
│
[[Prototype]]
│
┌──────────┐ [[Prototype]] ┌─────────────────┐ │
│ arr [] │───────────────▶│ Array.prototype │───┘
└──────────┘ └─────────────────┘
```
:::
---
## Functions
Functions are the only kind of objects that are _callable_, and JavaScript
also has a special syntax for defining them:
```javascript
function foo() {
console.log("I'm a function");
}
```
There are other ways to create functions, which will be explained later.
The most important implication of functions being objects is that "work" just
like any other value. Functions can be _passed to_ functions and _returned
from_ functions, allowing to creation of **higher-order functions**.
Similar to arrays, function objects also have a dedicated prototype,
`Function.prototype`:
:::ascii
```
┌──────────┐ [[Prototype]] ┌──────────────────┐
│ obj {} │───────────────────────────────▶│ Object.prototype │
└──────────┘ └──────────────────┘
▲ ▲
│ │
[[Prototype]] │
┌──────────┐ [[Prototype]] ┌─────────────────┐ │ │
│ arr [] │───────────────▶│ Array.prototype │─┘ │
└──────────┘ └─────────────────┘ [[Prototype]]
│
│
┌──────────────────┐ [[Prototype]] ┌──────────────────┐ │
│func function(){} │───────────────▶│Function.prototype│──┘
└──────────────────┘ └──────────────────┘
```
:::
---
title: Control structures
---
JavaScript provides the same control structures known from other C-like
languages:
- `if (...) { ... } else if (...) { ... } else { ... }`
- `while (...) { ... }` and `do { ... } while (...)`
- `for (...; ...; ...) { ... }`
- `switch (...) { case ...: ... }`
Additionally, JavaScript provides the `for...in` loop to iterate over properties
of objects:
```javascript
for (var prop in obj) {
console.log(prop, obj[prop]);
}
```
`prop` is a variable containing the property _name_. You can use bracket
notation to access the property values.
**ES2015**
ES2015 introduces [`for/of`][forof] statements for iterating over
_[iterables][]_:
```js
var arr = [1, 2, 3];
for (var v of arr) {
console.log(v);
}
// 1
// 2
// 3
```
---
title: Demo
layout_data:
description: Run this code and look at the output. Do you notice anything unexpected?
If yes, why do you think this is the case?
---
var obj = {foo: 0, bar: 1, 42: 2};
for (var prop in obj) {
log('property: ' + prop, 'value: ' + obj[prop]);
}
---
title: Exercise
layout_data:
description: |
Log two value: an object which has a property `foo` and a value `"bar"`,
and an array with the values `1`, `2` and `42`.
assertion: |
assert(
output.some(function(x) {
return JSON.stringify(x) === JSON.stringify({foo: 'bar'});
}),
'Your log must contain an object with property "foo" and value "bar". Hint: `{key: value}`.'
);
assert(
output.some(function(x) {
return JSON.stringify(x) === JSON.stringify([1, 2, 42]);
}),
'Your log must contain an array with values 1, 2 and 42. Hint: `[value, ...]`.'
);
---
---
title: Resources
---
- [MDN JavaScript Guide][mdn], [Eloquent JavaScript][eloquent] — basic
JavaScript introduction.
- [You don't know JS][ydkjs] — another JavaScript introduction, more
focus on advanced concepts and technical details
- [ECMAScript 5 specification][ecma] — only if you _really_ like
JavaScript.
- [ECMAScript 2015 specification][es2015]
- [quirksmode.org - JavaScript][quirksmode] — basic introduction and
excellent explanation of event handling.
- [Learning Advanced JavaScript][lajs] — "strange" JavaScript concepts
explored.
- [jsFiddle][] — an online "editor" to quickly prototype JavaScript
examples (with HTML, CSS and a selection of JS libraries).
- [Learn how to **debug** JavaScript][debug] — knowing how to debug a
program written in a specific language is as as important as knowing the
language itself.
[mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
[eloquent]: http://eloquentjavascript.net/
[ydkjs]: https://github.com/getify/You-Dont-Know-JS
[quirksmode]: http://quirksmode.org/js/contents.html
[lajs]: http://ejohn.org/apps/learn/
[ecma]: http://www.ecma-international.org/ecma-262/5.1/
[jsfiddle]: http://jsfiddle.net/
[debug]: https://developers.google.com/chrome-developer-tools/docs/javascript-debugging
---
title: Function definitions
---
There are two syntactic constructs to create functions: function **declaration**
and function **expressions**.
**Function declarations** start with the `function` keyword followed by a
**name**, the parameter list and the function body:
```javascript
function foo(a, b, c) {
// do something
}
```
**Function expressions** have the same structure, but their name is optional:
```javascript
var foo = function (a, b, c) {
// do something
};
```
Note: Since functions are objects, they can be treated like any
other value. They can be assigned to variables, passed to other functions and
returned from functions. The code above is just an assignment expression with a
function as value.
All function objects created either way behave exactly the same. Whether the
parser treats a function definition as declaration or expression depends on
where the definition is placed. If it is an expression context, it is
interpreted as an expression, otherwise as a declaration. That's why
```javascript
function () { }
```
generates an error (function declaration without name), but
```javascript
(function () {});
```
does not, because the grouping operator (`(...)`) can only contain expressions.
---
title: Function Calls
---
Like other C-like languages, functions are called by putting `()` after the
function reference:
```javascript
myFunction();
```
Unlike other languages, functions can be called with any number of arguments,
no matter how many formal parameters they have:
```javascript
function myFunction(foo, bar) {
console.log(foo, bar);
}
myFunction(); // undefined undefined
myFunction(1); // 1 undefined
myFunction(1, 2); // 1 2
myFunction(1, 2, 3); // 1 2
```
Each function has access to the special [`arguments`][arguments] variable,
which is an _array-like_ value. This allows you to access all the arguments
passed to a function, even if there are more than formal parameters:
```javascript
function myFunction(foo, bar) {
console.log(foo, bar, arguments);
}
myFunction(); // undefined undefined []
myFunction(1); // 1 undefined [1]
myFunction(1, 2); // 1 2 [1, 2]
myFunction(1, 2, 3); // 1 2 [1, 2, 3]
```
[arguments]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
title: Insert item inside an Array
tip-number: 00
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Inserting an item into an existing array is a daily common task. You can add elements to the end of an array using push, to the beginning using unshift, or to the middle using splice.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/insert-item-inside-an-array/
# Inserting an item into an existing array
Inserting an item into an existing array is a daily common task. You can add elements to the end of an array using push, to the beginning using unshift, or to the middle using splice.
Those are known methods, but it doesn't mean there isn't a more performant way. Here we go:
## Adding an element at the end
Adding an element at the end of the array is easy with push(), but it can be done in different ways.
```javascript
var arr = [1, 2, 3, 4, 5];
var arr2 = [];
arr.push(6);
arr[arr.length] = 6;
arr2 = arr.concat([6]);
```
Both first methods modify the original array. Don't believe me? Check the [jsperf](http://jsperf.com/push-item-inside-an-array)
### Performance on mobile :
#### Android (v4.2.2)
1. _arr.push(6);_ and _arr[arr.length] = 6;_ have the same performance // 3 319 694 ops/sec
2. _arr2 = arr.concat([6]);_ 50.61 % slower than the other two methods
#### Chrome Mobile (v33.0.0)
1. _arr[arr.length] = 6;_ // 6 125 975 ops/sec
2. _arr.push(6);_ 66.74 % slower
3. _arr2 = arr.concat([6]);_ 87.63 % slower
#### Safari Mobile (v9)
1. _arr[arr.length] = 6;_ // 7 452 898 ops/sec
2. _arr.push(6);_ 40.19 % slower
3. _arr2 = arr.concat([6]);_ 49.78 % slower
```javascript
Final victor
1. arr[arr.length] = 6; // with an average of 5 632 856 ops/sec
2. arr.push(6); // 35.64 % slower
3. arr2 = arr.concat([6]); // 62.67 % slower
```
### Performance on desktop
#### Chrome (v48.0.2564)
1. _arr[arr.length] = 6;_ // 21 602 722 ops/sec
2. _arr.push(6);_ 61.94 % slower
3. _arr2 = arr.concat([6]);_ 87.45 % slower
#### Firefox (v44)
1. _arr.push(6);_ // 56 032 805 ops/sec
2. _arr[arr.length] = 6;_ 0.52 % slower
3. _arr2 = arr.concat([6]);_ 87.36 % slower
#### IE (v11)
1. _arr[arr.length] = 6;_ // 67 197 046 ops/sec
2. _arr.push(6);_ 39.61 % slower
3. _arr2 = arr.concat([6]);_ 93.41 % slower
#### Opera (v35.0.2066.68)
1. _arr[arr.length] = 6;_ // 30 775 071 ops/sec
2. _arr.push(6);_ 71.60 % slower
3. _arr2 = arr.concat([6]);_ 83.70 % slower
#### Safari (v9.0.3)
1. _arr.push(6);_ // 42 670 978 ops/sec
2. _arr[arr.length] = 6;_ 0.80 % slower
3. _arr2 = arr.concat([6]);_ 76.07 % slower
```javascript
Final victor
1. arr[arr.length] = 6; // with an average of 42 345 449 ops/sec
2. arr.push(6); // 34.66 % slower
3. arr2 = arr.concat([6]); // 85.79 % slower
```
## Add an element at the beginning
Now if we are trying to add an item to the beginning of the array:
```javascript
var arr = [1, 2, 3, 4, 5];
arr.unshift(0);
[0].concat(arr);
```
Here is a little more detail: unshift edits the original array; concat returns a new array. [jsperf](http://jsperf.com/unshift-item-inside-an-array)
### Performance on mobile :
#### Android (v4.2.2)
1. _[0].concat(arr);_ // 1 808 717 ops/sec
2. _arr.unshift(0);_ 97.85 % slower
#### Chrome Mobile (v33.0.0)
1. _[0].concat(arr);_ // 1 269 498 ops/sec
2. _arr.unshift(0);_ 99.86 % slower
#### Safari Mobile (v9)
1. _arr.unshift(0);_ // 3 250 184 ops/sec
2. _[0].concat(arr);_ 33.67 % slower
```javascript
Final victor
1. [0].concat(arr); // with an average of 4 972 622 ops/sec
2. arr.unshift(0); // 64.70 % slower
```
### Performance on desktop
#### Chrome (v48.0.2564)
1. _[0].concat(arr);_ // 2 656 685 ops/sec
2. _arr.unshift(0);_ 96.77 % slower
#### Firefox (v44)
1. _[0].concat(arr);_ // 8 039 759 ops/sec
2. _arr.unshift(0);_ 99.72 % slower
#### IE (v11)
1. _[0].concat(arr);_ // 3 604 226 ops/sec
2. _arr.unshift(0);_ 98.31 % slower
#### Opera (v35.0.2066.68)
1. _[0].concat(arr);_ // 4 102 128 ops/sec
2. _arr.unshift(0);_ 97.44 % slower
#### Safari (v9.0.3)
1. _arr.unshift(0);_ // 12 356 477 ops/sec
2. _[0].concat(arr);_ 15.17 % slower
```javascript
Final victor
1. [0].concat(arr); // with an average of 6 032 573 ops/sec
2. arr.unshift(0); // 78.65 % slower
```
## Add an element in the middle
Adding items in the middle of an array is easy with splice, and it's the most performant way to do it.
```javascript
var items = ['one', 'two', 'three', 'four'];
items.splice(items.length / 2, 0, 'hello');
```
I tried to run these tests in various Browsers and OS and the results were similar. I hope these tips will be useful for you and encourage to perform your own tests!
title: Improve Nested Conditionals
tip-number: 03
tip-username: AlbertoFuente
tip-username-profile: https://github.com/AlbertoFuente
tip-tldr: How can we improve and make a more efficient nested `if` statement in javascript?
- /en/improve-nested-conditionals/
How can we improve and make a more efficient nested `if` statement in javascript?
```javascript
if (color) {
if (color === 'black') {
printBlackBackground();
} else if (color === 'red') {
printRedBackground();
} else if (color === 'blue') {
printBlueBackground();
} else if (color === 'green') {
printGreenBackground();
} else {
printYellowBackground();
}
}
```
One way to improve the nested `if` statement would be using the `switch` statement. Although it is less verbose and is more ordered, it's not recommended to use it because it's so difficult to debug errors. Here's [why](https://toddmotto.com/deprecating-the-switch-statement-for-object-literals).
```javascript
switch (color) {
case 'black':
printBlackBackground();
break;
case 'red':
printRedBackground();
break;
case 'blue':
printBlueBackground();
break;
case 'green':
printGreenBackground();
break;
default:
printYellowBackground();
}
```
But what if we have a conditional with several checks in each statement? In this case, if we want it less verbose and more ordered, we can use the conditional `switch`.
If we pass `true` as a parameter to the `switch` statement, it allows us to put a conditional in each case.
```javascript
switch (true) {
case typeof color === 'string' && color === 'black':
printBlackBackground();
break;
case typeof color === 'string' && color === 'red':
printRedBackground();
break;
case typeof color === 'string' && color === 'blue':
printBlueBackground();
break;
case typeof color === 'string' && color === 'green':
printGreenBackground();
break;
case typeof color === 'string' && color === 'yellow':
printYellowBackground();
break;
}
```
If refactoring is an option, we can try to simplify the functions themselves. For example instead of having a function for each background color we could have an function that takes the color as an argument.
```javascript
function printBackground(color) {
if (!color || typeof color !== 'string') {
return; // Invalid color, return immediately
}
}
```
But if refactoring is not an option, we must always avoid having several checks in every condition and avoid using `switch` as much as possible. We also must take into account that the most efficient way to do this is through an `object`.
```javascript
var colorObj = {
black: printBlackBackground,
red: printRedBackground,
blue: printBlueBackground,
green: printGreenBackground,
yellow: printYellowBackground
};
if (color in colorObj) {
colorObj[color]();
}
```
Here you can find more information about [this](http://www.nicoespeon.com/en/2015/01/oop-revisited-switch-in-js/).
title: Sorting strings with accented characters
tip-number: 04
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Javascript has a native method **sort** that allows sorting arrays. Doing a simple `array.sort()` will treat each array entry as a string and sort it alphabetically. But when you try order an array of non ASCII characters you will obtain a strange result.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/sorting-strings-with-accented-characters/
Javascript has a native method **[sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)** that allows sorting arrays. Doing a simple `array.sort()` will treat each array entry as a string and sort it alphabetically. Also you can provide your [own custom sorting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters) function.
```javascript
['Shanghai', 'New York', 'Mumbai', 'Buenos Aires'].sort();
// ["Buenos Aires", "Mumbai", "New York", "Shanghai"]
```
But when you try order an array of non ASCII characters like this `['é', 'a', 'ú', 'c']`, you will obtain a strange result `['c', 'e', 'á', 'ú']`. That happens because sort works only with the English language.
See the next example:
```javascript
// Spanish
['único', 'árbol', 'cosas', 'fútbol'].sort();
// ["cosas", "fútbol", "árbol", "único"] // bad order
// German
['Woche', 'wöchentlich', 'wäre', 'Wann'].sort();
// ["Wann", "Woche", "wäre", "wöchentlich"] // bad order
```
Fortunately, there are two ways to overcome this behavior [localeCompare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) and [Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator) provided by ECMAScript Internationalization API.
> Both methods have their own custom parameters in order to configure it to work adequately.
### Using `localeCompare()`
```javascript
['único', 'árbol', 'cosas', 'fútbol'].sort(function (a, b) {
return a.localeCompare(b);
});
// ["árbol", "cosas", "fútbol", "único"]
['Woche', 'wöchentlich', 'wäre', 'Wann'].sort(function (a, b) {
return a.localeCompare(b);
});
// ["Wann", "wäre", "Woche", "wöchentlich"]
```
### Using `Intl.Collator()`
```javascript
['único', 'árbol', 'cosas', 'fútbol'].sort(Intl.Collator().compare);
// ["árbol", "cosas", "fútbol", "único"]
['Woche', 'wöchentlich', 'wäre', 'Wann'].sort(Intl.Collator().compare);
// ["Wann", "wäre", "Woche", "wöchentlich"]
```
- For each method you can customize the location.
- According to [Firefox](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#Performance) Intl.Collator is faster when comparing large numbers of strings.
So when you are working with arrays of strings in a language other than English, remember to use this method to avoid unexpected sorting.
title: Differences between `undefined` and `null`
tip-number: 05
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Understanding the differences between `undefined` and `null`.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/differences-between-undefined-and-null/
- `undefined` means a variable has not been declared, or has been declared but has not yet been assigned a value
- `null` is an assignment value that means "no value"
- Javascript sets unassigned variables with a default value of `undefined`
- Javascript never sets a value to `null`. It is used by programmers to indicate that a `var` has no value.
- `undefined` is not valid in JSON while `null` is
- `undefined` typeof is `undefined`
- `null` typeof is an `object`. [Why?](http://www.2ality.com/2013/10/typeof-null.html)
- Both are primitives
- Both are [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)
(`Boolean(undefined) // false`, `Boolean(null) // false`)
- You can know if a variable is [undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)
```javascript
typeof variable === 'undefined';
```
````
- You can check if a variable is [null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)
```javascript
variable === null
````
- The **equality** operator considers them equal, but the **identity** doesn't
```javascript
null == undefined; // true
null === undefined; // false
```
```
```
title: Writing a single method for arrays and a single element
tip-number: 06
tip-username: mattfxyz
tip-username-profile: https://twitter.com/mattfxyz
tip-tldr: Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (`css` will modify everything matched by the selector).
- /en/writing-a-single-method-for-arrays-and-a-single-element/
Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (`css` will modify everything matched by the selector).
You just have to concat everything into an array first. `Array.concat` will accept an array or a single element.
```javascript
function printUpperCase(words) {
var elements = [].concat(words || []);
for (var i = 0; i < elements.length; i++) {
console.log(elements[i].toUpperCase());
}
}
```
`printUpperCase` is now ready to accept a single node or an array of nodes as its parameter. It also avoids the potential `TypeError` that would be thrown if no parameter was passed.
```javascript
printUpperCase('cactus');
// => CACTUS
printUpperCase(['cactus', 'bear', 'potato']);
// => CACTUS
// BEAR
// POTATO
```
title: use strict and get lazy
tip-number: 07
tip-username: nainslie
tip-username-profile: https://twitter.com/nat5an
tip-tldr: Strict-mode JavaScript makes it easier for the developer to write "secure" JavaScript.
- /en/use-strict-and-get-lazy/
Strict-mode JavaScript makes it easier for the developer to write "secure" JavaScript.
By default, JavaScript allows the programmer to be pretty careless, for example, by not requiring us to declare our variables with "var" when we first introduce them. While this may seem like a convenience to the unseasoned developer, it's also the source of many errors when a variable name is misspelled or accidentally referred to out of its scope.
Programmers like to make the computer do the boring stuff for us, and automatically check our work for mistakes. That's what the JavaScript "use strict" directive allows us to do, by turning our mistakes into JavaScript errors.
We add this directive either by adding it at the top of a js file:
```javascript
// Whole-script strict mode syntax
'use strict';
var v = "Hi! I'm a strict mode script!";
```
or inside a function:
```javascript
function f() {
// Function-level strict mode syntax
'use strict';
function nested() {
return 'And so am I!';
}
return "Hi! I'm a strict mode function! " + nested();
}
function f2() {
return "I'm not strict.";
}
```
By including this directive in a JavaScript file or function, we will direct the JavaScript engine to execute in strict mode which disables a bunch of behaviors that are usually undesirable in larger JavaScript projects. Among other things, strict mode changes the following behaviors:
- Variables can only be introduced when they are preceded with "var"
- Attempting to write to read-only properties generates a noisy error
- You have to call constructors with the "new" keyword
- "this" is not implicitly bound to the global object
- Very limited use of eval() allowed
- Protects you from using reserved words or future reserved words as variable names
Strict mode is great for new projects, but can be challenging to introduce into older projects that don't already use it in most places. It also can be problematic if your build chain concatenates all your js files into one big file, as this may cause all files to execute in strict mode.
It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
Strict mode is supported in:
- Internet Explorer from version 10.
- Firefox from version 4.
- Chrome from version 13.
- Safari from version 5.1.
- Opera from version 12.
[See MDN for a fuller description of strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode).
title: Converting a Node List to an Array
tip-number: 08
tip-username: Tevko
tip-username-profile: https://twitter.com/tevko
tip-tldr: Here's a quick, safe, and reusable way to convert a node list into an array of DOM elements.
- /en/converting-a-node-list-to-an-array/
The `querySelectorAll` method returns an array-like object called a node list. These data structures are referred to as "Array-like", because they appear as an array, but can not be used with array methods like `map` and `forEach`. Here's a quick, safe, and reusable way to convert a node list into an array of DOM elements:
```javascript
const nodelist = document.querySelectorAll('div');
const nodelistToArray = Array.apply(null, nodelist);
//later on ..
nodelistToArray.forEach(...);
nodelistToArray.map(...);
nodelistToArray.slice(...);
//etc...
```
The `apply` method is used to pass an array of arguments to a function with a given `this` value. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) states that `apply` will take an array-like object, which is exactly what `querySelectorAll` returns. Since we don't need to specify a value for `this` in the context of the function, we pass in `null` or `0`. The result is an actual array of DOM elements which contains all of the available array methods.
Alternatively you can use `Array.prototype.slice` combined with `Function.prototype.call` or `Function.prototype.apply` passing the array-like object as the value of `this`:
```javascript
const nodelist = document.querySelectorAll('div');
const nodelistToArray = Array.prototype.slice.call(nodelist); // or equivalently Array.prototype.slice.apply(nodelist);
//later on ..
nodelistToArray.forEach(...);
nodelistToArray.map(...);
nodelistToArray.slice(...);
//etc...
```
Or if you are using ES2015 you can use the [spread operator `...`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)
```js
const nodelist = [...document.querySelectorAll('div')]; // returns a real array
//later on ..
nodelist.forEach(...);
nodelist.map(...);
nodelist.slice(...);
//etc...
```
title: Template Strings
tip-number: 09
tip-username: JakeRawr
tip-username-profile: https://github.com/JakeRawr
tip-tldr: As of ES6, JS now has template strings as an alternative to the classic end quotes strings.
- /en/template-strings/
As of ES6, JS now has template strings as an alternative to the classic end quotes strings.
Ex:
Normal string
```javascript
var firstName = 'Jake';
var lastName = 'Rawr';
console.log('My name is ' + firstName + ' ' + lastName);
// My name is Jake Rawr
```
Template String
```javascript
var firstName = 'Jake';
var lastName = 'Rawr';
console.log(`My name is ${firstName} ${lastName}`);
// My name is Jake Rawr
```
You can do multi-line strings without `\n`, perform simple logic (ie 2+3) or even use the [ternary operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) inside `${}` in template strings.
```javascript
var val1 = 1,
val2 = 2;
console.log(`${val1} is ${val1 < val2 ? 'less than' : 'greater than'} ${val2}`);
// 1 is less than 2
```
You are also able to modify the output of template strings using a function; they are called [tagged template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings#Tagged_template_strings) for example usages of tagged template strings.
You may also want to [read](https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2) to understand template strings more.
title: Check if a property is in a Object
tip-number: 10
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: These are ways to check if a property is present in an object.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/check-if-a-property-is-in-a-object/
When you have to check if a property is present in an [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects), you probably are doing something like this:
```javascript
var myObject = {
name: '@tips_js'
};
if (myObject.name) { ... }
```
That's ok, but you have to know that there are two native ways for this kind of thing, the [`in` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) and [`Object.hasOwnProperty`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty). Every object descended from `Object`, has both ways available.
### See the big Difference
```javascript
var myObject = {
name: '@tips_js'
};
myObject.hasOwnProperty('name'); // true
'name' in myObject; // true
myObject.hasOwnProperty('valueOf'); // false, valueOf is inherited from the prototype chain
'valueOf' in myObject; // true
```
Both differ in the depth at which they check the properties. In other words, `hasOwnProperty` will only return true if key is available on that object directly. However, the `in` operator doesn't discriminate between properties created on an object and properties inherited from the prototype chain.
Here's another example:
```javascript
var myFunc = function () {
this.name = '@tips_js';
};
myFunc.prototype.age = '10 days';
var user = new myFunc();
user.hasOwnProperty('name'); // true
user.hasOwnProperty('age'); // false, because age is from the prototype chain
```
Check the [live examples here](https://jsbin.com/tecoqa/edit?js,console)!
I also recommend reading [this discussion](https://github.com/loverajoel/jstips/issues/62) about common mistakes made when checking a property's existence in objects.
title: Hoisting
tip-number: 11
tip-username: squizzleflip
tip-username-profile: https://twitter.com/squizzleflip
tip-tldr: Understanding hoisting will help you organize your function scope.
Understanding [hoisting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting) will help you organize your function scope. Just remember, variable declarations and function definitions are hoisted to the top. Variable definitions are not, even if you declare and define a variable on the same line. Also, a variable **declaration** lets the system know that the variable exists while **definition** assigns it a value.
```javascript
function doTheThing() {
// ReferenceError: notDeclared is not defined
console.log(notDeclared);
// Outputs: undefined
console.log(definedLater);
var definedLater;
definedLater = 'I am defined!';
// Outputs: 'I am defined!'
console.log(definedLater);
// Outputs: undefined
console.log(definedSimulateneously);
var definedSimulateneously = 'I am defined!';
// Outputs: 'I am defined!'
console.log(definedSimulateneously);
// Outputs: 'I did it!'
doSomethingElse();
function doSomethingElse() {
console.log('I did it!');
}
// TypeError: undefined is not a function
functionVar();
var functionVar = function () {
console.log('I did it!');
};
}
```
To make things easier to read, declare all of your variables at the top of your function scope so it is clear which scope the variables are coming from. Define your variables before you need to use them. Define your functions at the bottom of your scope to keep them out of your way.
title: Pseudomandatory parameters in ES6 functions
tip-number: 12
tip-username: Avraam Mavridis
tip-username-profile: https://github.com/AvraamMavridis
tip-tldr: In many programming languages the parameters of a function are by default mandatory and the developer has to explicitly define that a parameter is optional.
- /en/pseudomandatory-parameters-in-es6-functions/
In many programming languages the parameters of a function are by default mandatory and the developer has to explicitly define that a parameter is optional. In Javascript, every parameter is optional, but we can enforce this behavior without messing with the actual body of a function, taking advantage of [**es6's default values for parameters**] (http://exploringjs.com/es6/ch_parameter-handling.html#sec_parameter-default-values) feature.
```javascript
const _err = function (message) {
throw new Error(message);
};
const getSum = (a = _err('a is not defined'), b = _err('b is not defined')) => a + b;
getSum(10); // throws Error, b is not defined
getSum(undefined, 10); // throws Error, a is not defined
```
`_err` is a function that immediately throws an Error. If no value is passed for one of the parameters, the default value is going to be used, `_err` will be called and an Error will be thrown. You can see more examples for the **default parameters feature** on [Mozilla's Developer Network ](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters)
title: Tip to measure performance of a javascript block
tip-number: 13
tip-username: manmadareddy
tip-username-profile: https://twitter.com/manmadareddy
tip-tldr: For quickly measuring performance of a javascript block, we can use the console functions like `console.time(label)` and `console.timeEnd(label)`
- /en/tip-to-measure-performance-of-a-javascript-block/
For quickly measuring performance of a javascript block, we can use the console functions like
[`console.time(label)`](https://developer.chrome.com/devtools/docs/console-api#consoletimelabel) and [`console.timeEnd(label)`](https://developer.chrome.com/devtools/docs/console-api#consoletimeendlabel)
```javascript
console.time('Array initialize');
var arr = new Array(100),
len = arr.length,
i;
for (i = 0; i < len; i++) {
arr[i] = new Object();
}
console.timeEnd('Array initialize'); // Outputs: Array initialize: 0.711ms
```
More info:
[Console object](https://github.com/DeveloperToolsWG/console-object),
[Javascript benchmarking](https://mathiasbynens.be/notes/javascript-benchmarking)
Demo: [jsfiddle](https://jsfiddle.net/meottb62/) - [codepen](http://codepen.io/anon/pen/JGJPoa) (outputs in browser console)
> Note: As [Mozilla](https://developer.mozilla.org/en-US/docs/Web/API/Console/time) suggested don't use this for production sites, use it for development purposes only.
title: Fat Arrow Functions
tip-number: 14
tip-username: pklinger
tip-username-profile: https://github.com/pklinger/
tip-tldr: Introduced as a new feature in ES6, fat arrow functions may come as a handy tool to write more code in fewer lines
- /en/fat-arrow-functions/
Introduced as a new feature in ES6, fat arrow functions may come as a handy tool to write more code in fewer lines. The name comes from its syntax, `=>`, which is a 'fat arrow', as compared to a thin arrow `->`. Some programmers might already know this type of function from different languages such as Haskell, as 'lambda expressions', or as 'anonymous functions'. It is called anonymous, as these arrow functions do not have a descriptive function name.
### What are the benefits?
- Syntax: fewer LOC; no more typing `function` keyword over and over again
- Semantics: capturing the keyword `this` from the surrounding context
### Simple syntax example
Have a look at these two code snippets, which do the exact same job, and you will quickly understand what fat arrow functions do:
```javascript
// general syntax for fat arrow functions
param => expression
// may also be written with parentheses
// parentheses are required on multiple params
(param1 [, param2]) => expression
// using functions
var arr = [5,3,2,9,1];
var arrFunc = arr.map(function(x) {
return x * x;
});
console.log(arr)
// using fat arrow
var arr = [5,3,2,9,1];
var arrFunc = arr.map((x) => x*x);
console.log(arr)
```
As you can see, the fat arrow function in this case can save you time typing out the parentheses as well as the function and return keywords. I would advise you to always write parentheses around the parameter inputs, as the parentheses will be needed for multiple input parameters, such as in `(x,y) => x+y`. It is just a way to cope with forgetting them in different use cases. But the code above would also work like this: `x => x*x`. So far, these are only syntactical improvements, which lead to fewer LOC and better readability.
### Lexically binding `this`
There is another good reason to use fat arrow functions. There is the issue with the context of `this`. With arrow functions, you don't need to worry about `.bind(this)` or setting `that = this` anymore, as fat arrow functions pick the context of `this` from the lexical surrounding. Have a look at the next [example] (https://jsfiddle.net/pklinger/rw94oc11/):
```javascript
// globally defined this.i
this.i = 100;
var counterA = new CounterA();
var counterB = new CounterB();
var counterC = new CounterC();
var counterD = new CounterD();
// bad example
function CounterA() {
// CounterA's `this` instance (!! gets ignored here)
this.i = 0;
setInterval(function () {
// `this` refers to global object, not to CounterA's `this`
// therefore starts counting with 100, not with 0 (local this.i)
this.i++;
document.getElementById('counterA').innerHTML = this.i;
}, 500);
}
// manually binding that = this
function CounterB() {
this.i = 0;
var that = this;
setInterval(function () {
that.i++;
document.getElementById('counterB').innerHTML = that.i;
}, 500);
}
// using .bind(this)
function CounterC() {
this.i = 0;
setInterval(
function () {
this.i++;
document.getElementById('counterC').innerHTML = this.i;
}.bind(this),
500
);
}
// fat arrow function
function CounterD() {
this.i = 0;
setInterval(() => {
this.i++;
document.getElementById('counterD').innerHTML = this.i;
}, 500);
}
```
Further information about fat arrow functions may be found at [MDN] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). To see different syntax options visit [this site] (http://jsrocks.org/2014/10/arrow-functions-and-their-scope/).
title: Even simpler way of using `indexOf` as a contains clause
tip-number: 15
tip-username: jhogoforbroke
tip-username-profile: https://twitter.com/jhogoforbroke
tip-tldr: JavaScript by default does not have a contains method. And for checking existence of a substring in a string or an item in an array you may do this.
- /en/even-simpler-way-of-using-indexof-as-a-contains-clause/
JavaScript by default does not have a contains method. And for checking existence of a substring in a string or an item in an array you may do this:
```javascript
var someText = 'javascript rules';
if (someText.indexOf('javascript') !== -1) {
}
// or
if (someText.indexOf('javascript') >= 0) {
}
```
But let's look at these [Expressjs](https://github.com/strongloop/express) code snippets.
[examples/mvc/lib/boot.js](https://github.com/strongloop/express/blob/2f8ac6726fa20ab5b4a05c112c886752868ac8ce/examples/mvc/lib/boot.js#L26)
```javascript
for (var key in obj) {
// "reserved" exports
if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
```
[lib/utils.js](https://github.com/strongloop/express/blob/2f8ac6726fa20ab5b4a05c112c886752868ac8ce/lib/utils.js#L93)
```javascript
exports.normalizeType = function (type) {
return ~type.indexOf('/') ? acceptParams(type) : { value: mime.lookup(type), params: {} };
};
```
[examples/web-service/index.js](https://github.com/strongloop/express/blob/2f8ac6726fa20ab5b4a05c112c886752868ac8ce/examples/web-service/index.js#L35)
```javascript
// key is invalid
if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));
```
The gotcha is the [bitwise operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) **~**, "Bitwise operators perform their operations on binary representations, but they return standard JavaScript numerical values."
It transforms `-1` into `0`, and `0` evaluates to `false` in JavaScript:
```javascript
var someText = 'text';
!!~someText.indexOf('tex'); // someText contains "tex" - true
!~someText.indexOf('tex'); // someText NOT contains "tex" - false
~someText.indexOf('asd'); // someText doesn't contain "asd" - false
~someText.indexOf('ext'); // someText contains "ext" - true
```
### String.prototype.includes()
ES6 introduced the [includes() method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) and you can use it to determine whether or not a string includes another string:
```javascript
'something'.includes('thing'); // true
```
With ECMAScript 2016 (ES7) it is even possible to use these techniques with Arrays:
```javascript
!!~[1, 2, 3].indexOf(1); // true
[1, 2, 3].includes(1); // true
```
**Unfortunately, it is only supported in Chrome, Firefox, Safari 9 or above and Edge; not IE11 or lower.**
**It's better used in controlled environments.**
title: Passing arguments to callback functions
tip-number: 16
tip-username: minhazav
tip-username-profile: https://twitter.com/minhazav
tip-tldr: By default you cannot pass arguments to a callback function, but you can take advantage of the closure scope in Javascript to pass arguments to callback functions.
- /en/passing-arguments-to-callback-functions/
By default you cannot pass arguments to a callback function. For example:
```js
function callback() {
console.log('Hi human');
}
document.getElementById('someelem').addEventListener('click', callback);
```
You can take advantage of the closure scope in Javascript to pass arguments to callback functions. Check this example:
```js
function callback(a, b) {
return function () {
console.log('sum = ', a + b);
};
}
var x = 1,
y = 2;
document.getElementById('someelem').addEventListener('click', callback(x, y));
```
### What are closures?
Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. [Check MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) to learn more.
So this way the arguments `x` and `y` are in scope of the callback function when it is called.
Another method to do this is using the `bind` method. For example:
```js
var alertText = function (text) {
alert(text);
};
document.getElementById('someelem').addEventListener('click', alertText.bind(this, 'hello'));
```
There is a very slight difference in performance of both methods, checkout [jsperf](http://jsperf.com/bind-vs-closure-23).
title: Node.js - Run a module if it is not `required`
tip-number: 17
tip-username: odsdq
tip-username-profile: https://twitter.com/odsdq
tip-tldr: In node, you can tell your program to do two different things depending on whether the code is run from `require('./something.js')` or `node something.js`. This is useful if you want to interact with one of your modules independently.
- /en/nodejs-run-a-module-if-it-is-not-required/
In node, you can tell your program to do two different things depending on whether the code is run from `require('./something.js')` or `node something.js`. This is useful if you want to interact with one of your modules independently.
```js
if (!module.parent) {
// ran with `node something.js`
app.listen(8088, function () {
console.log('app listening on port 8088');
});
} else {
// used with `require('/.something.js')`
module.exports = app;
}
```
See [the documentation for modules](https://nodejs.org/api/modules.html#modules_module_parent) for more info.
title: Truncating the fast (but risky) way
tip-number: 18
tip-username: pklinger
tip-username-profile: https://github.com/pklinger
tip-tldr: .`~~X` is usually a faster `Math.trunc(X)`, but can also make your code do nasty things.
- /en/rounding-the-fast-way/
This tip is about performance...with a hidden price tag.
Have you ever come across the [double tilde `~~` operator](http://stackoverflow.com/questions/5971645/what-is-the-double-tilde-operator-in-javascript)? It's also often called the "double bitwise NOT" operator. You can often use it as a faster substitute for `Math.trunc()`. Why is that?
One bitwise shift `~` first truncates `input` to 32 bits, then transforms it into `-(input+1)`. The double bitwise shift therefore transforms the input into `-(-(input + 1)+1)` making it a great tool to round towards zero. For numeric input, it therefore mimics `Math.trunc()`. On failure, `0` is returned, which might come in handy sometimes instead of `Math.trunc()`, which returns `NaN` on failure.
```js
// single ~
console.log(~1337); // -1338
// numeric input
console.log(~~47.11); // -> 47
console.log(~~1.9999); // -> 1
console.log(~~3); // -> 3
```
However, while `~~` is probably a better performer, experienced programmers often stick with `Math.trunc()` instead. To understand why, here's a clinical view on this operator.
### INDICATIONS
##### When every CPU cycle counts
`~~` is probably faster than `Math.trunc()` across the board, though you should [test that assumption](https://jsperf.com/jsfvsbitnot/10) on whichever platforms matter to you. Also, you'd generally have to perform millions of such operations to have any visible impact at run time.
##### When code clarity is not a concern
If you're trying to confuse others, or get maximum utility from your minifier/uglifier, this is a relatively cheap way to do it.
### CONTRAINDICATIONS
##### When your code needs to be maintained
Code clarity is of great importance in the long term, whether you work in a team, contribute to public code repos, or fly solo. As [the oft-quoted saying](http://c2.com/cgi/wiki?CodeForTheMaintainer) goes:
> Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
For a solo programmer, that psychopath is inevitably "you in six months".
##### When you forget that `~~` always rounds to zero
Newbie programmers may fixate on the cleverness of `~~`, forgetting the significance of "just drop the fractional portion of this number". This can easily lead to **fencepost errors** (a.k.a. "off-by-one") when transforming floats to array indices or related ordinal values, where a different kind of fractional rounding may actually be called for. (Lack of code clarity usually contributes to this problem.)
For instance, if you're counting numbers on a "nearest integer" basis, you should use `Math.round()` instead of `~~`, but programmer laziness and the impact of **_10 whole characters saved per use_** on human fingers often triumph over cold logic, leading to incorrect results.
In contrast, the very names of the `Math.xyz()` functions clearly communicate their effect, reducing the probability of accidental errors.
##### When dealing with large-magnitude numbers
Because `~` first does a 32-bit conversion, `~~` results in bogus values around ±2.15 billion. If you don't properly range-check your input, a user could trigger unexpected behavior when the transformed value ends up being a great distance from the original:
```js
a = 2147483647.123; // maximum positive 32-bit integer, plus a bit more
console.log(~~a); // -> 2147483647 (ok)
a += 10000; // -> 2147493647.123 (ok)
console.log(~~a); // -> -2147483648 (huh?)
```
One particularly vulnerable area involves dealing with Unix epoch timestamps (measured in seconds from 1 Jan 1970 00:00:00 UTC). A quick way to get such values is:
```js
epoch_int = ~~(+new Date() / 1000); // Date() epochs in milliseconds, so we scale accordingly
```
However, when dealing with timestamps after 19 Jan 2038 03:14:07 UTC (sometimes called the **Y2038 limit**), this breaks horribly:
```js
// epoch timestamp for 1 Jan 2040 00:00:00.123 UTC
epoch = +new Date('2040-01-01') / 1000 + 0.123; // -> 2208988800.123
// back to the future!
epoch_int = ~~epoch; // -> -2085978496
console.log(new Date(epoch_int * 1000)); // -> Wed Nov 25 1903 17:31:44 UTC
// that was fun, now let's get real
epoch_flr = Math.floor(epoch); // -> 2208988800
console.log(new Date(epoch_flr * 1000)); // -> Sun Jan 01 2040 00:00:00 UTC
```
##### When the original input wasn't sanitized
Because `~~` transforms every non-number into `0`:
```js
console.log(~~[]); // -> 0
console.log(~~NaN); // -> 0
console.log(~~null); // -> 0
```
some programmers treat it as alternative to proper input validation. However, this can lead to strange logic bugs down the line, since you're no longer distinguishing between invalid inputs and actual `0` values. This is therefore _not_ a recommended practice.
##### When so many people think `~~X == Math.floor(X)`
Most people who write about "double bitwise NOT" incorrectly equate it with `Math.floor()` for some reason. If you can't write about it accurately, odds are good you'll eventually misuse it.
Others are more careful to mention `Math.floor()` for positive inputs and `Math.ceil()` for negative ones, but that forces you to stop and think about the values you're dealing with. This defeats the purpose of `~~` as a handy no-gotchas shortcut.
### DOSAGE
Avoid where possible. Use sparingly otherwise.
### ADMINISTRATION
1. Apply cautiously.
2. Sanitize values before applying.
3. Carefully document relevant assumptions about the values being transformed.
4. Review code to deal with, at minimum:
- logic bugs where invalid inputs are instead passed to other code modules as valid `0` values
- range errors on transformed inputs
- fencepost errors due to incorrect rounding direction
title: Safe string concatenation
tip-number: 19
tip-username: gogainda
tip-username-profile: https://twitter.com/gogainda
tip-tldr: Suppose you have a couple of variables with unknown types and you want to concatenate them in a string. To be sure that the arithmetical operation is not be applied during concatenation, use concat
- /en/safe-string-concatenation/
Suppose you have a couple of variables with unknown types and you want to concatenate them in a string. To be sure that the arithmetical operation is not be applied during concatenation, use `concat`:
```javascript
var one = 1;
var two = 2;
var three = '3';
var result = ''.concat(one, two, three); //"123"
```
This way of concatenting does exactly what you'd expect. In contrast, concatenation with pluses might lead to unexpected results:
```javascript
var one = 1;
var two = 2;
var three = '3';
var result = one + two + three; //"33" instead of "123"
```
Speaking about performance, compared to the `join` [type](http://www.sitepoint.com/javascript-fast-string-concatenation/) of concatenation, the speed of `concat` is pretty much the same.
You can read more about the `concat` function on MDN [page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat).
title: Return objects to enable chaining of functions
tip-number: 20
tip-username: WakeskaterX
tip-username-profile: https://twitter.com/WakeStudio
tip-tldr: When creating functions on an object in Object Oriented Javascript, returning the object in the function will enable you to chain functions together.
- /en/return-objects-to-enable-chaining-of-functions/
When creating functions on an object in Object Oriented Javascript, returning the object in the function will enable you to chain functions together.
```js
function Person(name) {
this.name = name;
this.sayName = function () {
console.log('Hello my name is: ', this.name);
return this;
};
this.changeName = function (name) {
this.name = name;
return this;
};
}
var person = new Person('John');
person.sayName().changeName('Timmy').sayName();
```
title: Shuffle an Array
tip-number: 21
tip-username: 0xmtn
tip-username-profile: https://github.com/0xmtn/
tip-tldr: Fisher-Yates Shuffling it's an algorithm to shuffle an array.
- /en/shuffle-an-array/
This snippet here uses [Fisher-Yates Shuffling](https://www.wikiwand.com/en/Fisher%E2%80%93Yates_shuffle) Algorithm to shuffle a given array.
```javascript
function shuffle(arr) {
var i, j, temp;
for (i = arr.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
```
An example:
```javascript
var a = [1, 2, 3, 4, 5, 6, 7, 8];
var b = shuffle(a);
console.log(b);
// [2, 7, 8, 6, 5, 3, 1, 4]
```
title: Two ways to empty an array
tip-number: 22
tip-username: microlv
tip-username-profile: https://github.com/microlv
tip-tldr: In JavaScript when you want to empty an array, there are a lot ways, but this is the most performant.
- /en/two-ways-to-empty-an-array/
You define an array and want to empty its contents.
Usually, you would do it like this:
```javascript
// define Array
var list = [1, 2, 3, 4];
function empty() {
//empty your array
list = [];
}
empty();
```
But there is another way to empty an array that is more performant.
You should use code like this:
```javascript
var list = [1, 2, 3, 4];
function empty() {
//empty your array
list.length = 0;
}
empty();
```
- `list = []` assigns a reference to a new array to a variable, while any other references are unaffected.
which means that references to the contents of the previous array are still kept in memory, leading to memory leaks.
- `list.length = 0` deletes everything in the array, which does hit other references.
In other words, if you have two references to the same array (`a = [1,2,3]; a2 = a;`), and you delete the array's contents using `list.length = 0`, both references (a and a2) will now point to the same empty array. (So don't use this technique if you don't want a2 to hold an empty array!)
Think about what this will output:
```js
var foo = [1, 2, 3];
var bar = [1, 2, 3];
var foo2 = foo;
var bar2 = bar;
foo = [];
bar.length = 0;
console.log(foo, bar, foo2, bar2);
// [] [] [1, 2, 3] []
```
Stackoverflow more detail:
[difference-between-array-length-0-and-array](http://stackoverflow.com/questions/4804235/difference-between-array-length-0-and-array)
title: Converting to number fast way
tip-number: 23
tip-username: sonnyt
tip-username-profile: http://twitter.com/sonnyt
tip-tldr: Converting strings to numbers is extremely common. The easiest and fastest way to achieve that would be using the + operator.
- /en/converting-to-number-fast-way/
Converting strings to numbers is extremely common. The easiest and fastest ([jsPerf](https://jsperf.com/number-vs-parseint-vs-plus/29)) way to achieve that would be using the `+` (plus) operator.
```javascript
var one = '1';
var numberOne = +one; // Number 1
```
You can also use the `-` (minus) operator which type-converts the value into number but also negates it.
```javascript
var one = '1';
var negativeNumberOne = -one; // Number -1
```
title: Use === instead of ==
tip-number: 24
tip-username: bhaskarmelkani
tip-username-profile: https://www.twitter.com/bhaskarmelkani
tip-tldr: The `==` (or `!=`) operator performs an automatic type conversion if needed. The `===` (or `!==`) operator will not perform any conversion. It compares the value and the type, which could be considered faster ([jsPref](http://jsperf.com/strictcompare)) than `==`.
- /en/use*===\_instead_of*==/
The `==` (or `!=`) operator performs an automatic type conversion if needed. The `===` (or `!==`) operator will not perform any conversion. It compares the value and the type, which could be considered faster ([jsPref](http://jsperf.com/strictcompare)) than `==`.
```js
[10] == 10 // is true
[10] === 10 // is false
'10' == 10 // is true
'10' === 10 // is false
[] == 0 // is true
[] === 0 // is false
'' == false // is true but true == "a" is false
'' === false // is false
```
title: Using immediately invoked function expression
tip-number: 25
tip-username: rishantagarwal
tip-username-profile: https://github.com/rishantagarwal
tip-tldr: Called as "Iffy" ( IIFE - immediately invoked function expression) is an anonymous function expression that is immediately invoked and has some important uses in Javascript.
- /en/Using-immediately-invoked-function-expression/
Called as "Iffy" ( IIFE - immediately invoked function expression) is an anonymous function expression that is immediately invoked and has some important uses in Javascript.
```javascript
(function () {
// Do something
})();
```
It is an anonymous function expression that is immediately invoked, and it has some particularly important uses in JavaScript.
The pair of parenthesis surrounding the anonymous function turns the anonymous function into a function expression or variable expression. So instead of a simple anonymous function in the global scope, or wherever it was defined, we now have an unnamed function expression.
Similarly, we can even create a named, immediately invoked function expression:
```javascript
(someNamedFunction = function(msg) {
console.log(msg || "Nothing for today !!")
}) (); // Output --> Nothing for today !!
someNamedFunction("Javascript rocks !!"); // Output --> Javascript rocks !!
someNamedFunction(); // Output --> Nothing for today !!
```
For more details, check the following URL's -
1. [Link 1](https://blog.mariusschulz.com/2016/01/13/disassembling-javascripts-iife-syntax)
2. [Link 2](http://javascriptissexy.com/12-simple-yet-powerful-javascript-tips/)
Performance:
[jsPerf](http://jsperf.com/iife-with-call)
title: Filtering and Sorting a List of Strings
tip-number: 26
tip-username: davegomez
tip-username-profile: https://github.com/davegomez
tip-tldr: You may have a big list of names you need to filter in order to remove duplicates and sort them alphabetically.
c
- /en/filtering-and-sorting-a-list-of-strings/
You may have a big list of names you need to filter in order to remove duplicates and sort them alphabetically.
In our example we are going to use the list of **JavaScript reserved keywords** we can find across the different versions of the language, but as you can notice, there is a lot of duplicated keywords and they are not alphabetically organized. So this is a perfect list ([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)) of strings to test out this JavaScript tip.
```js
var keywords = [
'do',
'if',
'in',
'for',
'new',
'try',
'var',
'case',
'else',
'enum',
'null',
'this',
'true',
'void',
'with',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'delete',
'export',
'import',
'return',
'switch',
'typeof',
'default',
'extends',
'finally',
'continue',
'debugger',
'function',
'do',
'if',
'in',
'for',
'int',
'new',
'try',
'var',
'byte',
'case',
'char',
'else',
'enum',
'goto',
'long',
'null',
'this',
'true',
'void',
'with',
'break',
'catch',
'class',
'const',
'false',
'final',
'float',
'short',
'super',
'throw',
'while',
'delete',
'double',
'export',
'import',
'native',
'public',
'return',
'static',
'switch',
'throws',
'typeof',
'boolean',
'default',
'extends',
'finally',
'package',
'private',
'abstract',
'continue',
'debugger',
'function',
'volatile',
'interface',
'protected',
'transient',
'implements',
'instanceof',
'synchronized',
'do',
'if',
'in',
'for',
'let',
'new',
'try',
'var',
'case',
'else',
'enum',
'eval',
'null',
'this',
'true',
'void',
'with',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'yield',
'delete',
'export',
'import',
'public',
'return',
'static',
'switch',
'typeof',
'default',
'extends',
'finally',
'package',
'private',
'continue',
'debugger',
'function',
'arguments',
'interface',
'protected',
'implements',
'instanceof',
'do',
'if',
'in',
'for',
'let',
'new',
'try',
'var',
'case',
'else',
'enum',
'eval',
'null',
'this',
'true',
'void',
'with',
'await',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'yield',
'delete',
'export',
'import',
'public',
'return',
'static',
'switch',
'typeof',
'default',
'extends',
'finally',
'package',
'private',
'continue',
'debugger',
'function',
'arguments',
'interface',
'protected',
'implements',
'instanceof'
];
```
Since we don't want to change our original list, we are going to use a high order function named [`filter`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), which will return a new filter array based in a predicate (_function_) we pass to it. The predicate will compare the index of the current keyword in the original list with its `index` in the new list and will push it to the new array only if the indexes match.
Finally we are going to sort the filtered list using the [`sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function which takes a comparison function as the only argument, returning a alphabetically sorted list.
```js
var filteredAndSortedKeywords = keywords
.filter(function (keyword, index) {
return keywords.lastIndexOf(keyword) === index;
})
.sort(function (a, b) {
return a < b ? -1 : 1;
});
```
The **ES6** (ECMAScript 2015) version using [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) looks a little simpler:
```js
const filteredAndSortedKeywords = keywords.filter((keyword, index) => keywords.lastIndexOf(keyword) === index).sort((a, b) => (a < b ? -1 : 1));
```
And this is the final filtered and sorted list of JavaScript reserved keywords:
```js
console.log(filteredAndSortedKeywords);
// ['abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'eval', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield']
```
_Thanks to [@nikshulipa](https://github.com/nikshulipa), [@kirilloid](https://twitter.com/kirilloid), [@lesterzone](https://twitter.com/lesterzone), [@tracker1](https://twitter.com/tracker1), [@manuel_del_pozo](https://twitter.com/manuel_del_pozo) for all the comments and suggestions!_
title: Short circuit evaluation in JS.
tip-number: 27
tip-username: bhaskarmelkani
tip-username-profile: https://www.twitter.com/bhaskarmelkani
tip-tldr: Short-circuit evaluation says, the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression, when the first argument of the AND `&&` function evaluates to false, the overall value must be false, and when the first argument of the OR `||` function evaluates to true, the overall value must be true.
- /en/short-circuit-evaluation-in-js/
[Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation) says, the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND (`&&`) function evaluates to false, the overall value must be false; and when the first argument of the OR (`||`) function evaluates to true, the overall value must be true.
For the following `test` condition and `isTrue` and `isFalse` function.
```js
var test = true;
var isTrue = function () {
console.log('Test is true.');
};
var isFalse = function () {
console.log('Test is false.');
};
```
Using logical AND - `&&`.
```js
// A normal if statement.
if (test) {
isTrue(); // Test is true
}
// Above can be done using '&&' as -
test && isTrue(); // Test is true
```
Using logical OR - `||`.
```js
test = false;
if (!test) {
isFalse(); // Test is false.
}
test || isFalse(); // Test is false.
```
The logical OR could also be used to set a default value for function argument.
```js
function theSameOldFoo(name) {
name = name || 'Bar';
console.log("My best friend's name is " + name);
}
theSameOldFoo(); // My best friend's name is Bar
theSameOldFoo('Bhaskar'); // My best friend's name is Bhaskar
```
The logical AND could be used to avoid exceptions when using properties of undefined.
Example:
```js
var dog = {
bark: function () {
console.log('Woof Woof');
}
};
// Calling dog.bark();
dog.bark(); // Woof Woof.
// But if dog is not defined, dog.bark() will raise an error "Cannot read property 'bark' of undefined."
// To prevent this, we can use &&.
dog && dog.bark(); // This will only call dog.bark(), if dog is defined.
```
title: Currying vs partial application
tip-number: 28
tip-username: bhaskarmelkani
tip-username-profile: https://www.twitter.com/bhaskarmelkani
tip-tldr: Currying and partial application are two ways of transforming a function into another function with a generally smaller arity.
- /en/curry-vs-partial-application/
**Currying**
Currying takes a function
f: X \* Y -> R
and turns it into a function
f': X -> (Y -> R)
Instead of calling f with two arguments, we invoke f' with the first argument. The result is a function that we then call with the second argument to produce the result.
Thus, if the uncurried f is invoked as
f(3,5)
then the curried f' is invoked as
f(3)(5)
For example:
Uncurried add()
```javascript
function add(x, y) {
return x + y;
}
add(3, 5); // returns 8
```
Curried add()
```javascript
function addC(x) {
return function (y) {
return x + y;
};
}
addC(3)(5); // returns 8
```
**The algorithm for currying.**
Curry takes a binary function and returns a unary function that returns a unary function.
curry: (X × Y → R) → (X → (Y → R))
Javascript Code:
```javascript
function curry(f) {
return function (x) {
return function (y) {
return f(x, y);
};
};
}
```
**Partial application**
Partial application takes a function
f: X \* Y -> R
and a fixed value for the first argument to produce a new function
f`: Y -> R
f' does the same as f, but only has to fill in the second parameter which is why its arity is one less than the arity of f.
For example: Binding the first argument of function add to 5 produces the function plus5.
```javascript
function plus5(y) {
return 5 + y;
}
plus5(3); // returns 8
```
**The algorithm of partial application.\***
partApply takes a binary function and a value and produces a unary function.
partApply : ((X × Y → R) × X) → (Y → R)
Javascript Code:
```javascript
function partApply(f, x) {
return function (y) {
return f(x, y);
};
}
```
title: Speed up recursive functions with memoization
tip-number: 29
tip-username: hingsir
tip-username-profile: https://github.com/hingsir
tip-tldr: Fibonacci sequence is very familiar to everybody. we can write the following function in 20 seconds.it works, but not efficient. it did lots of duplicate computing works, we can cache its previously computed results to speed it up.
- /en/speed-up-recursive-functions-with-memoization/
Fibonacci sequence is very familiar to everybody. We can write the following function in 20 seconds.
```js
var fibonacci = function (n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
```
It works, but is not efficient. It did lots of duplicate computing works, we can cache its previously computed results to speed it up.
```js
var fibonacci = (function () {
var cache = [0, 1]; // cache the value at the n index
return function (n) {
if (cache[n] === undefined) {
for (var i = cache.length; i <= n; ++i) {
cache[i] = cache[i - 1] + cache[i - 2];
}
}
return cache[n];
};
})();
```
Also, we can define a higher-order function that accepts a function as its argument and returns a memoized version of the function.
```js
var memoize = function (func) {
var cache = {};
return function () {
var key = JSON.stringify(Array.prototype.slice.call(arguments));
return key in cache ? cache[key] : (cache[key] = func.apply(this, arguments));
};
};
fibonacci = memoize(fibonacci);
```
And this is an ES6 version of the memoize function.
```js
var memoize = function (func) {
const cache = {};
return (...args) => {
const key = JSON.stringify(args);
return key in cache ? cache[key] : (cache[key] = func(...args));
};
};
fibonacci = memoize(fibonacci);
```
we can use `memoize()` in many other situations
- GCD(Greatest Common Divisor)
```js
var gcd = memoize(function (a, b) {
var t;
if (a < b) (t = b), (b = a), (a = t);
while (b != 0) (t = b), (b = a % b), (a = t);
return a;
});
gcd(27, 183); //=> 3
```
- Factorial calculation
```js
var factorial = memoize(function (n) {
return n <= 1 ? 1 : n * factorial(n - 1);
});
factorial(5); //=> 120
```
Learn more about memoization:
- [Memoization - Wikipedia](https://en.wikipedia.org/wiki/Memoization)
- [Implementing Memoization in JavaScript](https://www.sitepoint.com/implementing-memoization-in-javascript/)
title: Converting truthy/falsy values to boolean
tip-number: 30
tip-username: hakhag
tip-username-profile: https://github.com/hakhag
tip-tldr: Logical operators are a core part of JavaScript, here you can see a a way you always get a true or false no matter what was given to it.
- /en/converting-truthy-falsy-values-to-boolean/
You can convert a [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) or [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) value to true boolean with the `!!` operator.
```js
!!''; // false
!!0; // false
!!null; // false
!!undefined; // false
!!NaN; // false
!!'hello'; // true
!!1; // true
!!{}; // true
!![]; // true
```
title: Avoid modifying or passing `arguments` into other functions — it kills optimization
tip-number: 31
tip-username: berkana
tip-username-profile: https://github.com/berkana
tip-tldr: Within JavaScript functions, the variable name `arguments` lets you access all of the arguments passed to the function. `arguments` is an _array-like object_; `arguments` can be accessed using array notation, and it has the _length_ property, but it doesn't have many of the built-in methods that arrays have such as `filter` and `map` and `forEach`. Because of this, it is a fairly common practice to convert `arguments` into an array using the following snipet
- /en/avoid-modifying-or-passing-arguments-into-other-functions-it-kills-optimization/
###Background
Within JavaScript functions, the variable name [`arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) lets you access all of the arguments passed to the function. `arguments` is an _array-like object_; `arguments` can be accessed using array notation, and it has the _length_ property, but it doesn't have many of the built-in methods that arrays have such as `filter` and `map` and `forEach`. Because of this, it is a fairly common practice to convert `arguments` into an array using the following:
```js
var args = Array.prototype.slice.call(arguments);
```
This calls the `slice` method from the `Array` prototype, passing it `arguments`; the `slice` method returns a shallow copy of `arguments` as a new array object. A common shorthand for this is :
```js
var args = [].slice.call(arguments);
```
In this case, instead of calling `slice` from the `Array` prototype, it is simply being called from an empty array literal.
###Optimization
Unfortunately, passing `arguments` into any function call will cause the V8 JavaScript engine used in Chrome and Node to skip optimization on the function that does this, which can result in considerably slower performance. See this article on [optimization killers](https://github.com/petkaantonov/bluebird/wiki/Optimization-killers). Passing `arguments` to any other function is known as _leaking `arguments`_.
Instead, if you want an array of the arguments that lets you use you need to resort to this:
```js
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
```
Yes it is more verbose, but in production code, it is worth it for the performance optimization.
title: Map() to the rescue; adding order to Object properties
tip-number: 32
tip-username: loverajoel
tip-username-profile: https://twitter.com/loverajoel
tip-tldr: An Object it is an unordered collection of properties... that means that if you are trying to save ordered data inside an Object, you have to review it because properties order in objects are not guaranteed.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/map-to-the-rescue-adding-order-to-object-properties/
## Object properties order
> An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method. [ECMAScript](http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf)
Take a look in action
```js
var myObject = {
z: 1,
'@': 2,
b: 3,
1: 4,
5: 5
};
console.log(myObject) // Object {1: 4, 5: 5, z: 1, @: 2, b: 3}
for (item in myObject) {...
// 1
// 5
// z
// @
// b
```
Each browser have his own rules about the order in objects bebause technically, order is unspecified.
## How to solve this?
### Map
Using a new ES6 feature called Map. A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object iterates its elements in insertion order — a `for...of` loop returns an array of [key, value] for each iteration.
```js
var myObject = new Map();
myObject.set('z', 1);
myObject.set('@', 2);
myObject.set('b', 3);
for (var [key, value] of myObject) {
console.log(key, value);
...
// z 1
// @ 2
// b 3
```
### Hack for old browsers
Mozilla suggest:
> So, if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.
```js
// Using two separate arrays
var objectKeys = [z, @, b, 1, 5];
for (item in objectKeys) {
myObject[item]
...
// Build an array of single-property objects
var myData = [{z: 1}, {'@': 2}, {b: 3}, {1: 4}, {5: 5}];
```
title: Create array sequence `[0, 1, ..., N-1]` in one line
tip-number: 33
tip-username: SarjuHansaliya
tip-username-profile: https://github.com/SarjuHansaliya
tip-tldr: Compact one-liners that generate ordinal sequence arrays
- /en/create-range-0...n-easily-using-one-line/
Here are two compact code sequences to generate the `N`-element array `[0, 1, ..., N-1]`:
### Solution 1 (requires ES5)
```js
Array.apply(null, { length: N }).map(Function.call, Number);
```
#### Brief explanation
1. `Array.apply(null, {length: N})` returns an `N`-element array filled with `undefined` (i.e. `A = [undefined, undefined, ...]`).
2. `A.map(Function.call, Number)` returns an `N`-element array, whose index `I` gets the result of `Function.call.call(Number, undefined, I, A)`
3. `Function.call.call(Number, undefined, I, A)` collapses into `Number(I)`, which is naturally `I`.
4. Result: `[0, 1, ..., N-1]`.
For a more thorough explanation, go [here](https://github.com/gromgit/jstips-xe/blob/master/tips/33.md).
### Solution 2 (requires ES6)
It uses `Array.from` [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
```js
Array.from(new Array(N), (val, index) => index);
```
### Solution 3 (requires ES6)
```js
Array.from(Array(N).keys());
```
#### Brief explanation
1. `A = new Array(N)` returns an array with `N` _holes_ (i.e. `A = [,,,...]`, but `A[x] = undefined` for `x` in `0...N-1`).
2. `F = (val,index)=>index` is simply `function F (val, index) { return index; }`
3. `Array.from(A, F)` returns an `N`-element array, whose index `I` gets the results of `F(A[I], I)`, which is simply `I`.
4. Result: `[0, 1, ..., N-1]`.
### One More Thing
If you actually want the sequence [1, 2, ..., N], **Solution 1** becomes:
```js
Array.apply(null, { length: N }).map(function (value, index) {
return index + 1;
});
```
and **Solution 2**:
```js
Array.from(new Array(N), (val, index) => index + 1);
```
title: Implementing asynchronous loop
tip-number: 34
tip-username: madmantalking
tip-username-profile: https://github.com/madmantalking
tip-tldr: You may run into problems while implementing asynchronous loops.
- /en/implementing-asynchronous-loops/
Let's try out writing an asynchronous function which prints the value of the loop index every second.
```js
for (var i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i);
}, 1000 * (i + 1));
}
```
The output of the above programs turns out to be
```js
> 5
> 5
> 5
> 5
> 5
```
So this definitely doesn't work.
**Reason**
Each timeout refers to the original `i`, not a copy. So the for loop increments `i` until it gets to 5, then the timeouts run and use the current value of `i` (which is 5).
Well , this problem seems easy. An immediate solution that strikes is to cache the loop index in a temporary variable.
```js
for (var i = 0; i < 5; i++) {
var temp = i;
setTimeout(function () {
console.log(temp);
}, 1000 * (i + 1));
}
```
But again the output of the above programs turns out to be
```js
> 4
> 4
> 4
> 4
> 4
```
So , that doesn't work either , because blocks don't create a scope and variables initializers are hoisted to the top of the scope. In fact, the previous block is the same as:
```js
var temp;
for (var i = 0; i < 5; i++) {
temp = i;
setTimeout(function () {
console.log(temp);
}, 1000 * (i + 1));
}
```
**Solution**
There are a few different ways to copy `i`. The most common way is creating a closure by declaring a function and passing `i` as an argument. Here we do this as a self-calling function.
```js
for (var i = 0; i < 5; i++) {
(function (num) {
setTimeout(function () {
console.log(num);
}, 1000 * (i + 1));
})(i);
}
```
In JavaScript, arguments are passed by value to a function. So primitive types like numbers, dates, and strings are basically copied. If you change them inside the function, it does not affect the outside scope. Objects are special: if the inside function changes a property, the change is reflected in all scopes.
Another approach for this would be with using `let`. With ES6 the `let` keyword is useful since it's block scoped unlike `var`
```js
for (let i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i);
}, 1000 * (i + 1));
}
```
title: Assignment Operators
tip-number: 35
tip-username: hsleonis
tip-username-profile: https://github.com/hsleonis
tip-tldr: Assigning is very common. Sometimes typing becomes time consuming for us 'Lazy programmers'. So, we can use some tricks to help us and make our code cleaner and simpler.
- /en/assignment-shorthands/
Assigning is very common. Sometimes typing becomes time consuming for us 'Lazy programmers'.
So, we can use some tricks to help us and make our code cleaner and simpler.
This is the similar use of
```javascript
x += 23; // x = x + 23;
y -= 15; // y = y - 15;
z *= 10; // z = z * 10;
k /= 7; // k = k / 7;
p %= 3; // p = p % 3;
d **= 2; // d = d ** 2;
m >>= 2; // m = m >> 2;
n <<= 2; // n = n << 2;
n++; // n = n + 1;
n--;
n = n - 1;
```
### `++` and `--` operators
There is a special `++` operator. It's best to explain it with an example:
```javascript
var a = 2;
var b = a++;
// Now a is 3 and b is 2
```
The `a++` statement does this:
1. return the value of `a`
2. increment `a` by 1
But what if we wanted to increment the value first? It's simple:
```javascript
var a = 2;
var b = ++a;
// Now both a and b are 3
```
See? I put the operator _before_ the variable.
The `--` operator is similar, except it decrements the value.
### If-else (Using ternary operator)
This is what we write on regular basis.
```javascript
var newValue;
if (value > 10) newValue = 5;
else newValue = 2;
```
We can user ternary operator to make it awesome:
```javascript
var newValue = value > 10 ? 5 : 2;
```
### Null, Undefined, Empty Checks
```javascript
if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
var variable2 = variable1;
}
```
Shorthand here:
```javascript
var variable2 = variable1 || '';
```
P.S.: If variable1 is a number, then first check if it is 0.
### Object Array Notation
Instead of using:
```javascript
var a = new Array();
a[0] = 'myString1';
a[1] = 'myString2';
```
Use this:
```javascript
var a = ['myString1', 'myString2'];
```
### Associative array
Instead of using:
```javascript
var skillSet = new Array();
skillSet['Document language'] = 'HTML5';
skillSet['Styling language'] = 'CSS3';
```
Use this:
```javascript
var skillSet = {
'Document language': 'HTML5',
'Styling language': 'CSS3'
};
```
title: Observe DOM changes in extensions
tip-number: 36
tip-username: beyondns
tip-username-profile: https://github.com/beyondns
tip-tldr: When you develop extensions to existent sites it's not so easy to play with DOM 'cause of modern dynamic javascript.
- /en/observe-dom-changes/
[MutationObserver](https://developer.mozilla.org/en/docs/Web/API/MutationObserver) is a solution to listen DOM changes and do what you want to do with elements when they changed. In following example there is some emulation of dynamic content loading with help of timers, after first "target" element creation goes "subTarget".
In extension code firstly rootObserver works till targetElement appearance then elementObserver starts. This cascading observing helps finally get moment when subTargetElement found.
This useful to develop extensions to complex sites with dynamic content loading.
```js
const observeConfig = {
attributes: true,
childList: true,
characterData: true,
subtree: true
};
function initExtension(rootElement, targetSelector, subTargetSelector) {
var rootObserver = new MutationObserver(function (mutations) {
console.log('Inside root observer');
targetElement = rootElement.querySelector(targetSelector);
if (targetElement) {
rootObserver.disconnect();
var elementObserver = new MutationObserver(function (mutations) {
console.log('Inside element observer');
subTargetElement = targetElement.querySelector(subTargetSelector);
if (subTargetElement) {
elementObserver.disconnect();
console.log('subTargetElement found!');
}
});
elementObserver.observe(targetElement, observeConfig);
}
});
rootObserver.observe(rootElement, observeConfig);
}
(function () {
initExtension(document.body, 'div.target', 'div.subtarget');
setTimeout(function () {
del = document.createElement('div');
del.innerHTML = "
target
";
document.body.appendChild(del);
}, 3000);
setTimeout(function () {
var el = document.body.querySelector('div.target');
if (el) {
del = document.createElement('div');
del.innerHTML = "
subtarget
";
el.appendChild(del);
}
}, 5000);
})();
```
title: Deduplicate an Array
tip-number: 37
tip-username: danillouz
tip-username-profile: https://www.twitter.com/danillouz
tip-tldr: How to remove duplicate elements, of different data types, from an Array.
- /en/deduplicate-an-array/
# Primitives
If an Array only contains primitive values, we can deduplicate it by
only using the [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) methods.
```javascript
var deduped = [1, 1, 'a', 'a'].filter(function (el, i, arr) {
return arr.indexOf(el) === i;
});
console.log(deduped); // [ 1, 'a' ]
```
## ES2015
We can write this in a more compact way using an [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions).
```javascript
var deduped = [1, 1, 'a', 'a'].filter((el, i, arr) => arr.indexOf(el) === i);
console.log(deduped); // [ 1, 'a' ]
```
But with the introduction of [Sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and the [`from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) method, we can achieve the same
result in a more concise way.
```javascript
var deduped = Array.from(new Set([1, 1, 'a', 'a']));
console.log(deduped); // [ 1, 'a' ]
```
# Objects
We can't use the same approach when the elements are Objects,
because Objects are stored by reference and primitives are stored
by value.
```javascript
1 === 1 // true
'a' === 'a' // true
{ a: 1 } === { a: 1 } // false
```
Therefore we need to change our approach and use a hash table.
```javascript
function dedup(arr) {
var hashTable = {};
return arr.filter(function (el) {
var key = JSON.stringify(el);
var match = Boolean(hashTable[key]);
return match ? false : (hashTable[key] = true);
});
}
var deduped = dedup([{ a: 1 }, { a: 1 }, [1, 2], [1, 2]]);
console.log(deduped); // [ {a: 1}, [1, 2] ]
```
Because a hash table in javascript is simply an `Object`, the keys
will always be of the type `String`. This means that normally we can't
distinguish between strings and numbers of the same value, i.e. `1` and
`'1'`.
```javascript
var hashTable = {};
hashTable[1] = true;
hashTable['1'] = true;
console.log(hashTable); // { '1': true }
```
However, because we're using [`JSON.stringify`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify), keys that are of the
type `String`, will be stored as an escaped string value, giving us unique
keys in our `hashTable`.
```javascript
var hashTable = {};
hashTable[JSON.stringify(1)] = true;
hashTable[JSON.stringify('1')] = true;
console.log(hashTable); // { '1': true, '\'1\'': true }
```
This means duplicate elements of the same value, but of a different type,
will still be deduplicated using the same implementation.
```javascript
var deduped = dedup([{ a: 1 }, { a: 1 }, [1, 2], [1, 2], 1, 1, '1', '1']);
console.log(deduped); // [ {a: 1}, [1, 2], 1, '1' ]
```
# Resources
## Methods
- [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
- [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
- [`from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
- [`JSON.stringify`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
## ES2015
- [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
- [Sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
## Stack overflow
- [remove duplicates from array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array/9229821#9229821)
title: Flattening multidimensional Arrays in JavaScript
tip-number: 38
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: Three different solutions to merge multidimensional array into a single array.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/flattening-multidimensional-arrays-in-javascript/
These are the three known ways to merge multidimensional array into a single array.
Given this array:
```js
var myArray = [
[1, 2],
[3, 4, 5],
[6, 7, 8, 9]
];
```
We wanna have this result:
```js
[1, 2, 3, 4, 5, 6, 7, 8, 9];
```
### Solution 1: Using [`concat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) and [`apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply)
```js
var myNewArray = [].concat.apply([], myArray);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
### Solution 2: Using [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Flatten_an_array_of_arrays)
```js
var myNewArray = myArray.reduce(function (prev, curr) {
return prev.concat(curr);
});
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
### Solution 3:
```js
var myNewArray3 = [];
for (var i = 0; i < myArray.length; ++i) {
for (var j = 0; j < myArray[i].length; ++j) myNewArray3.push(myArray[i][j]);
}
console.log(myNewArray3);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
### Solution 4: Using [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator) in ES6
```js
var myNewArray4 = [].concat(...myArray);
console.log(myNewArray4);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
### Solution 5: Using [`flat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) in ES10
```js
var myNewArray5 = myArray.flat();
console.log(myNewArray5);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
Take a look [here](https://jsbin.com/janana/edit?js,console) these 4 algorithms in action.
For infinitely nested array try Lodash [flattenDeep()](https://lodash.com/docs#flattenDeep).
If you are curious about performance, [here](http://jsperf.com/flatten-an-array-loop-vs-reduce/6) a test for check how it works.
title: Advanced Javascript Properties
tip-number: 39
tip-username: mallowigi
tip-username-profile: https://github.com/mallowigi
tip-tldr: How to add private properties, getters and setters to objects.
- /en/advanced-properties/
It is possible to configure object properties in Javascript for example to set properties to be pseudo-private or readonly. This feature is available since ECMAScript 5.1, therefore supported by all recent browsers.
To do so, you need to use the method `defineProperty` of the `Object` prototype like so:
```js
var a = {};
Object.defineProperty(a, 'readonly', {
value: 15,
writable: false
});
a.readonly = 20;
console.log(a.readonly); // 15
```
The syntax is as follows:
```js
Object.defineProperty(dest, propName, options);
```
or for multiple definitions:
```js
Object.defineProperties(dest, {
propA: optionsA,
propB: optionsB //...
});
```
where options include the following attributes:
- _value_: if the property is not a getter (see below), value is a mandatory attribute. `{a: 12}` === `Object.defineProperty(obj, 'a', {value: 12})`
- _writable_: set the property as readonly. Note that if the property is a nested objects, its properties are still editable.
- _enumerable_: set the property as hidden. That means that `for ... of` loops and `stringify` will not include the property in their result, but the property is still there. Note: That doesn't mean that the property is private! It can still be accessible from the outside, it just means that it won't be printed.
- _configurable_: set the property as non modifiable, e.g. protected from deletion or redefinition. Again, if the property is a nested object, its properties are still configurable.
So in order to create a private constant property, you can define it like so:
```js
Object.defineProperty(obj, 'myPrivateProp', { value: val, enumerable: false, writable: false, configurable: false });
```
Besides configuring properties, `defineProperty` allows us to define _dynamic properties_, thanks to the second parameter being a string. For instance, let's say that I want to create properties according to some external configuration:
```js
var obj = {
getTypeFromExternal(): true // illegal in ES5.1
}
Object.defineProperty(obj, getTypeFromExternal(), {value: true}); // ok
// For the example sake, ES6 introduced a new syntax:
var obj = {
[getTypeFromExternal()]: true
}
```
But that's not all! Advanced properties allows us to create **getters** and **setters**, just like other OOP languages! In that case, one cannot use the `writable`, `enumerable` and `configurable` properties, but instead:
```js
function Foobar () {
var _foo; // true private property
Object.defineProperty(obj, 'foo', {
get: function () { return _foo; }
set: function (value) { _foo = value }
});
}
var foobar = new Foobar();
foobar.foo; // 15
foobar.foo = 20; // _foo = 20
```
Aside for the obvious advantage of encapsulation and advanced accessors, you will notice that we didn't "call" the getter, instead we just "get" the property without parentheses! This is awesome! For instance, let's imagine that we have an object with long nested properties, like so:
```js
var obj = { a: { b: { c: [{ d: 10 }, { d: 20 }] } } };
```
Now instead of doing `a.b.c[0].d` (where one of the properties can resolve to `undefined` and throw an error), we can instead create an alias:
```js
Object.defineProperty(obj, 'firstD', {
get: function () {
return a && a.b && a.b.c && a.b.c[0] && a.b.c[0].d;
}
});
console.log(obj.firstD); // 10
```
### Note
If you define a getter without a setter and still try to set a value, you will get an error! This is particularly important when using helper functions such as `$.extend` or `_.merge`. Be careful!
### Links
- [defineProperty](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
- [Defining properties in JavaScript](http://bdadam.com/blog/defining-properties-in-javascript.html)
title: Using JSON.Stringify
tip-number: 40
tip-username: vamshisuram
tip-username-profile: https://github.com/vamshisuram
tip-tldr: Create string from selected properties of JSON object.
- /en/using-json-stringify/
Let's say there is an object with properties "prop1", "prop2", "prop3".
We can pass **additional params** to **JSON.stringify** to selectively write properties of the object to string like:
```javascript
var obj = {
prop1: 'value1',
prop2: 'value2',
prop3: 'value3'
};
var selectedProperties = ['prop1', 'prop2'];
var str = JSON.stringify(obj, selectedProperties);
// str
// {"prop1":"value1","prop2":"value2"}
```
The **"str"** will contain only info on selected properties only.
Instead of array we can pass a function also.
```javascript
function selectedProperties(key, val) {
// the first val will be the entire object, key is empty string
if (!key) {
return val;
}
if (key === 'prop1' || key === 'prop2') {
return val;
}
return;
}
```
The last optional param it takes is to modify the way it writes the object to string.
```javascript
var str = JSON.stringify(obj, selectedProperties, '\t\t');
/* str output with double tabs in every line.
{
"prop1": "value1",
"prop2": "value2"
}
*/
```
title: Array average and median
tip-number: 41
tip-username: soyuka
tip-username-profile: https://github.com/soyuka
tip-tldr: Calculate the average and median from array values
- /en/array-average-and-median/
The following examples will be based on the following array:
```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
```
To get the average, we have to sum up numbers and then divide by the number of values. Steps are:
- get the array length
- sum up values
- get the average (`sum/length`)
```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
let sum = values.reduce((previous, current) => (current += previous));
let avg = sum / values.length;
// avg = 28
```
Or:
```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
let count = values.length;
values = values.reduce((previous, current) => (current += previous));
values /= count;
// avg = 28
```
Now, to get the median steps are:
- sort the array
- get the arethmic mean of the middle values
```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let lowMiddle = Math.floor((values.length - 1) / 2);
let highMiddle = Math.ceil((values.length - 1) / 2);
let median = (values[lowMiddle] + values[highMiddle]) / 2;
// median = 13,5
```
With a bitwise operator:
```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let median = (values[(values.length - 1) >> 1] + values[values.length >> 1]) / 2;
// median = 13,5
```
title: Preventing Unapply Attacks
tip-number: 42
tip-username: emars
tip-username-profile: https://twitter.com/marseltov
tip-tldr: Freeze the builtin prototypes.
- /en/preventing-unapply-attacks/
By overriding the builtin prototypes, external code can cause code to break by rewriting code to expose and change bound arguments. This can be an issue that seriously breaks applications that works by using polyfill es5 methods.
```js
// example bind polyfill
function bind(fn) {
var prev = Array.prototype.slice.call(arguments, 1);
return function bound() {
var curr = Array.prototype.slice.call(arguments, 0);
var args = Array.prototype.concat.apply(prev, curr);
return fn.apply(null, args);
};
}
// unapply-attack
function unapplyAttack() {
var concat = Array.prototype.concat;
Array.prototype.concat = function replaceAll() {
Array.prototype.concat = concat; // restore the correct version
var curr = Array.prototype.slice.call(arguments, 0);
var result = concat.apply([], curr);
return result;
};
}
```
The above function discards the `prev` array from the bind meaning that any `.concat` the first concat call following using the unapply attack will throw an error.
By using [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze), making an object immutable, you prevent any overriding of the builtin object prototypes.
```js
(function freezePrototypes() {
if (typeof Object.freeze !== 'function') {
throw new Error('Missing Object.freeze');
}
Object.freeze(Object.prototype);
Object.freeze(Array.prototype);
Object.freeze(Function.prototype);
})();
```
You can read more about unapply attacks [here](https://glebbahmutov.com/blog/unapply-attack/).
Although this concept is called an 'unapply attack' due to some code being able to access closures that normally wouldn't be in scope, it is mostly wrong to consider this a security feature due to it not preventing an attacker with code execution from extending prototypes before the freezing happens and also still having the potential to read all scopes using various language features. ECMA modules would give realm based isolation which is much stronger than this solution however still doesn't fix the issues of third party scripts.
title: Use destructuring in function parameters
tip-number: 43
tip-username: dislick
tip-username-profile: https://github.com/dislick
tip-tldr: Did you know that you can use destructuring in function parameters?
- /en/use-destructuring-in-function-parameters/
I am sure many of you are already familiar with the [ES6 Destructuring Assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). Did you know that you can also use it in function parameters?
```js
var sayHello = function ({ name, surname }) {
console.log(`Hello ${name} ${surname}! How are you?`);
};
sayHello({ name: 'John', surname: 'Smith' });
// -> Hello John Smith! How are you?
```
This is great for functions which accept an options object. For this use case, you can also add [default parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) to fill in whatever values the caller leaves out, or if the caller forgets to pass one at all:
```js
var sayHello2 = function ({ name = 'Anony', surname = 'Moose' } = {}) {
console.log(`Hello ${name} ${surname}! How are you?`);
};
```
The `= {}` says that the default object to be destructured for this parameter is `{}`, in case the caller forgets to pass the parameter, or passes one of the wrong type (more on this below).
```js
sayHello2();
// -> Hello Anony Moose! How are you?
sayHello2({ name: 'Bull' });
// -> Hello Bull Moose! How are you?
```
##### Argument Handling
With plain destructuring assignment, if the the input parameter can't be matched with the function's specified object arguments, all the unmatched arguments are `undefined`, so you need to add code that handles this properly:
```js
var sayHelloTimes = function ({ name, surname }, times) {
console.log(`Hello ${name} ${surname}! I've seen you ${times} times before.`);
};
sayHelloTimes({ name: 'Pam' }, 5678);
// -> Hello Pam undefined! I've seen you 5678 times before.
sayHelloTimes(5678);
// -> Hello undefined undefined! I've seen you undefined times before.
```
Worse, if the parameter to be destructured is missing, an exception is thrown, probably bringing your app to a screeching halt:
```js
sayHelloTimes();
// -> Uncaught TypeError: Cannot match against 'undefined' or 'null'...
```
It's conceptually similar to accessing a property of an undefined object, just with a different exception type.
Destructuring assignment with default parameters hides all the above to a certain extent:
```js
var sayHelloTimes2 = function ({ name = 'Anony', surname = 'Moose' } = {}, times) {
console.log(`Hello ${name} ${surname}! I've seen you ${times} times before.`);
};
sayHelloTimes2({ name: 'Pam' }, 5678);
// -> Hello Pam Moose! I've seen you 5678 times before.
sayHelloTimes2(5678);
// -> Hello Anony Moose! I've seen you undefined times before.
sayHelloTimes2();
// -> Hello Anony Moose! I've seen you undefined times before.
```
As for `= {}`, it covers the case of a missing _object_, for which individual property defaults won't help at all:
```js
var sayHelloTimes2a = function ({ name = 'Anony', surname = 'Moose' }, times) {
console.log(`Hello ${name} ${surname}! I've seen you ${times} times before.`);
};
sayHelloTimes2a({ name: 'Pam' }, 5678);
// -> Hello Pam Moose! I've seen you 5678 times before.
sayHelloTimes2a(5678);
// -> Hello Anony Moose! I've seen you undefined times before.
sayHelloTimes2a();
// -> Uncaught TypeError: Cannot match against 'undefined' or 'null'.
```
##### Availability
Note that destructuring assignment may not yet be available by default, in the version of Node.js or browser that you're using. For Node.js, you can try using the `--harmony-destructuring` flag on startup to activate this feature.
title: Know the passing mechanism
tip-number: 44
tip-username: bmkmanoj
tip-username-profile: https://github.com/bmkmanoj
tip-tldr: JavaScript technically only passes by value for both primitives and object (or reference) types. In case of reference types the reference value itself is passed by value.
- /en/know-the-passing-mechanism/
JavaScript is pass-by-value, technically. It is neither pass-by-value nor pass-by-reference, going by the truest sense of these terms. To understand this passing mechanism, take a look at the following two example code snippets and the explanations.
### Example 1
```js
var me = {
// 1
partOf: 'A Team'
};
function myTeam(me) {
// 2
me = {
// 3
belongsTo: 'A Group'
};
}
myTeam(me);
console.log(me); // 4 : {'partOf' : 'A Team'}
```
In above example, when the `myTeam` gets invoked, JavaScript is _passing the reference to_ `me` _object as value, as it is an object_ and invocation itself creates two independent references to the same object, (though the name being same here i.e. `me`, is misleading and gives us an impression that it is the single reference) and hence, the reference variable themselves are independent.
When we assigned a new object at #`3`, we are changing this reference value entirely within the `myTeam` function, and it will not have any impact on the original object outside this function scope, from where it was passed and the reference in the outside scope is going to retain the original object and hence the output from #`4`.
### Example 2
```js
var me = {
// 1
partOf: 'A Team'
};
function myGroup(me) {
// 2
me.partOf = 'A Group'; // 3
}
myGroup(me);
console.log(me); // 4 : {'partOf' : 'A Group'}
```
In the case of `myGroup` invocation, we are passing the object `me`. But unlike the example 1 scenario, we are not assigning this `me` variable to any new object, effectively meaning the object reference value within the `myGroup` function scope still is the original object's reference value and when we are modifying the property within this scope, it is effectively modifying the original object's property. Hence, you get the output from #`4`.
So does this later case not prove that javascript is pass-by-reference? No, it does not. Remember, _JavaScript passes the reference as value, in case of objects_. The confusion arises as we tend not to understand fully what pass by reference is. This is the exact reason, some prefer to call this as _call-by-sharing_.
_Initially posted by the author on [js-by-examples](https://github.com/bmkmanoj/js-by-examples/blob/master/examples/js_pass_by_value_or_reference.md)_
title: Calculate the Max/Min value from an array
tip-number: 45
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: Ways to use the built-in functions Math.max() and Math.min() with arrays of numbers
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/calculate-the-max-min-value-from-an-array/
The built-in functions [Math.max()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) and [Math.min()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min) find the maximum and minimum value of the arguments, respectively.
```js
Math.max(1, 2, 3, 4); // 4
Math.min(1, 2, 3, 4); // 1
```
These functions will not work as-is with arrays of numbers. However, there are some ways around this.
[`Function.prototype.apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) allows you to call a function with a given `this` value and an _array_ of arguments.
```js
var numbers = [1, 2, 3, 4];
Math.max.apply(null, numbers); // 4
Math.min.apply(null, numbers); // 1
```
Passing the `numbers` array as the second argument of `apply()` results in the function being called with all values in the array as parameters.
A simpler, ES2015 way of accomplishing this is with the new [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
```js
var numbers = [1, 2, 3, 4];
Math.max(...numbers); // 4
Math.min(...numbers); // 1
```
This operator causes the values in the array to be expanded, or "spread", into the function's arguments.
title: Detect document ready in pure JS
tip-number: 46
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: The cross-browser way to check if the document has loaded in pure JavaScript
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/detect-document-ready-in-pure-js/
The cross-browser way to check if the document has loaded in pure JavaScript is using [`readyState`](https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState).
```js
if (document.readyState === 'complete') {
// The page is fully loaded
}
```
You can detect when the document is ready...
```js
let stateCheck = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(stateCheck);
// document ready
}
}, 100);
```
or with [onreadystatechange](https://developer.mozilla.org/en-US/docs/Web/Events/readystatechange)...
```js
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
// document ready
}
};
```
Use `document.readyState === 'interactive'` to detect when the DOM is ready.
title: Basics declarations
tip-number: 47
tip-username: adaniloff
tip-username-profile: https://github.com/adaniloff
tip-tldr: Understand and work with declarations.
- /en/basics-declarations/
Below, different ways to declare variables in JavaScript.
Comments and console.log should be enough to explain what's happening here:
```js
var y,
x = (y = 1); //== var x; var y; x = y = 1
console.log('--> 1:', `x = ${x}, y = ${y}`);
// Will print
//--> 1: x = 1, y = 1
```
First, we just set two variables. Nothing much here.
```js
(() => {
var x = (y = 2); // == var x; x = y = 2;
console.log('2.0:', `x = ${x}, y = ${y}`);
})();
console.log('--> 2.1:', `x = ${x}, y = ${y}`);
// Will print
//2.0: x = 2, y = 2
//--> 2.1: x = 1, y = 2
```
As you can see, the code has only changed the global y, as we haven't declared the variable in the closure.
```js
(() => {
var x,
y = 3; // == var x; var y = 3;
console.log('3.0:', `x = ${x}, y = ${y}`);
})();
console.log('--> 3.1:', `x = ${x}, y = ${y}`);
// Will print
//3.0: x = undefined, y = 3
//--> 3.1: x = 1, y = 2
```
Now we declare both variables through var. Meaning they only live in the context of the closure.
```js
(() => {
var y,
x = (y = 4); // == var x; var y; x = y = 4
console.log('4.0:', `x = ${x}, y = ${y}`);
})();
console.log('--> 4.1:', `x = ${x}, y = ${y}`);
// Will print
//4.0: x = 4, y = 4
//--> 4.1: x = 1, y = 2
```
Both variables have been declared using var and only after that we've set their values. As local > global, x and y are local in the closure, meaning the global x and y are untouched.
```js
x = 5; // == x = 5
console.log('--> 5:', `x = ${x}, y = ${y}`);
// Will print
//--> 5: x = 5, y = 2
```
This last line is explicit by itself.
You can test this and see the result [thanks to babel]().
More informations available on the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var).
Special thanks to @kurtextrem for his collaboration :)!
title: How to `reduce()` arrays
tip-number: 48
tip-username: darul75
tip-username-profile: https://twitter.com/darul75
tip-tldr: Some reminders about using `reduce()`
- /en/reminders-about-reduce-function-usage/
As written in documentation the `reduce()` method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
### Signature
[reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) function accepts 2 parameters (M: mandatory, O: optional):
- (M) a callback **reducer function** to be applied that deals with a pair of previous (result of previous computation) and next element until end of the list.
- (O) an **initial value** to be used as the first argument to the first call of the callback.
So let's see a common usage and later a more sophisticated one.
### Common usage (accumulation, concatenation)
We are on Amazon website (prices in $) and our caddy is quite full, let's compute total.
```javascript
// my current amazon caddy purchases
var items = [{ price: 10 }, { price: 120 }, { price: 1000 }];
// our reducer function
var reducer = function add(sumSoFar, item) {
return sumSoFar + item.price;
};
// do the job
var total = items.reduce(reducer, 0);
console.log(total); // 1130
```
Optional reduce function parameter was primitive integer type 0 in that first case, but it could have been an Object, an Array...instead of a primitive type,
but we will see that later.
Now, cool I received a discount coupon of 20$.
```javascript
var total = items.reduce(reducer, -20);
console.log(total); // 1110
```
### Advanced usage (combination)
This second usage example is inspired by Redux [combineReducers](http://redux.js.org/docs/api/combineReducers.html) function [source](https://github.com/reactjs/redux/blob/master/src/combineReducers.js#L93).
Idea behind is to separate reducer function into separate individual functions and at the end compute a new _single big reducer function_.
To illustrate this, let's create a single object literal with some reducers function able to compute total prices in different currency $, €...
```javascript
var reducers = {
totalInDollar: function (state, item) {
// specific statements...
return (state.dollars += item.price);
},
totalInEuros: function (state, item) {
return (state.euros += item.price * 0.897424392);
},
totalInPounds: function (state, item) {
return (state.pounds += item.price * 0.692688671);
},
totalInYen: function (state, item) {
return (state.yens += item.price * 113.852);
}
// more...
};
```
Then, we create a new swiss knife function
- responsible for applying each partial reduce functions.
- that will return a new callback reducer function
```javascript
var combineTotalPriceReducers = function (reducers) {
return function (state, item) {
return Object.keys(reducers).reduce(function (nextState, key) {
reducers[key](state, item);
return state;
}, {});
};
};
```
Now let's see how using it.
```javascript
var bigTotalPriceReducer = combineTotalPriceReducers(reducers);
var initialState = { dollars: 0, euros: 0, yens: 0, pounds: 0 };
var totals = items.reduce(bigTotalPriceReducer, initialState);
console.log(totals);
/*
Object {dollars: 1130, euros: 1015.11531904, yens: 127524.24, pounds: 785.81131152}
*/
```
I hope this approach can give you another idea of using reduce() function for your own needs.
Your reduce function could handle an history of each computation by instance as it is done in Ramdajs with [scan](http://ramdajs.com/docs/#scan) function
[JSFiddle to play with](https://jsfiddle.net/darul75/81tgt0cd/)
title: Easiest way to extract unix timestamp in JS
tip-number: 49
tip-username: nmrony
tip-username-profile: https://github.com/nmrony
tip-tldr: In Javascript you can easily get the unix timestamp
- /en/extract-unix-timestamp-easily/
We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is
```js
const dateTime = Date.now();
const timestamp = Math.floor(dateTime / 1000);
```
or
```js
const dateTime = new Date().getTime();
const timestamp = Math.floor(dateTime / 1000);
```
To get unix timestamp of a specific date pass `YYYY-MM-DD` or `YYYY-MM-DDT00:00:00Z` as parameter of `Date` constructor. For example
```js
const dateTime = new Date('2012-06-08').getTime();
const timestamp = Math.floor(dateTime / 1000);
```
You can just add a `+` sign also when declaring a `Date` object like below
```js
const dateTime = +new Date();
const timestamp = Math.floor(dateTime / 1000);
```
or for specific date
```js
const dateTime = +new Date('2012-06-08');
const timestamp = Math.floor(dateTime / 1000);
```
Under the hood the runtime calls `valueOf` method of the `Date` object. Then the unary `+` operator calls `toNumber()` with that returned value. For detailed explanation please check the following links
- [Date.prototype.valueOf](http://es5.github.io/#x15.9.5.8)
- [Unary + operator](http://es5.github.io/#x11.4.6)
- [toNumber()](http://es5.github.io/#x9.3)
- [Date Javascript MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
- [Date.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)
title: Helpful Console Logging Tricks
tip-number: 50
tip-username: zackhall
tip-username-profile: https://twitter.com/zthall
tip-tldr: Helpful logging techniques using coercion and conditonal breakpoints.
- /en/helpful-console-log-hacks/
## Using conditional breakpoints to log data
If you wanted to log to the console a value each time a function is called, you can use conditional break points to do this. Open up your dev tools, find the function where you'd like to log data to the console and set a breakpoint with the following condition:
```js
console.log(data.value) && false;
```
A conditional breakpoint pauses the page thread only if the condition for the breakpoint evaluates to true. So by using a condition like console.log('foo') && false it's guaranteed to evaluate to false since you're putting the literal false in the AND condition. So this will not pause the page thread when it's hit, but it will log data to the console. This can also be used to count how many times a function or callback is called.
Here's how you can set a conditional breakpoint in [Edge](https://dev.windows.com/en-us/microsoft-edge/platform/documentation/f12-devtools-guide/debugger/#setting-and-managing-breakpoints 'Managing Breakpoints in Edge'), [Chrome](https://developer.chrome.com/devtools/docs/javascript-debugging#breakpoints 'Managing Breakpoints in Chrome'), [Firefox](https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Set_a_conditional_breakpoint 'Managing Breakpoints in Firefox') and [Safari](https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Debugger/Debugger.html 'Managing Breakpoints in Safari').
## Printing a function variable to console
Have you ever logged a function variable to the console and weren't able to just view the function's code? The quickest way to see the function's code is to coerce it to a string using concatenation with an empty string.
```js
console.log(funcVariable + '');
```
title: DOM event listening made easy
tip-number: 51
tip-username: octopitus
tip-username-profile: https://github.com/octopitus
tip-tldr: An elegant and easy way to handle DOM events
- /en/DOM-event-listening-made-easy/
Many of us are still doing these things:
- `element.addEventListener('type', obj.method.bind(obj))`
- `element.addEventListener('type', function (event) {})`
- `element.addEventListener('type', (event) => {})`
The above examples all create new anonymous event handlers that can't be removed when no longer needed. This may cause performance problems or unexpected logic bugs, when handlers that you no longer need still get accidentally triggered through unexpected user interactions or [event bubbling](http://www.javascripter.net/faq/eventbubbling.htm).
Safer event-handling patterns include the following:
Use a reference:
```js
const handler = function () {
console.log('Tada!');
};
element.addEventListener('click', handler);
// Later on
element.removeEventListener('click', handler);
```
Named function that removes itself:
```js
element.addEventListener('click', function click(e) {
if (someCondition) {
return e.currentTarget.removeEventListener('click', click);
}
});
```
A better approach:
```js
function handleEvent(eventName, { onElement, withCallback, useCapture = false } = {}, thisArg) {
const element = onElement || document.documentElement;
function handler(event) {
if (typeof withCallback === 'function') {
withCallback.call(thisArg, event);
}
}
handler.destroy = function () {
return element.removeEventListener(eventName, handler, useCapture);
};
element.addEventListener(eventName, handler, useCapture);
return handler;
}
// Anytime you need
const handleClick = handleEvent('click', {
onElement: element,
withCallback: (event) => {
console.log('Tada!');
}
});
// And anytime you want to remove it
handleClick.destroy();
```
title: Return Values with the 'new' Operator
tip-number: 52
tip-username: Morklympious
tip-username-profile: https://github.com/morklympious
tip-tldr: Understand what gets returned when using new vs. not using new.
- /en/return-values-with-the-new-operator/
You're going to run into some instances where you'll be using `new` to allocate new objects in JavaScript. It's going to blow your mind unless you read this tip to understand what's happening behind the scenes.
The `new` operator in JavaScript is an operator that, under reasonable circumstances, returns a new instance of an object. Let's say we have a constructor function:
```js
function Thing() {
this.one = 1;
this.two = 2;
}
var myThing = new Thing();
myThing.one; // 1
myThing.two; // 2
```
**Note**: `this` refers to the new object created by `new`. Otherwise if `Thing()` is called without `new`, **no object is created**, and `this` is going to point to the global object, which is `window`. This means that:
1. You'll suddenly have two new global variables named `one` and `two`.
2. `myThing` is now undefined, since nothing is returned in `Thing()`.
Now that you get that example, here's where things get a little bit wonky. Let's say I add something to the constructor function, a little SPICE:
```js
function Thing() {
this.one = 1;
this.two = 2;
return 5;
}
var myThing = new Thing();
```
Now, what does myThing equal? Is it 5? is it an object? Is it my crippled sense of self-worth? The world may never know!
Except the world does know:
```js
myThing.one; // 1
myThing.two; // 2
```
Interestingly enough, we never actually see the five that we supposedly 'returned' from our constructor. That's weird, isn't it? What are you doing function? WHERE'S THE FIVE? Let's try it with something else.
Let's return a non-primitive type instead, something like an object.
```js
function Thing() {
this.one = 1;
this.two = 2;
return {
three: 3,
four: 4
};
}
var myThing = new Thing();
```
Let's check it out. A quick console.log reveals all:
```js
console.log(myThing);
/*
Object {three: 3, four: 4}
What happened to this.one and this.two!?
They've been stomped, my friend.
*/
```
**Here's where we learn:** When you invoke a function with the `new` keyword, you can set properties on it using the keyword `this` (but you probably already knew that). Returning a primitive value from a function you called with the `new` keyword will not return the value you specified, but instead will return the `this` instance of the function (the one you put properties on, like `this.one = 1;`).
However, returning a non-primitive, like an `object`, `array`, or `function` will stomp on the `this` instance, and return that non-primitive instead, effectively ruining all the hard work you did assigning everything to `this`.
title: Get File Extension
tip-number: 53
tip-username: richzw
tip-username-profile: https://github.com/richzw
tip-tldr: How to get the file extension more efficiently?
- /en/get-file-extension/
categories: - en - javascript
---
### Question: How to get the file extension?
```javascript
var file1 = '50.xsl';
var file2 = '30.doc';
getFileExtension(file1); //returs xsl
getFileExtension(file2); //returs doc
function getFileExtension(filename) {
/*TODO*/
}
```
### Solution 1: Regular Expression
```js
function getFileExtension1(filename) {
return /[.]/.exec(filename) ? /[^.]+$/.exec(filename)[0] : undefined;
}
```
### Solution 2: String `split` method
```js
function getFileExtension2(filename) {
return filename.split('.').pop();
}
```
Those two solutions couldnot handle some edge cases, here is another more robust solution.
### Solution3: String `slice`, `lastIndexOf` methods
```js
function getFileExtension3(filename) {
return filename.slice(((filename.lastIndexOf('.') - 1) >>> 0) + 2);
}
console.log(getFileExtension3('')); // ''
console.log(getFileExtension3('filename')); // ''
console.log(getFileExtension3('filename.txt')); // 'txt'
console.log(getFileExtension3('.hiddenfile')); // ''
console.log(getFileExtension3('filename.with.many.dots.ext')); // 'ext'
```
_How does it works?_
- [String.lastIndexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) method returns the last occurrence of the specified value (`'.'` in this case). Returns `-1` if the value is not found.
- The return values of `lastIndexOf` for parameter `'filename'` and `'.hiddenfile'` are `-1` and `0` respectively. [Zero-fill right shift operator (>>>)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#%3E%3E%3E_%28Zero-fill_right_shift%29) will transform `-1` to `4294967295` and `-2` to `4294967294`, here is one trick to insure the filename unchanged in those edge cases.
- [String.prototype.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) extracts file extension from the index that was calculated above. If the index is more than the length of the filename, the result is `""`.
### Comparison
| Solution | Paramters | Results |
| ----------------------------------------- | :-----------------------------------------------------------------------------------------: | :-------------------------------------------------------------------: |
| Solution 1: Regular Expression | '' 'filename' 'filename.txt' '.hiddenfile' 'filename.with.many.dots.ext' | undefined undefined 'txt' 'hiddenfile' 'ext' |
| Solution 2: String `split` | '' 'filename' 'filename.txt' '.hiddenfile' 'filename.with.many.dots.ext' | '' 'filename' 'txt' 'hiddenfile' 'ext' |
| Solution 3: String `slice`, `lastIndexOf` | '' 'filename' 'filename.txt' '.hiddenfile' 'filename.with.many.dots.ext' | '' '' 'txt' '' 'ext' |
### Live Demo and Performance
[Here](https://jsbin.com/tipofu/edit?js,console) is the live demo of the above codes.
[Here](http://jsperf.com/extract-file-extension) is the performance test of those 3 solutions.
### Source
[How can I get file extensions with JavaScript](http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript)
title: How to use optional arguments in functions (with optional callback)
tip-number: 54
tip-username: alphashuro
tip-username-profile: https://github.com/alphashuro
tip-tldr: You can make function arguments and callback optional
- /en/use-optional-arguments/
categories: - en - javascript
---
Example function where arguments 2 and 3 are optional
```javascript
function example(err, optionalA, optionalB, callback) {
// retrieve arguments as array
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
// first argument is the error object
// shift() removes the first item from the
// array and returns it
err = args.shift();
// if last argument is a function then its the callback function.
// pop() removes the last item in the array
// and returns it
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
}
// if args still holds items, these are
// your optional items which you could
// retrieve one by one like this:
if (args.length > 0) optionalA = args.shift();
else optionalA = null;
if (args.length > 0) optionalB = args.shift();
else optionalB = null;
// continue as usual: check for errors
if (err) {
return callback && callback(err);
}
// for tutorial purposes, log the optional parameters
console.log('optionalA:', optionalA);
console.log('optionalB:', optionalB);
console.log('callback:', callback);
/* do your thing */
}
// ES6 with shorter, more terse code
function example(...args) {
// first argument is the error object
const err = args.shift();
// if last argument is a function then its the callback function
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : null;
// if args still holds items, these are your optional items which you could retrieve one by one like this:
const optionalA = args.length > 0 ? args.shift() : null;
const optionalB = args.length > 0 ? args.shift() : null;
// ... repeat for more items
if (err && callback) return callback(err);
/* do your thing */
}
// invoke example function with and without optional arguments
example(null, 'AA');
example(null, function (err) {
/* do something */
});
example(null, 'AA', function (err) {});
example(null, 'AAAA', 'BBBB', function (err) {});
```
### How do you determine if optionalA or optionalB is intended?
Design your function to require optionalA in order to accept optionalB
title: Create an easy loop using an array
tip-number: 55
tip-username: jamet-julien
tip-username-profile: https://github.com/jamet-julien
tip-tldr: Sometimes, we need to loop endlessly over an array of items, like a carousel of images or an audio playlist. Here's how to take an array and give it "looping powers”
- /en/make-easy-loop-on-array/
categories: - en - javascript
---
Sometimes, we need to loop endlessly over an array of items, like a carousel of images or an audio playlist. Here's how to take an array and give it "looping powers":
```js
var aList = ['A', 'B', 'C', 'D', 'E'];
function make_looper(arr) {
arr.loop_idx = 0;
// return current item
arr.current = function () {
if (this.loop_idx < 0) {
// First verification
this.loop_idx = this.length - 1; // update loop_idx
}
if (this.loop_idx >= this.length) {
// second verification
this.loop_idx = 0; // update loop_idx
}
return arr[this.loop_idx]; //return item
};
// increment loop_idx AND return new current
arr.next = function () {
this.loop_idx++;
return this.current();
};
// decrement loop_idx AND return new current
arr.prev = function () {
this.loop_idx--;
return this.current();
};
}
make_looper(aList);
aList.current(); // -> A
aList.next(); // -> B
aList.next(); // -> C
aList.next(); // -> D
aList.next(); // -> E
aList.next(); // -> A
aList.pop(); // -> E
aList.prev(); // -> D
aList.prev(); // -> C
aList.prev(); // -> B
aList.prev(); // -> A
aList.prev(); // -> D
```
Using the `%` ( Modulus ) operator is prettier.The modulus return division's rest ( ` 2 % 5 = 1` and ` 5 % 5 = 0`):
```js
var aList = ['A', 'B', 'C', 'D', 'E'];
function make_looper(arr) {
arr.loop_idx = 0;
// return current item
arr.current = function () {
this.loop_idx = this.loop_idx % this.length; // no verification !!
return arr[this.loop_idx];
};
// increment loop_idx AND return new current
arr.next = function () {
this.loop_idx++;
return this.current();
};
// decrement loop_idx AND return new current
arr.prev = function () {
this.loop_idx += this.length - 1;
return this.current();
};
}
make_looper(aList);
aList.current(); // -> A
aList.next(); // -> B
aList.next(); // -> C
aList.next(); // -> D
aList.next(); // -> E
aList.next(); // -> A
aList.pop(); // -> E
aList.prev(); // -> D
aList.prev(); // -> C
aList.prev(); // -> B
aList.prev(); // -> A
aList.prev(); // -> D
```
title: Copy to Clipboard
tip-number: 56
tip-username: loverajoel
tip-username-profile: https://twitter.com/loverajoel
tip-tldr: This week I had to create a common "Copy to Clipboard" button, I've never created one before and I want to share how I made it.
tip-writer-support: https://www.coinbase.com/loverajoel
- /en/copy-to-clipboard/
categories: - en - javascript
---
This is a simple tip, this week I had to create a common "Copy to Clipboard" button, I've never created one before and I want to share how I made it.
It's easy, the bad thing is that we must add an `` with the text to be copied to the DOM. Then, we selected the content and execute the copy command with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand).
`execCommand('copy')` will copy the actual selected content.
Also, this command that now is [supported](http://caniuse.com/#search=execCommand) by all the latest version of browsers, allows us to execute another system commands like `copy`, `cut`, `paste`, and make changes like fonts color, size, and much more.
```js
document.querySelector('#input').select();
document.execCommand('copy');
```
##### Playground
title: Comma operator in JS
tip-number: 57
tip-username: bhaskarmelkani
tip-username-profile: https://www.twitter.com/bhaskarmelkani
tip-tldr: When placed in an expression, it evaluates every expression from left to right and returns the last one.
- /en/comma-operaton-in-js/
categories: - en - javascript
---
Apart from being just a delimiter, the comma operator allows you to put multiple statements in a place where one statement is expected.
Eg:-
```js
for (var i = 0, j = 0; i < 5; i++, j++, j++) {
console.log('i:' + i + ', j:' + j);
}
```
Output:-
```js
i:0, j:0
i:1, j:2
i:2, j:4
i:3, j:6
i:4, j:8
```
When placed in an expression, it evaluates every expression from left to right and returns the right most expression.
Eg:-
```js
function a() {
console.log('a');
return 'a';
}
function b() {
console.log('b');
return 'b';
}
function c() {
console.log('c');
return 'c';
}
var x = (a(), b(), c());
console.log(x); // Outputs "c"
```
Output:-
```js
'a';
'b';
'c';
'c';
```
- Note: The comma(`,`) operator has the lowest priority of all javascript operators, so without the parenthesis the expression would become: `(x = a()), b(), c();`.
##### Playground
title: Breaking or continuing loop in functional programming
tip-number: 58
tip-username: vamshisuram
tip-username-profile: https://github.com/vamshisuram
tip-tldr: A common task for us is iterate over a list looking for a value or values, but we can't return from inside a loop so we will have to iterate the whole array, even if the item we search is the first in the list, in this tip we will see how to short circuit with `.some` and `.every`.
- /en/break-continue-loop-functional/
categories: - en - javascript
---
A common requirement of iteration is cancelation. Using `for` loops we can `break` to end iteration early.
```javascript
const a = [0, 1, 2, 3, 4];
for (var i = 0; i < a.length; i++) {
if (a[i] === 2) {
break; // stop the loop
}
console.log(a[i]);
}
//> 0, 1
```
Another common requirement is to close over our variables.
A quick approach is to use `.forEach` but
then we lack the ability to `break`. In this situation the closest we get is `continue` functionality through `return`.
```javascript
[0, 1, 2, 3, 4].forEach(function (val, i) {
if (val === 2) {
// how do we stop?
return true;
}
console.log(val); // your code
});
//> 0, 1, 3, 4
```
The `.some` is a method on Array prototype. It tests whether some element in the array passes the test implemented by the provided function. If any value is returning true, then it stops executing. Here is a [MDN link](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some) for more details.
An example quoted from that link
```javascript
const isBiggerThan10 = (numb) => numb > 10;
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
```
Using `.some` we get iteration functionally similar to `.forEach` but with the ability to `break` through `return` instead.
```javascript
[0, 1, 2, 3, 4].some(function (val, i) {
if (val === 2) {
return true;
}
console.log(val); // your code
});
//> 0, 1
```
You keep returning `false` to make it `continue` to next item. When you return `true`, the loop will `break` and `a.some(..)` will `return` `true`.
```javascript
// Array contains 2
const isTwoPresent = [0, 1, 2, 3, 4].some(function (val, i) {
if (val === 2) {
return true; // break
}
});
console.log(isTwoPresent);
//> true
```
Also there is `.every`, which can be used. We have to return the opposite boolean compared to `.some`.
##### Playground
title: ES6, var vs let
tip-number: 59
tip-username: richzw
tip-username-profile: https://github.com/richzw
tip-tldr: In this tip, I will introduce the block-scope difference between keyword var and let. Should I replace var by let? let's take a look
- /en/keyword-var-vs-let/
categories: - en - javascript
---
### Overview
- The scope of a variable defined with `var` is function scope or declared outside any function, global.
- The scope of a variable defined with `let` is block scope.
```js
function varvslet() {
console.log(i); // i is undefined due to hoisting
// console.log(j); // ReferenceError: j is not defined
for (var i = 0; i < 3; i++) {
console.log(i); // 0, 1, 2
}
console.log(i); // 3
// console.log(j); // ReferenceError: j is not defined
for (let j = 0; j < 3; j++) {
console.log(j);
}
console.log(i); // 3
// console.log(j); // ReferenceError: j is not defined
}
```
### Difference Details
- Variable Hoisting
`let` will not hoist to the entire scope of the block they appear in. By contrast, `var` could hoist as below.
```js
{
console.log(c); // undefined. Due to hoisting
var c = 2;
}
{
console.log(b); // ReferenceError: b is not defined
let b = 3;
}
```
- Closure in Loop
`let` in the loop can re-binds it to each iteration of the loop, making sure to re-assign it the value from the end of the previous loop iteration, so it can be used to avoid issue with closures.
```js
for (var i = 0; i < 5; ++i) {
setTimeout(function () {
console.log(i); // output '5' 5 times
}, 100);
}
```
After replacing `var` with `let`
```js
// print 1, 2, 3, 4, 5
for (let i = 0; i < 5; ++i) {
setTimeout(function () {
console.log(i); // output 0, 1, 2, 3, 4
}, 100);
}
```
### Should I replace `var` with `let`?
> NO, `let` is the new block scoping `var`. That statement emphasizes that `let` should replace `var` only when `var` was already signaling
> block scoping stylistically. Otherwise, leave `var` alone. `let` improves scoping options in JS, not replaces. `var` is still a useful signal for variables that are used throughout the function.
### `let` compatibility
- In server side, such as Node.js, you can safely use the `let` statement now.
- In client side, through a transpiler (like [Traceur](https://github.com/google/traceur-compiler)), you can safely use the `let` statement. Otherwise, please consider the browser support [here](http://caniuse.com/#search=let)
### Playground
### More info
- [Let keyword vs var keyword](http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword)
- [For and against let](https://davidwalsh.name/for-and-against-let)
- [Explanation of `let` and block scoping with for loops](http://stackoverflow.com/questions/30899612/explanation-of-let-and-block-scoping-with-for-loops/30900289#30900289).
title: Three useful hacks
tip-number: 60
tip-username: leandrosimoes
tip-username-profile: https://github.com/leandrosimoes
tip-tldr: Three very useful and syntax sugar hacks to speed up your development.
- /en/three-useful-hacks/
categories: - en - javascript
---
#### Getting array items from behind to front
If you want to get the array items from behind to front, just do this:
```javascript
var newArray = [1, 2, 3, 4];
console.log(newArray.slice(-1)); // [4]
console.log(newArray.slice(-2)); // [3, 4]
console.log(newArray.slice(-3)); // [2, 3, 4]
console.log(newArray.slice(-4)); // [1, 2, 3, 4]
```
#### Short-circuits conditionals
If you have to execute a function just if a condition is `true`, like this:
```javascript
if (condition) {
dosomething();
}
```
You can use a short-circuit just like this:
```javascript
condition && dosomething();
```
#### Set variable default values using "||"
If you have to set a default value to variables, you can simple do this:
```javascript
var a;
console.log(a); //undefined
a = a || 'default value';
console.log(a); //default value
a = a || 'new value';
console.log(a); //default value
```
title: Binding objects to functions
tip-number: 61
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Understanding how to use `Bind` method with objects and functions in JavaScript
- /en/binding-objects-to-functions/
categories: - en - javascript
---
More than often, we need to bind an object to a function's this object. JS uses the bind method when this is specified explicitly and we need to invoke desired method.
### Bind syntax
```js
fun.bind(thisArg[, arg1[, arg2[, ...]]])
```
## Parameters
**thisArg**
`this` parameter value to be passed to target function while calling the `bound` function.
**arg1, arg2, ...**
Prepended arguments to be passed to the `bound` function while invoking the target function.
**Return value**
A copy of the given function along with the specified `this` value and initial arguments.
### Bind method in action in JS
```js
const myCar = {
brand: 'Ford',
type: 'Sedan',
color: 'Red'
};
const getBrand = function () {
console.log(this.brand);
};
const getType = function () {
console.log(this.type);
};
const getColor = function () {
console.log(this.color);
};
getBrand(); // object not bind,undefined
getBrand(myCar); // object not bind,undefined
getType.bind(myCar)(); // Sedan
let boundGetColor = getColor.bind(myCar);
boundGetColor(); // Red
```
title: Working With Websocket Timeout
tip-number: 63
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: A trick to control the timeout
categories: - en - javascript
---
In case of established websocket connection, server or firewall could timeout and terminate the connection after a period of inactivity. To deal with this situation, we send periodic message to the server. To control the timeout we will add two functions in our code : one to make sure connection keep alive and another one to cancel the keep alive. Also we need a common `timerID` variable.
Let's have a look on implementation-
```js
var timerID = 0;
function keepAlive() {
var timeout = 20000;
if (webSocket.readyState == webSocket.OPEN) {
webSocket.send('');
}
timerId = setTimeout(keepAlive, timeout);
}
function cancelKeepAlive() {
if (timerId) {
clearTimeout(timerId);
}
}
```
Now as we have both of our desired function for the task, we will place `keepAlive()` function at the end of `onOpen()` method of websocket connection and `cancelKeepAlive()` function at the end of `onClose()` method of websocket connection.
Yes! We have perfectly implemented hack for websocket timeout problem.
title: 3 Array Hacks
tip-number: 64
tip-username: hassanhelfi
tip-username-profile: https://twitter.com/hassanhelfi
tip-tldr: Arrays are everywhere and with the new spread operators introduced in ECMAScript 6, you can do awesome things with them. In this post I will show you 3 useful tricks you can use when programming.
categories: - en - javascript
---
Arrays are everywhere in JavaScript and with the new [spread operators](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) introduced in ECMAScript 6, you can do awesome things with them. In this post I will show you 3 useful tricks you can use when programming.
### 1. Iterating through an empty array
JavaScript arrays are sparse in nature in that there are a lot of holes in them. Try creating an array using the Array's constructor and you will see what I mean.
```javascript
> const arr = new Array(4);
[undefined, undefined, undefined, undefined]
```
You may find that iterating over a sparse array to apply a certain transformation is hard.
```javascript
> const arr = new Array(4);
> arr.map((elem, index) => index);
[undefined, undefined, undefined, undefined]
```
To solve this, you can use `Array.apply` when creating the array.
```javascript
> const arr = Array.apply(null, new Array(4));
> arr.map((elem, index) => index);
[0, 1, 2, 3]
```
### 2. Passing an empty parameter to a method
If you want to call a method and ignore one of its parameters, then JavaScript will complain if you keep it empty.
```javascript
> method('parameter1', , 'parameter3');
Uncaught SyntaxError: Unexpected token ,
```
A workaround that people usually resort to is to pass either `null` or `undefined`.
```javascript
> method('parameter1', null, 'parameter3') // or
> method('parameter1', undefined, 'parameter3');
```
I personally don't like using `null` since JavaScript treats it as an object and that's just weird. With the introduction of spread operators in ES6, there is a neater way of passing empty parameters to a method. As previously mentioned, arrays are sparse in nature and so passing empty values to it is totally okay. We'll use this to our advantage.
```javascript
> method(...['parameter1', , 'parameter3']); // works!
```
### 3. Unique array values
I always wonder why the Array constructor does not have a designated method to facilitate the use of unique array values. Spread operators are here for the rescue. Use spread operators with the `Set` constructor to generate unique array values.
```javascript
> const arr = [...new Set([1, 2, 3, 3])];
[1, 2, 3]
```
title: Tapping for quick debugging
tip-number: 65
tip-username: loverajoel
tip-username-profile: https://twitter.com/loverajoel
tip-tldr: This little beastie here is tap. A really useful function for quick-debugging chains of function calls, anonymous functions and, actually, whatever you just want to print.
tip-md-link: https://github.com/loverajoel/jstips/blob/master/_posts/en/javascript/2017-03-16-tapping-for-quick-debugging.md
categories: - en - javascript
---
This little beastie here is tap. A really useful function for quick-debugging
chains of function calls, anonymous functions and, actually, whatever you just
want to print.
```javascript
function tap(x) {
console.log(x);
return x;
}
```
Why would you use instead of good old `console.log`? Let me show you an example:
```javascript
bank_totals_by_client(bank_info(1, banks), table)
.filter((c) => c.balance > 25000)
.sort((c1, c2) => (c1.balance <= c2.balance ? 1 : -1))
.map((c) => console.log(`${c.id} | ${c.tax_number} (${c.name}) => ${c.balance}`));
```
Now, suppose you're getting nothing from this chain (possibly an error).
Where is it failing? Maybe `bank_info` isn't returning anything, so we'll tap it:
```javascript
bank_totals_by_client(tap(bank_info(1, banks)), table);
```
Depending on our particular implementation, it might print something or not.
I'll assume the information that we got from our tapping was correct and
therefore, bank_info isn't causing anything.
We must then move on to the next chain, filter.
```javascript
.filter(c => tap(c).balance > 25000)
```
Are we receiving any c's (clients actually)? If so, then bank_totals_by_client
works alright. Maybe it's the condition within the filter?
```javascript
.filter(c => tap(c.balance > 25000))
```
Ah! Sweet, we see nothing but `false` printed, so there's no client with >25000,
that's why the function was returning nothing.
## (Bonus) A more advanced tap.
```javascript
function tap(x, fn = (x) => x) {
console.log(fn(x));
return x;
}
```
Now we're talking about a more advanced beast, what if we wanted to perform a
certain operation _prior_ to tapping? i.e, we want to access a certain object
property, perform a logical operation, etc. with our tapped object? Then we
call old good tap with an extra argument, a function to be applied at the moment
of tapping.
```javascript
tap(3, (x) => x + 2) === 3; // prints 5, but expression evaluates to true, why :-)?
```
title: Recursion, iteration and tail calls in JS
tip-number: 67
tip-username: loverajoel
tip-username-profile: https://twitter.com/loverajoel
tip-tldr: If you've been on the business for some time, you have, most likely, come across the definition of recursion, for which the factorial of a given number `n! = n * n - 1 * ... * 1` is a standard example...
tip-md-link: https://github.com/loverajoel/jstips/blob/master/_posts/en/javascript/2017-03-29-recursion-iteration-and-tail-calls-in-js.md
categories: - en - javascript
---
If you've been on the business for some time, you have, most likely,
come across the definition of recursion, for which the factorial of
a given number `n! = n * (n - 1) * ... * 1` is a standard example.
```javascript
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
```
The example shown above is but the most naive implementation of the
factorial function.
For the sake of completeness, let's look at how this executes for
`n = 6`:
- factorial(6)
- 6 \* factorial(5)
- 5 \* factorial (4)
- 4 \* factorial(3)
- 3 \* factorial(2)
- 2 \* factorial(1)
- 1 \* factorial(0)
- 1
- (resuming previous execution) 1 \* 1 = 1
- (resuming...) 2 \* 1 = 2
- (...) 3 \* 2 = 6
- ... 4 \* 6 = 24
- 5 \* 24 = 120
- 6 \* 120 = 720
- factorial(6) = 720
Now, we must be very cautious as to what's happening so we can understand
what is to come next.
When we invoke a function, several things happen at once. The location to
which we must return to after calling the function is saved, along with
the information of the current frame (i.e, the value of n). Then space is
allocated for the new function and a new frame is born.
This goes on and on, we keep stacking these frames and then we unwind that
stack, replacing function calls with values returned by them.
Another thing to notice is the shape of the process generated by our function.
You might not be surprised if I call this shape _recursive_. We have, thus, a
_recursive process_.
Let's take a look at a second implementation of this function.
```javascript
function factorial(n, res) {
if (n === 0) {
return res;
}
return factorial(n - 1, res * n);
}
```
We can encapsulate functionality a bit further by defining an inner function.
```javascript
function factorial(n) {
function inner_factorial(n, res) {
if (n === 0) {
return res;
}
return inner_factorial(n - 1, res * n);
}
return inner_factorial(n, 1);
}
```
Let's take a look at how this gets executed:
- factorial(6)
- inner anonymous function (iaf) gets called with (n = 6, res = 1)
- iaf(5, 1 \* 6)
- iaf(4, 6 \* 5)
- iaf(3, 30 \* 4)
- iaf(2, 120 \* 3)
- iaf(1, 360 \* 2)
- iaf(0, 720)
- 720
- 720
- 720
- 720
- 720
- 720
- 720
- iaf (6, 1) = 720
- factorial(6) = 720
You might notice that we didn't need to perform any calculation after unwinding
the stack. We just returned a value. But, according to our rules, we had to save
the state as a stack frame, even if it weren't of any use later in the chain.
Our rules, however, are not applied to every language out there. In fact, in
Scheme it's mandatory for such chains to be optimized with tail call
optimization. This ensures that our stack is not filled with unnecessary frames.
Our previous calculation would look, thus, this way:
- factorial(6)
- iaf(6, 1)
- iaf(5, 6)
- iaf(4, 30)
- iaf(3, 120)
- iaf(2, 360)
- iaf(1, 720)
- iaf(0, 720)
- 720
Which in turns, looks an awfully lot like
```javascript
res = 1;
n = 6;
while (n > 1) {
res = res * n;
n--;
}
```
This means, we actually have an _iterative process_, even if we're using
recursion. How cool is that?
The good news is, this is a feature in ES6. As long as your recursive call
is in tail position and your function has strict mode, tail call optimization
will kick in and save you from having a `maximum stack size exceeded` error.
UPDATE Dec 1, 2017:
The only major browser with tail call optimization is Safari.1 V8 has an implentation2 but has not shipped it yet3 for the reasons listed.
1: https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation)
2: https://bugs.chromium.org/p/v8/issues/detail?id=4698
3: https://v8project.blogspot.com/2016/04/es6-es7-and-beyond.html
title: Why you should use Object.is() in equality comparison
tip-number: 68
tip-username: TarekAlQaddy
tip-username-profile: https://github.com/TarekAlQaddy
tip-tldr: A good solution for the looseness of equality comparisons in javascript
categories: - en - javascript
---
We all know that JavaScript is loosely typed and in some cases it fall behind specially when it comes to quality comparison with '==', comparing with '==' gives unexpected results due to whats called coercion or casting "converting one of the 2 operands to the other's type then compare".
```javascript
0 == ' '; //true
(null == undefined[1]) == //true
true; //true
```
So they provided us with the triple equal operator '===' which is more strict and does not coerce operands, However comparing with '===' is not the best solution you can get:
```javascript
NaN === NaN; //false
```
The great news that in ES6 there is the new 'Object.is()' which is better and more precise it has the same features as '===' and moreover it behaves well in some special cases:
```javascript
Object.is(0, ' '); //false
Object.is(null, undefined); //false
Object.is([1], true); //false
Object.is(NaN, NaN); //true
```
Mozilla team doesn't think that Object.is is "stricter" than '===', they say that we should think of how this method deal with NaN, -0 and +0 but overall I think it is now a good practice in real applications.
Now this table illustrates..
![differences of operators in equality comparisons javascript](http://i.imgur.com/pCyqkLc.png)
## References:
[Equality comparisons and sameness](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
title: Picking and rejecting object properties
tip-number: 70
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Sometimes we need to whitelist certain attributes from an object, say we've got an array representation of a database table and we need to `select` just a few fields for some function.
categories: - en - javascript
---
Sometimes we need to whitelist certain attributes from an object, say we've
got an array representation of a database table and we need to `select` just
a few fields for some function:
```javascript
function pick(obj, keys) {
return keys.map((k) => (k in obj ? { [k]: obj[k] } : {})).reduce((res, o) => Object.assign(res, o), {});
}
const row = {
'accounts.id': 1,
'client.name': 'John Doe',
'bank.code': 'MDAKW213'
};
const table = [row, { 'accounts.id': 3, 'client.name': 'Steve Doe', 'bank.code': 'STV12JB' }];
pick(row, ['client.name']); // Get client name
table.map((row) => pick(row, ['client.name'])); // Get a list of client names
```
There's a bit of skulduggery going on in pick. First, we `map` a function over
the keys that will return, each time, an object with only the attribute pointed
by the current key (or an empty object if there's no such attribute in the
object). Then, we `reduce` this collection of single-attribute objects by
merging the objects.
But what if we want to `reject` the attributes? Well, the function changes a bit
```javascript
function reject(obj, keys) {
return Object.keys(obj)
.filter((k) => !keys.includes(k))
.map((k) => Object.assign({}, { [k]: obj[k] }))
.reduce((res, o) => Object.assign(res, o), {});
}
// or, reusing pick
function reject(obj, keys) {
const vkeys = Object.keys(obj).filter((k) => !keys.includes(k));
return pick(obj, vkeys);
}
reject({ a: 2, b: 3, c: 4 }, ['a', 'b']); // => {c: 4}
```
title: Protocols for the Brave
tip-number: 73
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: You might have heard about the old ways gaining hype recently, and we don't mean praying to the gods of the north. Today we're introducing a feature found in Clojure which allows you to define interfaces for your classes.
categories: - en - javascript
---
You might have heard about the old ways gaining hype recently, and we don't
mean praying to the gods of the north.
Functional programming is the rediscovered toy which is bringing some sanity
to the world of mutable state and global bindings.
Today we're introducing a feature found in Clojure which allows you to define
interfaces for your classes. Let's look at one-off implementation:
```javascript
const protocols = (...ps) => ps.reduce((c, p) => p(c), Object);
const Mappable = (klass) => {
return class extends klass {
map() {
throw 'Not implemented';
}
};
};
const Foldable = (klass) => {
return class extends klass {
fold() {
throw 'Not implemented';
}
};
};
class NaturalNumbers extends protocols(Mappable, Foldable) {
constructor() {
super();
this.elements = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
map(f) {
return this.elements.map(f);
}
fold(f) {
return this.elements.reduce(f, this.elements, 0);
}
}
```
Yes, we're building a chain of class inheritance up there with that reduce boy.
It's pretty cool. We're doing it dynamically! You see, each protocol receives
a base class (Object) and extends it somehow returning the new class. The idea
is similar to that of interfaces.
We supply method signatures for the protocol and make sure we provide
implementations for it on our base classes.
What's so cool about it? We get to write things like these:
```javascript
const map = (f) => (o) => o.map(f);
const fold = (f) => (o) => o.fold(f);
const compose = (...fns) => fns.reduce((acc, f) => (x) => acc(f(x)), id);
```
Ok, maybe we could have written those two functions without the above fuzz but,
now that we know `NaturalNumbers` are `Mappable`, we can call `map` on them
and trust it will return the right result. Furthermore, with our third function,
we can _compose_ any number of operations defined in protocols cleanly:
```javascript
const plus1 = (x) => x + 1;
const div5 = (x) => x / 5;
const plus_then_div = compose(map(div5), map(plus1));
console.log(plus_then_div(new NaturalNumbers()));
// => [ 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2 ]
```
More important, if we know that an object of ours is `Mappable`, we know `map`
will work on it. Protocols gives us an idea of what we can do with an object and
help us abstract common operations between data types, thus reducing the
overhead of dealing with a hundred functions.
What is easier? To have a hundred functions for every different object or ten
functions that work on a hundred objects?
title: Improving your Async functions with WebWorkers
tip-number: 74
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: JS runs in a single thread in the browser, this is the truth. In this tip I'll show you how to unleash the full power of asynchronous processing with Web Workers.
categories: - en - javascript
---
> JS shall have but one Thread (in the browser at least)
>
> -- Thus spoke the master programmer.
JS runs in a single thread in the browser, this is the truth.
Somewhere in its own universe, there exists a Queue which holds messages
and functions associated with them.
Every time an event (i.e, a user clicks a button) is registered, there's
a runtime check to see whether there's any listener attached to that event.
If there's one, it will enqueue the message. Otherwise, it will be lost
forever.
Now, our event loop processes one message at a time, meaning that if you
do some CPU intensive operation (i.e, number crunching) this will indeed
'block' the one Thread, rendering our application useless.
This is true even for `async` functions, which will be queued as soon as
invoked and executed as soon as possible (immediately given the queue is
empty).
I/O such as requests to external resources are non-blocking though, so you
can request a file as large as you want without fear. The associated
callback, however, will show the same characteristics of an `async` function.
Strategies for processing lots of data vary a lot. You could partition data
and set timeouts for processing bits of it a time for example. But to unleash
the full power of asynchronous processing, you should use Web Workers.
To do so, you separate the processing part in a different file (possibly
'my_worker.js'), create a worker with `newWorker = new Worker('my_worker.js');`
and offload the processing to it.
```javascript
// my_worker.js
const do_a_lot_of_processing = (data) => {
....
}
onmessage = (e) => {
postMessage(do_a_lot_of_processing(e.data));
}
// main.js
const myWorker = new Worker('my_worker.js');
async function get_useful_data() {
const raw_data = await request(some_url);
myWorker.postmessage(raw_data);
}
const show_data => (e) {
const data = e.data;
...
}
myWorker.onmessage(show_data);
get_useful_data();
```
Your mileage may vary of course, and there are many abstractions that can be
built upon this model.
title: Closures inside loops
tip-number: 76
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Closure in a loop is an interesting topic and this is the tip to be a master of it
categories: - en - javascript
---
If you ever come across the likes of
```javascript
var funcs = [];
for (var i = 0; i < 3; i++) {
funcs[i] = function () {
console.log('i value is ' + i);
};
}
for (var k = 0; k < 3; k++) {
funcs[k]();
}
```
You will notice that the expected output of
```
i value is 0
i value is 1
i value is 2
```
Doesn't match the actual output which will resemble
```
i value is 3
i value is 3
i value is 3
```
This is because of how the capturing mechanism of closures work and how `i` is represented internally.
To solve this situation you can do as follows:
```javascript
for (var i = 0; i < 3; i++) {
funcs[i] = (function (value) {
console.log('i value is ' + i);
})(i);
}
```
Which effectively copies i by value by handing it to our closure or
```javascript
for (let i = 0; i < 3; i++) {
funcs[i] = function () {
console.log('i value is ' + i);
};
}
```
Where `let` scopes the variable to our `for` loop and produces a new value each iteration, thus `i` will be bound to different values on our closures as expected.
title: Immutable structures and cloning
tip-number: 78
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Object cloning is a tricky, full of edge-cases, endeavor. The reason is simple enough. Objects maintain internal state, and that is much abused. There are countless techniques, or better phrased, countless derivations of the same technique.
categories: - en - javascript
---
Object cloning is a tricky, full of edge-cases, endeavor. The reason is simple
enough. Objects maintain internal state, and that is much abused. There are
countless techniques, or better phrased, countless derivations of the same
technique.
Cloning an object is an indicator that your application is growing, and that
you've got a complex object which you'd want to treat as an immutable value, i.e
operate on it while maintaining a previous state.
If the object is in your control, you're lucky. A bit of refactoring here and
there might lead you to a point where you avoid the problem entirely by
rethinking your object's structure and behavior.
With the rediscovering of functional programming techniques, a myriad of debates
have been held about immutable structures and how they offer exactly what you
seek for. Mutable state is the root of all evil, some might argue.
We encourage to reach **ImmutableJS** by Facebook which provides a nice set of
immutable structures free for use. By rethinking your object's inner workings
and separating state from behavior, making each function consume a state to
produce a new one - much like the Haskell's **State** monad - you will
reduce many nuisances.
If the object is outside your control, you're partly out of luck. This can be
circumvented by creating convoluted computations where you solve for yourself
circular references and reach enlightenment. However, as you're using external
objects anyways, and they must come, as their name says, from external sources,
then you might be more comfortable handling the matter to yet another external
library and focus on what matters the most, i.e, your application itself.
One such library is [pvorb/clone](https://github.com/pvorb/clone), which has a
very simple API. To clone an object you only have to
```javascript
var clone = require('clone');
var a = { foo: { bar: 'baz' } };
var b = clone(a);
a.foo.bar = 'foo';
console.log(a); // {foo: {bar: 'foo'}}
console.log(b); // {foo: {bar: 'baz'}}
```
There are, of course, many more libraries that allow you to do the same such as
[Ramda](http://ramdajs.com/docs/#clone), [lodash.clonedeep](https://www.npmjs.com/package/lodash.clonedeep)
and [lodash.clone](https://www.npmjs.com/package/lodash.clone).
As an end note, if you are serious about dealing with immutable structures, you
might want to check **ClojureScript** or (for those that feel that Haskell's
worth a shot) **PureScript**.
We neither encourage, nor condemn, the use of self made cloning mechanisms. Only
noting that considerable work has been done on the area and that you'd probably
be better of reusing than reinventing the wheel.
title: Looping over arrays
tip-number: 79
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: There's a few methods for looping over arrays in Javascript. We'll start with the classical ones and move towards additions made to the standard.
categories: - en - javascript
---
# Looping over arrays
There's a few methods for looping over arrays in Javascript. We'll start
with the classical ones and move towards additions made to the standard.
## while
```javascript
let index = 0;
const array = [1, 2, 3, 4, 5, 6];
while (index < array.length) {
console.log(array[index]);
index++;
}
```
## for (classical)
```javascript
const array = [1, 2, 3, 4, 5, 6];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
```
## forEach
```javascript
const array = [1, 2, 3, 4, 5, 6];
array.forEach(function (current_value, index, array) {
console.log(`At index ${index} in array ${array} the value is ${current_value}`);
});
// => undefined
```
## map
The last construct was useful, however, it doesn't return a new array which might
be undesirable for your specific case. `map` solves this by applying a function
over every element and then returning the new array.
```javascript
const array = [1, 2, 3, 4, 5, 6];
const square = (x) => Math.pow(x, 2);
const squares = array.map(square);
console.log(`Original array: ${array}`);
console.log(`Squared array: ${squares}`);
```
The full signature for `map` is `.map(current_value, index, array)`.
## reduce
From MDN:
> The reduce() method applies a function against an accumulator and each element
> in the array (from left to right) to reduce it to a single value.
```javascript
const array = [1, 2, 3, 4, 5, 6];
const sum = (x, y) => x + y;
const array_sum = array.reduce(sum, 0);
console.log(`The sum of array: ${array} is ${array_sum}`);
```
## filter
Filters elements on an array based on a boolean function.
```javascript
const array = [1, 2, 3, 4, 5, 6];
const even = (x) => x % 2 === 0;
const even_array = array.filter(even);
console.log(`Even numbers in array ${array}: ${even_array}`);
```
## every
Got an array and want to test if a given condition is met in every element?
```javascript
const array = [1, 2, 3, 4, 5, 6];
const under_seven = (x) => x < 7;
if (array.every(under_seven)) {
console.log('Every element in the array is less than 7');
} else {
console.log('At least one element in the array was bigger than 7');
}
```
## some
Test if at least one element matches our boolean function.
```javascript
const array = [1, 2, 3, 9, 5, 6, 4];
const over_seven = (x) => x > 7;
if (array.some(over_seven)) {
console.log('At least one element bigger than 7 was found');
} else {
console.log('No element bigger than 7 was found');
}
```
title: Hash maps without side effects
tip-number: 73
tip-username: bhaskarmelkani
tip-username-profile: https://www.twitter.com/bhaskarmelkani
tip-tldr: Create hash maps(without side effects) using `Object.create(null)`.
categories: - en - javascript
---
# Hash maps without side effects
When you want to use javascript object as a hash map(purely for storing data), you might want to create it as follows.
```javascript
const map = Object.create(null);
```
When creating a map using object literal(`const map = {}`), the map inherits properties from Object by default. It is equivalent to `Object.create(Object.prototype)`.
But by doing `Object.create(null)`, we explicitly specify `null` as its prototype. So it have absolutely no properties, not even constructor, toString, hasOwnProperty, etc. so you're free to use those keys in your data structure if you need to.
## Rationale:
```javascript
const dirtyMap = {};
const cleanMap = Object.create(null);
dirtyMap.constructor // function Object() { [native code] }
cleanMap.constructor // undefined
// Iterating maps
const key;
for(key in dirtyMap){
if (dirtyMap.hasOwnProperty(key)) { // Check to avoid iterating over inherited properties.
console.log(key + " -> " + dirtyMap[key]);
}
}
for(key in cleanMap){
console.log(key + " -> " + cleanMap[key]); // No need to add extra checks, as the object will always be clean
}
```
## Notes:
- Object.create() was introduced in ES5: [Compatibility](http://kangax.github.io/compat-table/es5/)
- ES6 introduced some new structures: [Map](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap), [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and [Weak Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet)
title: Creating immutable objects in native JavaScript
tip-number: 74
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: With the latest versions of JavaScript it's possible to create immutable objects. I'll walk you through how to do it in three different ways.
categories: - en - javascript
---
# Creating immutable objects in native JavaScript
Javascript it's a flexible language, you can redefine anything. But when projects get complex we find problems with mutable data structures.
With the latest versions of JavaScript this situation changed. Now it's possible to create immutable objects. I'll walk you through how to do it in three different ways.
### Wait, what means immutable?
> Immutability in object means we don't want our objects to change in any ways once we create them i.e make them read-only type.
Let's suppose we need to define a car [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) and use its properties to perform operations throughout our entire project.
We can't allow modifying by mistake any data.
```
const myTesla = {
maxSpeed: 250,
batteryLife: 300,
weight: 023
};
```
## [Object.preventExtensions()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions)
This method prevents the addition of new properties to our existing object.
`preventExtensions()` is a irreversible operation. We can never add extra properties to the object again.
```
Object.isExtensible(myTesla); // true
Object.preventExtensions(myTesla);
Object.isExtensible(myTesla); // false
myTesla.color = 'blue';
console.log(myTesla.color) // undefined
```
## [Object.seal()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal)
It prevents additions or deletion of properties. `seal()` also prevents the modification of property descriptors.
```
Object.isSealed(myTesla); // false
Object.seal(myTesla);
Object.isSealed(myTesla); // true
myTesla.color = 'blue';
console.log(myTesla.color); // undefined
delete myTesla.batteryLife; // false
console.log(myTesla.batteryLife); // 300
Object.defineProperty(myTesla, 'batteryLife'); // TypeError: Cannot redefine property: batteryLife
```
## [Object.freeze()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
It does the same that `Object.seal()` plus it makes the properties non-writable.
```
Object.isFrozen(myTesla); // false
Object.freeze(myTesla);
Object.isFrozen(myTesla); // true
myTesla.color = 'blue';
console.log(myTesla.color); // undefined
delete myTesla.batteryLife;
console.log(myTesla.batteryLife); // 300
Object.defineProperty(myTesla, 'batteryLife'); // TypeError: Cannot redefine property: batteryLife
myTesla.batteryLife = 400;
console.log(myTesla.batteryLife); // 300
```
## Extra
Use `strict mode` if you want to throw an error when trying to modify an immutable object.
title: What is Functional Inheritance?
tip-number: 75
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: Functional inheritance is the process of inheriting features by applying an augmenting function to an object instance.
categories: - en - javascript
---
Functional inheritance is the process of inheriting features by applying an augmenting function to an object instance. The function supplies a closure scope which you can use to keep some data private. The augmenting function uses dynamic object extension to extend the object instance with new properties and methods.
Functional mixins are composable factory functions that add properties and behaviors to objects like stations in an assembly line.
```javascript
// Base object constructor function
function Animal(data) {
var that = {}; // Create an empty object
that.name = data.name; // Add it a "name" property
return that; // Return the object
}
// Create achild object, inheriting from the base Animal
function Cat(data) {
// Create the Animal object
var that = Animal(data);
// Extend base object
that.sayHello = function () {
return "Hello, I'm " + that.name;
};
return that;
}
// Usage
var myCat = Cat({ name: 'Rufi' });
console.log(myCat.sayHello());
// Output: "Hello, I'm Rufi"
```
title: What is a currying function?
tip-number: 75
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: A currying function is a function that takes multiple arguments and turns it into a sequence of functions having only one argument at a time.
categories: - en - javascript
---
A currying function is a function that takes multiple arguments and turns it into a sequence of functions having only one argument at a time.
In this way, an n-ary function becomes a unary function, and the last function returns the result of all the arguments together in a function.
```javascript
// Normal definition
function multiply(a, b, c) {
return a * b * c;
}
console.log(multiply(1, 2, 3));
// Output: 6
// Simple curry function definition
function multiply(a) {
return (b) => {
return (c) => {
return a * b * c;
};
};
}
console.log(multiply(1)(2)(3));
// Output: 6
```
### Further readings:
- [Currying in JavaScript](https://dev.to/suprabhasupi/currying-in-javascript-1k3l)
- [Lodash curry](https://lodash.com/docs/#curry)
- [JavaScript currying](http://zetcode.com/javascript/currying/)
title: What is the Temporal Dead Zone?
tip-number: 76
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: Temporal Dead Zone is a JavaScript behavior while using variables declared using `let` and `const` keywords.
categories: - en - javascript
---
Temporal Dead Zone is a JavaScript behavior while using variables declared using `let` and `const` keywords. Since the keywords are block-scoped, the variables declared these keywords could not be accessed before the declaration, and then you will have to witness where variables will be said to be `undefined`.
```javascript
function myFunc() {
console.log(greeting);
var greeting = 'Hello World!';
}
myFunc(); // Output: undefined
function myFunc() {
console.log(greeting);
let greeting = 'Hello World!';
}
myFunc(); // Output: ReferenceError: greeting is not defined
function myFunc() {
console.log(greeting);
const greeting = 'Hello World!';
}
myFunc(); // Output: ReferenceError: greeting is not defined
```
title: What is the difference between Target and currentTarget in the event context?
tip-number: 77
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: target refers to the element that triggers an event. currentTarget to the element that the event listener is listening on.
categories: - en - javascript
---
`target` refers to the DOM element that triggers an event. Otherwise, `currentTarget` refers to the DOM element that the event listener is listening on.
```html
});
```
title: What is a spread operator?
tip-number: 78
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: The spread operator is a useful syntax for manipulating arrays and objects.
categories: - en - javascript
---
The spread operator in JavaScript is a useful syntax for adding elements to an array, combining arrays into one larger one, spreading an array inside the arguments of a function, and more.
```js
// Concatenating arrays and objects
let arr1 = [1, 2, 3];
let arr2 = [4, 5];
let newArray = [...arr1, ...arr2];
console.log(newArray);
// Output: [ 1, 2, 3, 4, 5 ]
// Copying array elements
let arr = ['a', 'b', 'c'];
let newArray = [...arr];
console.log(newArray);
// Output: ["a", "b", "c"]
// Expanding arrays
let arr = ['a', 'b'];
let newArray = [...arr, 'c', 'd'];
console.log(newArray);
// Output: ["a", "b", "c", "d"]
// Merging objects
const userBasic = {
name: 'Jen',
age: 22
};
const userMoreInfo = {
country: 'Argentina',
city: 'Córdoba'
};
const user = { ...userBasic, ...userMoreInfo };
// Output: { name: "Jen", age: 22, country: "Argentina", city: "Córdoba" }
```
title: What is a void operator?
tip-number: 79
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: The void operator returns an undefined value from an evaluated expression
categories: - en - javascript
---
The `void` operator returns an `undefined` value from an evaluated expression, or in other words; the `void` operator specifies an expression to be evaluated without returning a value. It is commonly used in client-side JavaScript, where the browser should not display the value.
```js
function getYear() {
return 2020;
}
console.log(getYear());
// Output: 2020
console.log(void getYear());
// Output: undefined
// Useful use case
button.onclick = () => void getYear();
```
---
layout: post
title: What is the promise executor?
tip-number: 80
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: The method received as an argument for the promise.
categories:
- en
- javascript
---
All `Promise` instances accept a method as an argument called the executor. This executor takes two methods as arguments: resolve and reject. Within the executor, if resolve is called, the `Promise` instance becomes fulfilled. If an exception is thrown, reject is called instead, and the `Promise` instance becomes rejected.
```js
const executor = (resolve, reject) => {
setTimeout(() => resolve("I'm done"), 1000);
};
new Promise(executor).then((result) => {
console.log(result);
// Output after 1000ms: I'm done
});
```
---
layout: post
title: What is the JavaScript ternary operator?
tip-number: 81
tip-username: loverajoel
tip-username-profile: https://www.twitter.com/loverajoel
tip-tldr: The ternary operator is a shortcut for the if statement.
categories:
- en
- javascript
---
The ternary operator is a shortcut for the `if` statement. It consists of three operands; a question mark, a condition, and an expression to execute if the condition is true, followed by a colon and another expression to execute if it's false.
```js
let age = 26;
// condition ? expression if true : expression if false
let drink = age >= 21 ? 'Beer' : 'Juice';
console.log(drink); // "Beer"
// Equivalent to:
let drink;
if (age >= 21) {
drink = 'Beer';
} else {
drink = 'Juice';
}
console.log(drink); // "Beer"
```
# About the JavaScript reference
The JavaScript reference serves as a repository of facts about the JavaScript language. The entire language is described here in detail. As you write JavaScript code, you'll refer to these pages often (thus the title "JavaScript reference"). If you're learning JavaScript, or need help understanding some of its capabilities or features, check out the [JavaScript guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide).
The JavaScript language is intended to be used within some larger environment, be it a browser, server-side scripts, or similar. For the most part, this reference attempts to be environment-agnostic and does not target a web browser environment.
## Where to find JavaScript information
JavaScript documentation of core language features (pure [ECMAScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Language_Resources), for the most part) includes the following:
- The [JavaScript guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)
- The [JavaScript reference](index)
If you are new to JavaScript, start with the [guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide). Once you have a firm grasp of the fundamentals, you can use the [reference](index) to get more details on individual objects and language constructs.
## Structure of the reference
In the JavaScript reference you can find the following chapters:
[Standard built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)
This chapter documents all the JavaScript standard built-in objects, along with their methods and properties.
[Statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements)
JavaScript applications consist of statements with an appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon. This isn't a keyword, but a group of keywords.
[Expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)
This chapter documents all the JavaScript language operators, expressions and keywords.
Functions
Chapter about JavaScript functions.
[Classes](classes)
Chapter about JavaScript classes introduced in ECMAScript 2015.
[Errors](errors)
Chapter about specific errors, exceptions and warnings thrown by JavaScript.
[New in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript)
Chapter about JavaScript version history.
### More reference pages
- [Deprecated and obsolete features](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features)
- [Lexical grammar](lexical_grammar)
- [Data types and data structures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/About
# Math.abs()
The `Math.abs()` function returns the absolute value of a number. That is, it returns `x` if `x` is positive or zero, and the negation of `x` if `x` is negative.
## Syntax
Math.abs(x)
### Parameters
`x`
A number.
### Return value
The absolute value of the given number.
## Description
Because `abs()` is a static method of `Math`, you always use it as `Math.abs()`, rather than as a method of a `Math` object you created (`Math` is not a constructor).
## Examples
### Behavior of Math.abs()
Passing an empty object, an array with more than one member, a non-numeric string or [`undefined`](../undefined)/empty variable returns [`NaN`](../nan). Passing [`null`](../null), an empty string or an empty array returns 0.
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN
## Specifications
`add`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
## See also
- [`Atomics`](../atomics)
- [`Atomics.sub()`](sub)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/add
# Addition (+)
The addition operator (`+`) produces the sum of numeric operands or string concatenation.
## Syntax
Operator: x + y
## Examples
### Numeric addition
// Number + Number -> addition
1 + 2 // 3
// Boolean + Number -> addition
true + 1 // 2
// Boolean + Boolean -> addition
false + false // 0
### String concatenation
// String + String -> concatenation
'foo' + 'bar' // "foobar"
// Number + String -> concatenation
5 + 'foo' // "5foo"
// String + Boolean -> concatenation
'foo' + false // "foofalse"
## Specifications
`AggregateError`
85
85
79
No
No
14
85
85
79
No
14
No
`AggregateError`
85
85
79
No
No
14
85
85
79
No
14
No
## See also
- [`Error`](error)
- [`Promise.any`](promise/any)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
# Promise.all()
The `Promise.all()` method takes an iterable of promises as an input, and returns a single [`Promise`](../promise) that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.
## Syntax
Promise.all(iterable);
### Parameters
`iterable`
An [iterable](../../iteration_protocols#the_iterable_protocol) object such as an [`Array`](../array).
### Return value
- An **already resolved** [`Promise`](../promise) if the iterable passed is empty.
- An **asynchronously resolved** [`Promise`](../promise) if the iterable passed contains no promises. Note, Google Chrome 58 returns an **already resolved** promise in this case.
- A **pending** [`Promise`](../promise) in all other cases. This returned promise is then resolved/rejected **asynchronously** (as soon as the stack is empty) when all the promises in the given iterable have resolved, or if any of the promises reject. See the example about "Asynchronicity or synchronicity of Promise.all" below. Returned values will be in order of the Promises passed, regardless of completion order.
## Description
This method can be useful for aggregating the results of multiple promises. It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues.
`Promise.all()` will reject immediately upon **any** of the input promises rejecting. In comparison, the promise returned by [`Promise.allSettled()`](allsettled) will wait for all input promises to complete, regardless of whether or not one rejects. Consequently, it will always return the final result of every promise and function from the input iterable.
### Fulfillment
The returned promise is fulfilled with an array containing **all** the resolved values (including non-promise values) in the iterable passed as the argument.
- If an empty iterable is passed, then the promise returned by this method is fulfilled synchronously. The resolved value is an empty array.
- If a nonempty _iterable_ is passed, and **all** of the promises fulfill, or are not promises, then the promise returned by this method is fulfilled asynchronously.
### Rejection
If any of the passed-in promises reject, `Promise.all` asynchronously rejects with the value of the promise that rejected, whether or not the other promises have resolved.
## Examples
### Using `Promise.all`
`Promise.all` waits for all fulfillments (or the first rejection).
var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("foo");
}, 100);
});
Promise.all([p1, p2, p3]).then(values => {
console.log(values); // [3, 1337, "foo"]
});
If the iterable contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled):
// this will be counted as if the iterable passed is empty, so it gets fulfilled
var p = Promise.all([1,2,3]);
// this will be counted as if the iterable passed contains only the resolved promise with value "444", so it gets fulfilled
var p2 = Promise.all([1,2,3, Promise.resolve(444)]);
// this will be counted as if the iterable passed contains only the rejected promise with value "555", so it gets rejected
var p3 = Promise.all([1,2,3, Promise.reject(555)]);
// using setTimeout we can execute code after the stack is empty
setTimeout(function() {
console.log(p);
console.log(p2);
console.log(p3);
});
// logs
// Promise { : "fulfilled", : Array[3] }
// Promise { : "fulfilled", : Array[4] }
// Promise { : "rejected", : 555 }
### Asynchronicity or synchronicity of `Promise.all`
This following example demonstrates the asynchronicity (or synchronicity, if the iterable passed is empty) of `Promise.all`:
// we are passing as argument an array of promises that are already resolved,
// to trigger Promise.all as soon as possible
var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];
var p = Promise.all(resolvedPromisesArray);
// immediately logging the value of p
console.log(p);
// using setTimeout we can execute code after the stack is empty
setTimeout(function() {
console.log('the stack is now empty');
console.log(p);
});
// logs, in order:
// Promise { : "pending" }
// the stack is now empty
// Promise { : "fulfilled", : Array[2] }
The same thing happens if `Promise.all` rejects:
var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
var p = Promise.all(mixedPromisesArray);
console.log(p);
setTimeout(function() {
console.log('the stack is now empty');
console.log(p);
});
// logs
// Promise { : "pending" }
// the stack is now empty
// Promise { : "rejected", : 44 }
But, `Promise.all` resolves synchronously **if and only if** the iterable passed is empty:
var p = Promise.all([]); // will be immediately resolved
var p2 = Promise.all([1337, "hi"]); // non-promise values will be ignored, but the evaluation will be done asynchronously
console.log(p);
console.log(p2)
setTimeout(function() {
console.log('the stack is now empty');
console.log(p2);
});
// logs
// Promise { : "fulfilled", : Array[0] }
// Promise { : "pending" }
// the stack is now empty
// Promise { : "fulfilled", : Array[2] }
### `Promise.all` fail-fast behavior
`Promise.all` is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then `Promise.all` will reject immediately.
var p1 = new Promise((resolve, reject) => {
setTimeout(() => resolve('one'), 1000);
});
var p2 = new Promise((resolve, reject) => {
setTimeout(() => resolve('two'), 2000);
});
var p3 = new Promise((resolve, reject) => {
setTimeout(() => resolve('three'), 3000);
});
var p4 = new Promise((resolve, reject) => {
setTimeout(() => resolve('four'), 4000);
});
var p5 = new Promise((resolve, reject) => {
reject(new Error('reject'));
});
// Using .catch:
Promise.all([p1, p2, p3, p4, p5])
.then(values => {
console.log(values);
})
.catch(error => {
console.error(error.message)
});
//From console:
//"reject"
It is possible to change this behavior by handling possible rejections:
var p1 = new Promise((resolve, reject) => {
setTimeout(() => resolve('p1_delayed_resolution'), 1000);
});
var p2 = new Promise((resolve, reject) => {
reject(new Error('p2_immediate_rejection'));
});
Promise.all([
p1.catch(error => { return error }),
p2.catch(error => { return error }),
]).then(values => {
console.log(values[0]) // "p1_delayed_resolution"
console.error(values[1]) // "Error: p2_immediate_rejection"
})
## Specifications
For example, a bitwise AND of `5 & 1` results in `0001` which is 1 in decimal.
5 0101
1 0001
----
1 0001
## Examples
### Using and()
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 5;
Atomics.and(ta, 0, 1); // returns 0, the old value
Atomics.load(ta, 0); // 1
## Specifications
`and`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
## See also
- [`Atomics`](../atomics)
- [`Atomics.or()`](or)
- [`Atomics.xor()`](xor)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/and
# Promise.any()
`Promise.any()` takes an iterable of [`Promise`](../promise) objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfill (if all of the given promises are rejected), then the returned promise is rejected with an [`AggregateError`](../aggregateerror), a new subclass of [`Error`](../error) that groups together individual errors. Essentially, this method is the opposite of [`Promise.all()`](all).
## Syntax
Promise.any(iterable);
### Parameters
`iterable`
An [iterable](../../iteration_protocols#the_iterable_protocol) object, such as an [`Array`](../array).
### Return value
- An **already rejected** [`Promise`](../promise) if the iterable passed is empty.
- An **asynchronously resolved** [`Promise`](../promise) if the iterable passed contains no promises.
- A **pending** [`Promise`](../promise) in all other cases. This returned promise is then resolved/rejected **asynchronously** (as soon as the stack is empty) when any of the promises in the given iterable resolve, or if all the promises have rejected.
## Description
This method is useful for returning the first promise that fulfills. It short-circuits after a promise fulfills, so it does not wait for the other promises to complete once it finds one. Unlike [`Promise.all()`](all), which returns an _array_ of fulfillment values, we only get one fulfillment value (assuming at least one promise fulfills). This can be beneficial if we need only one promise to fulfill but we do not care which one does. Note another difference: This method rejects upon receiving an _empty iterable_, since, truthfully, the iterable contains no items that fulfill.
Also, unlike [`Promise.race()`](race), which returns the first _settled_ value (either fulfillment or rejection), this method returns the first _fulfilled_ value. This method will ignore all rejected promises up until the first promise that fulfils.
### Fulfillment
The returned promise is fulfilled with **the first** resolved value (or non-promise value) in the iterable passed as the argument, whether or not the other promises have rejected.
- If a nonempty _iterable_ is passed, and **any** of the promises fulfill, or are not promises, then the promise returned by this method is fulfilled asynchronously.
### Rejection
If all of the passed-in promises reject, `Promise.any` asynchronously rejects with an [`AggregateError`](../aggregateerror) object, which extends [`Error`](../error), and contains an `errors` property with an array of rejection values.
- If an empty iterable is passed, then the promise returned by this method is rejected synchronously. The rejected reason is an `AggregateError` object whose `errors` property is an empty array.
## Examples
### First to fulfil
`Promise.any()` resolves with the first promise to fulfil, even if a promise rejects first. This is in contrast to [`Promise.race()`](race), which resolves or rejects with the first promise to settle.
const pErr = new Promise((resolve, reject) => {
reject("Always fails");
});
const pSlow = new Promise((resolve, reject) => {
setTimeout(resolve, 500, "Done eventually");
});
const pFast = new Promise((resolve, reject) => {
setTimeout(resolve, 100, "Done quick");
});
Promise.any([pErr, pSlow, pFast]).then((value) => {
console.log(value);
// pFast fulfils first
})
// expected output: "Done quick"
### Rejections with AggregateError
`Promise.any()` rejects with an [`AggregateError`](../aggregateerror) if no promise fulfils.
const pErr = new Promise((resolve, reject) => {
reject('Always fails');
});
Promise.any([pErr]).catch((err) => {
console.log(err);
})
// expected output: "AggregateError: No Promise in Promise.any was resolved"
### Displaying the first image loaded
In this example, we have a function that fetches an image and returns a blob. We use `Promise.any()` to fetch a couple of images and display the first one available (i.e. whose promise has resolved).
function fetchAndDecode(url) {
return fetch(url).then(response => {
if(!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
} else {
return response.blob();
}
})
}
let coffee = fetchAndDecode('coffee.jpg');
let tea = fetchAndDecode('tea.jpg');
Promise.any([coffee, tea]).then(value => {
let objectURL = URL.createObjectURL(value);
let image = document.createElement('img');
image.src = objectURL;
document.body.appendChild(image);
})
.catch(e => {
console.log(e.message);
});
## Specifications
';
var args = Array.prototype.slice.call(arguments, 1);
html += args.join('
');
html += '
' + type + 'l>'; // end list
return html;
}
You can pass any number of arguments to this function, and it adds each argument as a list item to a list of the type indicated. For example:
let listHTML = list('u', 'One', 'Two', 'Three');
/* listHTML is:
"
One
Two
Three
"
*/
### Rest, default, and destructured parameters
The `arguments` object can be used in conjunction with [rest](rest_parameters), [default](default_parameters), and [destructured](../operators/destructuring_assignment) parameters.
function foo(...args) {
return args;
}
foo(1, 2, 3); // [1, 2, 3]
While the presence of rest, default, or destructured parameters does not alter [the behavior of the `arguments` object in strict mode code](../strict_mode#making_eval_and_arguments_simpler), there are subtle differences for non-strict code.
In strict-mode code, the `arguments` object behaves the same whether or not a function is passed rest, default, or destructured parameters. That is, assigning new values to variables in the body of the function will not affect the `arguments` object. Nor will assigning new variables to the `arguments` object affect the value of variables.
**Note:** You cannot write a `"use strict";` directive in the body of a function definition that accepts rest, default, or destructured parameters. Doing so will throw [a syntax error](../errors/strict_non_simple_params).
Non-strict functions that are passed only simple parameters (that is, not rest, default, or restructured parameters) will sync the value of variables new values in the body of the function with the `arguments` object, and vice versa:
function func(a) {
arguments[0] = 99; // updating arguments[0] also updates a
console.log(a);
}
func(10); // 99
And also:
function func(a) {
a = 99; // updating a also updates arguments[0]
console.log(arguments[0]);
}
func(10); // 99
Conversely, non-strict functions that **are** passed rest, default, or destructured parameters **will not** sync new values assigned to argument variables in the function body with the `arguments` object. Instead, the `arguments` object in non-strict functions with complex parameters **will always** reflect the values passed to the function when the function was called (this is the same behavior as exhibited by all strict-mode functions, regardless of the type of variables they are passed):
function func(a = 55) {
arguments[0] = 99; // updating arguments[0] does not also update a
console.log(a);
}
func(10); // 10
And also:
function func(a = 55) {
a = 99; // updating a does not also update arguments[0]
console.log(arguments[0]);
}
func(10); // 10
And also:
// An untracked default parameter
function func(a = 55) {
console.log(arguments[0]);
}
func(); // undefined
## Specifications
`arguments`
1
12
1
3
3
1
1
18
4
10.1
1
1.0
`callee`
1
12
1
6
4
1
1
18
4
10.1
1
1.0
`length`
1
12
1
4
4
1
1
18
4
10.1
1
1.0
`@@iterator`
52
12
46
No
39
9
52
52
46
41
9
6.0
## See also
- [`Function`](../global_objects/function)
- [Rest parameters](rest_parameters)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
# Array
The JavaScript `Array` class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
## Description
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.
Arrays cannot use strings as element indexes (as in an [associative array](https://en.wikipedia.org/wiki/Associative_array)) but must use integers. Setting or accessing via non-integers using [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#objects_and_properties) (or [dot notation](../operators/property_accessors)) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's [object property collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties). The array's object properties and list of array elements are separate, and the array's [traversal and mutation operations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#array_methods) cannot be applied to these named properties.
### Common operations
**Create an Array**
let fruits = ['Apple', 'Banana']
console.log(fruits.length)
// 2
**Access an Array item using the index position**
let first = fruits[0]
// Apple
let last = fruits[fruits.length - 1]
// Banana
**Loop over an Array**
fruits.forEach(function(item, index, array) {
console.log(item, index)
})
// Apple 0
// Banana 1
**Add an item to the end of an Array**
let newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]
**Remove an item from the end of an Array**
let last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]
**Remove an item from the beginning of an Array**
let first = fruits.shift() // remove Apple from the front
// ["Banana"]
**Add an item to the beginning of an Array**
let newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]
**Find the index of an item in the Array**
fruits.push('Mango')
// ["Strawberry", "Banana", "Mango"]
let pos = fruits.indexOf('Banana')
// 1
**Remove an item by index position**
let removedItem = fruits.splice(pos, 1) // this is how to remove an item
// ["Strawberry", "Mango"]
**Remove items from an index position**
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
console.log(vegetables)
// ["Cabbage", "Turnip", "Radish", "Carrot"]
let pos = 1
let n = 2
let removedItems = vegetables.splice(pos, n)
// this is how to remove items, n defines the number of items to be removed,
// starting at the index position specified by pos and progressing toward the end of array.
console.log(vegetables)
// ["Cabbage", "Carrot"] (the original array is changed)
console.log(removedItems)
// ["Turnip", "Radish"]
**Copy an Array**
let shallowCopy = fruits.slice() // this is how to make a copy
// ["Strawberry", "Mango"]
### Accessing array elements
JavaScript arrays are zero-indexed. The first element of an array is at index `0`, and the last element is at the index value equal to the value of the array's [`length`](array/length) property minus `1`.
Using an invalid index number returns `undefined`.
let arr = ['this is the first element', 'this is the second element', 'this is the last element']
console.log(arr[0]) // logs 'this is the first element'
console.log(arr[1]) // logs 'this is the second element'
console.log(arr[arr.length - 1]) // logs 'this is the last element'
Array elements are object properties in the same way that `toString` is a property (to be specific, however, `toString()` is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid:
console.log(arr.0) // a syntax error
There is nothing special about JavaScript arrays and the properties that cause this. JavaScript properties that begin with a digit cannot be referenced with dot notation and must be accessed using bracket notation.
For example, if you had an object with a property named `3d`, it can only be referenced using bracket notation.
let years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
console.log(years.0) // a syntax error
console.log(years[0]) // works properly
renderer.3d.setTexture(model, 'character.png') // a syntax error
renderer['3d'].setTexture(model, 'character.png') // works properly
In the `3d` example, `'3d'` _had_ to be quoted (because it begins with a digit). But it's also possible to quote the array indexes as well (e.g., `years['2']` instead of `years[2]`), although it's not necessary.
The `2` in `years[2]` is coerced into a string by the JavaScript engine through an implicit `toString` conversion. As a result, `'2'` and `'02'` would refer to two different slots on the `years` object, and the following example could be `true`:
console.log(years['2'] != years['02'])
### Relationship between length and numerical properties
A JavaScript array's [`length`](array/length) property and numerical properties are connected.
Several of the built-in array methods (e.g., [`join()`](array/join), [`slice()`](array/slice), [`indexOf()`](array/indexof), etc.) take into account the value of an array's [`length`](array/length) property when they're called.
Other methods (e.g., [`push()`](array/push), [`splice()`](array/splice), etc.) also result in updates to an array's [`length`](array/length) property.
const fruits = []
fruits.push('banana', 'apple', 'peach')
console.log(fruits.length) // 3
When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's [`length`](array/length) property accordingly:
fruits[5] = 'mango'
console.log(fruits[5]) // 'mango'
console.log(Object.keys(fruits)) // ['0', '1', '2', '5']
console.log(fruits.length) // 6
Increasing the [`length`](array/length).
fruits.length = 10
console.log(fruits) // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)) // ['0', '1', '2', '5']
console.log(fruits.length) // 10
console.log(fruits[8]) // undefined
Decreasing the [`length`](array/length) property does, however, delete elements.
fruits.length = 2
console.log(Object.keys(fruits)) // ['0', '1']
console.log(fruits.length) // 2
This is explained further on the [`Array.length`](array/length) page.
### Creating an array using the result of a match
The result of a match between a [`RegExp`](regexp) and a string can create a JavaScript array. This array has properties and elements which provide information about the match. Such an array is returned by [`RegExp.exec()`](regexp/exec), [`String.match()`](string/match), and [`String.replace()`](string/replace).
To help explain these properties and elements, see this example and then refer to the table below:
// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case
const myRe = /d(b+)(d)/i
const myArray = myRe.exec('cdbBdbsbz')
The properties and elements returned from this match are as follows:
Property/Element
Description
Example
input Read only
The original string against which the regular expression was matched.
"cdbBdbsbz"
index Read only
The zero-based index of the match in the string.
1
[0] Read only
The last matched characters.
"dbBd"
[1], ...[n] Read only
Elements that specify the parenthesized substring matches (if included) in the regular expression. The number of possible parenthesized substrings is unlimited.
[1]: "bB" [2]: "d"
## Constructor
[`Array()`](array/array)
Creates a new `Array` object.
## Static properties
[`get Array[@@species]`](array/@@species)
The constructor function is used to create derived objects.
## Static methods
[`Array.from()`](array/from)
Creates a new `Array` instance from an array-like or iterable object.
[`Array.isArray()`](array/isarray)
Returns `true` if the argument is an array, or `false` otherwise.
[`Array.of()`](array/of)
Creates a new `Array` instance with a variable number of arguments, regardless of number or type of the arguments.
## Instance properties
[`Array.prototype.length`](array/length)
Reflects the number of elements in an array.
[`Array.prototype[@@unscopables]`](array/@@unscopables)
A symbol containing property names to exclude from a [`with`](../statements/with) binding scope.
## Instance methods
[`Array.prototype.at()`](array/at) This is an experimental API that should not be used in production code.
Returns the array item at the given index. Accepts negative integers, which count back from the last item.
[`Array.prototype.concat()`](array/concat)
Returns a new array that is this array joined with other array(s) and/or value(s).
[`Array.prototype.copyWithin()`](array/copywithin)
Copies a sequence of array elements within the array.
[`Array.prototype.entries()`](array/entries)
Returns a new `Array Iterator` object that contains the key/value pairs for each index in the array.
[`Array.prototype.every()`](array/every)
Returns `true` if every element in this array satisfies the testing function.
[`Array.prototype.fill()`](array/fill)
Fills all the elements of an array from a start index to an end index with a static value.
[`Array.prototype.filter()`](array/filter)
Returns a new array containing all elements of the calling array for which the provided filtering function returns `true`.
[`Array.prototype.find()`](array/find)
Returns the found `element` in the array, if some element in the array satisfies the testing function, or `undefined` if not found.
[`Array.prototype.findIndex()`](array/findindex)
Returns the found index in the array, if an element in the array satisfies the testing function, or `-1` if not found.
[`Array.prototype.forEach()`](array/foreach)
Calls a function for each element in the array.
[`Array.prototype.includes()`](array/includes)
Determines whether the array contains a value, returning `true` or `false` as appropriate.
[`Array.prototype.indexOf()`](array/indexof)
Returns the first (least) index of an element within the array equal to an element, or `-1` if none is found.
[`Array.prototype.join()`](array/join)
Joins all elements of an array into a string.
[`Array.prototype.keys()`](array/keys)
Returns a new `Array Iterator` that contains the keys for each index in the array.
[`Array.prototype.lastIndexOf()`](array/lastindexof)
Returns the last (greatest) index of an element within the array equal to an element, or `-1` if none is found.
[`Array.prototype.map()`](array/map)
Returns a new array containing the results of calling a function on every element in this array.
[`Array.prototype.pop()`](array/pop)
Removes the last element from an array and returns that element.
[`Array.prototype.push()`](array/push)
Adds one or more elements to the end of an array, and returns the new `length` of the array.
[`Array.prototype.reduce()`](array/reduce)
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
[`Array.prototype.reduceRight()`](array/reduceright)
Apply a function against an accumulator> and each value of the array (from right-to-left) as to reduce it to a single value.
[`Array.prototype.reverse()`](array/reverse)
Reverses the order of the elements of an array _in place_. (First becomes the last, last becomes first.)
[`Array.prototype.shift()`](array/shift)
Removes the first element from an array and returns that element.
[`Array.prototype.slice()`](array/slice)
Extracts a section of the calling array and returns a new array.
[`Array.prototype.some()`](array/some)
Returns `true` if at least one element in this array satisfies the provided testing function.
[`Array.prototype.sort()`](array/sort)
Sorts the elements of an array in place and returns the array.
[`Array.prototype.splice()`](array/splice)
Adds and/or removes elements from an array.
[`Array.prototype.toLocaleString()`](array/tolocalestring)
Returns a localized string representing the array and its elements. Overrides the [`Object.prototype.toLocaleString()`](object/tolocalestring) method.
[`Array.prototype.toString()`](array/tostring)
Returns a string representing the array and its elements. Overrides the [`Object.prototype.toString()`](object/tostring) method.
[`Array.prototype.unshift()`](array/unshift)
Adds one or more elements to the front of an array, and returns the new `length` of the array.
[`Array.prototype.values()`](array/values)
Returns a new `Array Iterator` object that contains the values for each index in the array.
[`Array.prototype[@@iterator]()`](array/@@iterator)
Returns a new `Array Iterator` object that contains the values for each index in the array.
## Examples
### Creating an array
The following example creates an array, `msgArray`, with a length of `0`, then assigns values to `msgArray[0]` and `msgArray[99]`, changing the `length` of the array to `100`.
let msgArray = []
msgArray[0] = 'Hello'
msgArray[99] = 'world'
if (msgArray.length === 100) {
console.log('The length is 100.')
}
### Creating a two-dimensional array
The following creates a chessboard as a two-dimensional array of strings. The first move is made by copying the `'p'` in `board[6][4]` to `board[4][4]`. The old position at `[6][4]` is made blank.
let board = [
['R','N','B','Q','K','B','N','R'],
['P','P','P','P','P','P','P','P'],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
['p','p','p','p','p','p','p','p'],
['r','n','b','q','k','b','n','r'] ]
console.log(board.join('\n') + '\n\n')
// Move King's Pawn forward 2
board[4][4] = board[6][4]
board[6][4] = ' '
console.log(board.join('\n'))
Here is the output:
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
p,p,p,p,p,p,p,p
r,n,b,q,k,b,n,r
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
, , , , , , ,
, , , , , , ,
, , , ,p, , ,
, , , , , , ,
p,p,p,p, ,p,p,p
r,n,b,q,k,b,n,r
### Using an array to tabulate a set of values
values = []
for (let x = 0; x < 10; x++){
values.push([
2 ** x,
2 * x ** 2
])
}
console.table(values)
Results in
// The first column is the index
0 1 0
1 2 2
2 4 8
3 8 18
4 16 32
5 32 50
6 64 72
7 128 98
8 256 128
9 512 162
# TypeError: invalid Array.prototype.sort argument
The JavaScript exception "invalid Array.prototype.sort argument" occurs when the argument of [`Array.prototype.sort()`](../global_objects/array/sort) isn't either [`undefined`](../global_objects/undefined) or a function which compares its operands.
## Message
TypeError: argument is not a function object (Edge)
TypeError: invalid Array.prototype.sort argument (Firefox)
## Error type
[`TypeError`](../global_objects/typeerror)
## What went wrong?
The argument of [`Array.prototype.sort()`](../global_objects/array/sort) is expected to be either [`undefined`](../global_objects/undefined) or a function which compares its operands.
## Examples
### Invalid cases
[1, 3, 2].sort(5); // TypeError
var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key] || 'asc'); // TypeError
### Valid cases
[1, 3, 2].sort(); // [1, 2, 3]
var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key || 'asc']); // [1, 2, 3]
## See also
- [`Array.prototype.sort()`](../global_objects/array/sort)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Array_sort_argument
# ArrayBuffer
The `ArrayBuffer` object is used to represent a generic, fixed-length raw binary data buffer.
It is an array of bytes, often referred to in other languages as a "byte array".You cannot directly manipulate the contents of an `ArrayBuffer`; instead, you create one of the [typed array objects](typedarray) or a [`DataView`](dataview) object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
The `ArrayBuffer()` constructor creates a new `ArrayBuffer` of the given length in bytes. You can also get an array buffer from existing data, for example [from a Base64 string](https://developer.mozilla.org/en-US/docs/Glossary/Base64#appendix_to_solution_1_decode_a_base64_string_to_uint8array_or_arraybuffer) or [from a local file](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer).
## Constructor
[`ArrayBuffer()`](arraybuffer/arraybuffer)
Creates a new `ArrayBuffer` object.
## Static properties
[`get ArrayBuffer[@@species]`](arraybuffer/@@species)
The constructor function that is used to create derived objects.
## Static methods
[`ArrayBuffer.isView(arg)`](arraybuffer/isview)
Returns `true` if `arg` is one of the ArrayBuffer views, such as [typed array objects](typedarray) or a [`DataView`](dataview). Returns `false` otherwise.
## Instance properties
[`ArrayBuffer.prototype.byteLength`](arraybuffer/bytelength)
The read-only size, in bytes, of the `ArrayBuffer`. This is established when the array is constructed and cannot be changed.
## Instance methods
[`ArrayBuffer.prototype.slice()`](arraybuffer/slice)
Returns a new `ArrayBuffer` whose contents are a copy of this `ArrayBuffer`'s bytes from `begin` (inclusive) up to `end` (exclusive). If either `begin` or `end` is negative, it refers to an index from the end of the array, as opposed to from the beginning.
## Examples
### Creating an ArrayBuffer
In this example, we create a 8-byte buffer with a [`Int32Array`](int32array) view referring to the buffer:
const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);
## Specifications
`ArrayBuffer`
7
12
4
10
11.6
5.1
4
18
4
12
4.2
1.0
`ArrayBuffer`
7
12
4
10
11.6
5.1
4
18
4
12
4.2
1.0
`byteLength`
7
12
4
10
11.6
5.1
4
18
4
12
4.2
1.0
`isView`
32
12
29
11
19
7
≤37
32
29
19
7
2.0
`slice`
17
12
12
The non-standard `ArrayBuffer.slice()` method has been removed in Firefox 53 (but the standardized version `ArrayBuffer.prototype.slice()` is kept.
11
12.1
6
≤37
18
14
The non-standard `ArrayBuffer.slice()` method has been removed in Firefox 53 (but the standardized version `ArrayBuffer.prototype.slice()` is kept.
12.1
6
1.0
`@@species`
51
13
48
No
38
10
51
51
48
41
10
5.0
## See also
- [JavaScript typed arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
- [`SharedArrayBuffer`](sharedarraybuffer)
- [RangeError: invalid array length](../errors/invalid_array_length)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
# Arrow function expressions
An **arrow function expression** is a compact alternative to a traditional [function expression](../operators/function), but is limited and can't be used in all situations.
**Differences & Limitations:**
- Does not have its own bindings to `this` or `super`, and should not be used as `methods`.
- Does not have `arguments`, or `new.target` keywords.
- Not suitable for `call`, `apply` and [`bind`](../global_objects/function/bind) methods, which generally rely on establishing a [scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope).
- Can not be used as [constructors](https://developer.mozilla.org/en-US/docs/Glossary/Constructor).
- Can not use `yield`, within its body.
### Comparing traditional functions to arrow functions
Let's decompose a "traditional function" down to the simplest "arrow function" step-by-step:
NOTE: Each step along the way is a valid "arrow function"
// Traditional Function
function (a){
return a + 100;
}
// Arrow Function Break Down
// 1. Remove the word "function" and place arrow between the argument and opening body bracket
(a) => {
return a + 100;
}
// 2. Remove the body brackets and word "return" -- the return is implied.
(a) => a + 100;
// 3. Remove the argument parentheses
a => a + 100;
**Note:** As shown above, the { brackets } and ( parentheses ) and "return" are optional, but may be required.
For example, if you have **multiple arguments** or **no arguments**, you'll need to re-introduce parentheses around the arguments:
// Traditional Function
function (a, b){
return a + b + 100;
}
// Arrow Function
(a, b) => a + b + 100;
// Traditional Function (no arguments)
let a = 4;
let b = 2;
function (){
return a + b + 100;
}
// Arrow Function (no arguments)
let a = 4;
let b = 2;
() => a + b + 100;
Likewise, if the body requires **additional lines** of processing, you'll need to re-introduce brackets **PLUS the "return"** (arrow functions do not magically guess what or when you want to "return"):
// Traditional Function
function (a, b){
let chuck = 42;
return a + b + chuck;
}
// Arrow Function
(a, b) => {
let chuck = 42;
return a + b + chuck;
}
And finally, for **named functions** we treat arrow expressions like variables
// Traditional Function
function bob (a){
return a + 100;
}
// Arrow Function
let bob = a => a + 100;
## Syntax
### Basic syntax
One param. With simple expression return is not needed:
param => expression
Multiple params require parentheses. With simple expression return is not needed:
(param1, paramN) => expression
Multiline statements require body brackets and return:
param => {
let a = 1;
return a + param;
}
Multiple params require parentheses. Multiline statements require body brackets and return:
(param1, paramN) => {
let a = 1;
return a + param1 + paramN;
}
### Advanced syntax
To return an object literal expression requires parentheses around expression:
params => ({foo: "a"}) // returning the object {foo: "a"}
[Rest parameters](rest_parameters) are supported:
(a, b, ...r) => expression
[Default parameters](default_parameters) are supported:
(a=400, b=20, c) => expression
[Destructuring](../operators/destructuring_assignment) within params supported:
([a, b] = [10, 20]) => a + b; // result is 30
({ a, b } = { a: 10, b: 20 }) => a + b; // result is 30
## Description
### Arrow functions used as methods
As stated previously, arrow function expressions are best suited for non-method functions. Let's see what happens when we try to use them as methods:
'use strict';
var obj = { // does not create a new scope
i: 10,
b: () => console.log(this.i, this),
c: function() {
console.log(this.i, this);
}
}
obj.b(); // prints undefined, Window {...} (or the global object)
obj.c(); // prints 10, Object {...}
Arrow functions do not have their own `this`. Another example involving [`Object.defineProperty()`](../global_objects/object/defineproperty):
'use strict';
var obj = {
a: 10
};
Object.defineProperty(obj, 'b', {
get: () => {
console.log(this.a, typeof this.a, this); // undefined 'undefined' Window {...} (or the global object)
return this.a + 10; // represents global object 'Window', therefore 'this.a' returns 'undefined'
}
});
### call, apply and bind
The `call`, `apply` and [`bind`](../global_objects/function/bind) methods are **NOT suitable** for Arrow functions -- as they were designed to allow methods to execute within different scopes -- because **Arrow functions establish "this" based on the scope the Arrow function is defined within.**
For example `call`, `apply` and [`bind`](../global_objects/function/bind) work as expected with Traditional functions, because we establish the scope for each of the methods:
// ----------------------
// Traditional Example
// ----------------------
// A simplistic object with its very own "this".
var obj = {
num: 100
}
// Setting "num" on window to show how it is NOT used.
window.num = 2020; // yikes!
// A simple traditional function to operate on "this"
var add = function (a, b, c) {
return this.num + a + b + c;
}
// call
var result = add.call(obj, 1, 2, 3) // establishing the scope as "obj"
console.log(result) // result 106
// apply
const arr = [1, 2, 3]
var result = add.apply(obj, arr) // establishing the scope as "obj"
console.log(result) // result 106
// bind
var result = add.bind(obj) // establishing the scope as "obj"
console.log(result(1, 2, 3)) // result 106
With Arrow functions, since our `add` function is essentially created on the `window` (global) scope, it will assume `this` is the window.
// ----------------------
// Arrow Example
// ----------------------
// A simplistic object with its very own "this".
var obj = {
num: 100
}
// Setting "num" on window to show how it gets picked up.
window.num = 2020; // yikes!
// Arrow Function
var add = (a, b, c) => this.num + a + b + c;
// call
console.log(add.call(obj, 1, 2, 3)) // result 2026
// apply
const arr = [1, 2, 3]
console.log(add.apply(obj, arr)) // result 2026
// bind
const bound = add.bind(obj)
console.log(bound(1, 2, 3)) // result 2026
Perhaps the greatest benefit of using Arrow functions is with DOM-level methods (setTimeout, setInterval, addEventListener) that usually required some kind of closure, call, apply or bind to ensure the function executed in the proper scope.
**Traditional Example:**
var obj = {
count : 10,
doSomethingLater : function (){
setTimeout(function(){ // the function executes on the window scope
this.count++;
console.log(this.count);
}, 300);
}
}
obj.doSomethingLater(); // console prints "NaN", because the property "count" is not in the window scope.
**Arrow Example:**
var obj = {
count : 10,
doSomethingLater : function(){ // of course, arrow functions are not suited for methods
setTimeout( () => { // since the arrow function was created within the "obj", it assumes the object's "this"
this.count++;
console.log(this.count);
}, 300);
}
}
obj.doSomethingLater();
### No binding of `arguments`
Arrow functions do not have their own [`arguments` object](arguments). Thus, in this example, `arguments` is a reference to the arguments of the enclosing scope:
var arguments = [1, 2, 3];
var arr = () => arguments[0];
arr(); // 1
function foo(n) {
var f = () => arguments[0] + n; // foo's implicit arguments binding. arguments[0] is n
return f();
}
foo(3); // 3 + 3 = 6
In most cases, using [rest parameters](rest_parameters) is a good alternative to using an `arguments` object.
function foo(n) {
var f = (...args) => args[0] + n;
return f(10);
}
foo(1); // 11
### Use of the `new` operator
Arrow functions cannot be used as constructors and will throw an error when used with `new`.
var Foo = () => {};
var foo = new Foo(); // TypeError: Foo is not a constructor
### Use of `prototype` property
Arrow functions do not have a `prototype` property.
var Foo = () => {};
console.log(Foo.prototype); // undefined
### Use of the `yield` keyword
The `yield` keyword may not be used in an arrow function's body (except when permitted within functions further nested within it). As a consequence, arrow functions cannot be used as generators.
### Function body
Arrow functions can have either a "concise body" or the usual "block body".
In a concise body, only an expression is specified, which becomes the implicit return value. In a block body, you must use an explicit `return` statement.
var func = x => x * x;
// concise body syntax, implied "return"
var func = (x, y) => { return x + y; };
// with block body, explicit "return" needed
### Returning object literals
Keep in mind that returning object literals using the concise body syntax `params => {object:literal}` will not work as expected.
var func = () => { foo: 1 };
// Calling func() returns undefined!
var func = () => { foo: function() {} };
// SyntaxError: function statement requires a name
This is because the code inside braces ({}) is parsed as a sequence of statements (i.e. `foo` is treated like a label, not a key in an object literal).
You must wrap the object literal in parentheses:
var func = () => ({ foo: 1 });
### Line breaks
An arrow function cannot contain a line break between its parameters and its arrow.
var func = (a, b, c)
=> 1;
// SyntaxError: expected expression, got '=>'
However, this can be amended by putting the line break after the arrow or using parentheses/braces as seen below to ensure that the code stays pretty and fluffy. You can also put line breaks between arguments.
var func = (a, b, c) =>
1;
var func = (a, b, c) => (
1
);
var func = (a, b, c) => {
return 1
};
var func = (
a,
b,
c
) => 1;
// no SyntaxError thrown
### Parsing order
Although the arrow in an arrow function is not an operator, arrow functions have special parsing rules that interact differently with [operator precedence](../operators/operator_precedence) compared to regular functions.
let callback;
callback = callback || function() {}; // ok
callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments
callback = callback || (() => {}); // ok
## Examples
### Basic usage
// An empty arrow function returns undefined
let empty = () => {};
(() => 'foobar')();
// Returns "foobar"
// (this is an Immediately Invoked Function Expression)
var simple = a => a > 15 ? 15 : a;
simple(16); // 15
simple(10); // 10
let max = (a, b) => a > b ? a : b;
// Easy array filtering, mapping, ...
var arr = [5, 6, 13, 0, 1, 18, 23];
var sum = arr.reduce((a, b) => a + b);
// 66
var even = arr.filter(v => v % 2 == 0);
// [6, 0, 18]
var double = arr.map(v => v * 2);
// [10, 12, 26, 0, 2, 36, 46]
// More concise promise chains
promise.then(a => {
// ...
}).then(b => {
// ...
});
// Parameterless arrow functions that are visually easier to parse
setTimeout( () => {
console.log('I happen sooner');
setTimeout( () => {
// deeper code
console.log('I happen later');
}, 1);
}, 1);
## Specifications
`Arrow_functions`
45
12
22
\["The initial implementation of arrow functions in Firefox made them automatically strict. This has been changed as of Firefox 24. The use of `'use strict';` is now required.", "Prior to Firefox 39, a line terminator (`\\n`) was incorrectly allowed after arrow function arguments. This has been fixed to conform to the ES2015 specification and code like `() \\n => {}` will now throw a `SyntaxError` in this and later versions."\]
No
32
10
45
45
22
\["The initial implementation of arrow functions in Firefox made them automatically strict. This has been changed as of Firefox 24. The use of `'use strict';` is now required.", "Prior to Firefox 39, a line terminator (`\\n`) was incorrectly allowed after arrow function arguments. This has been fixed to conform to the ES2015 specification and code like `() \\n => {}` will now throw a `SyntaxError` in this and later versions."\]
32
10
5.0
`trailing_comma`
58
12
52
No
45
10
58
58
52
43
10
7.0
## See also
- ["ES6 In Depth: Arrow functions" on hacks.mozilla.org](https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
# Math.asin()
The `Math.asin()` function returns the arcsine (in radians) of a number, that is
$$\\forall x \\in \\lbrack{- 1};1\\rbrack,\\;\\mathtt{\\operatorname{Math.asin}(x)} = \\arcsin(x) = \\text{the\\ unique}\\; y \\in \\left\\lbrack {- \\frac{\\pi}{2};\\frac{\\pi}{2}} \\right\\rbrack\\,\\text{such\\ that}\\;\\sin(y) = x$$
## Syntax
Math.asin(x)
### Parameters
`x`
A number.
### Return value
The arcsine (in radians) of the given number if it's between **-1** and **1**; otherwise, [`NaN`](../nan).
## Description
The `Math.asin()` method returns a numeric value between $- \\frac{\\pi}{2}$ and $\\frac{\\pi}{2}$ radians for x between -1 and 1. If the value of x is outside this range, it returns [`NaN`](../nan).
Because `asin()` is a static method of `Math`, you always use it as `Math.asin()`, rather than as a method of a `Math` object you created (`Math` is not a constructor).
## Examples
### Using Math.asin()
Math.asin(-2); // NaN
Math.asin(-1); // -1.5707963267948966 (-pi/2)
Math.asin(0); // 0
Math.asin(0.5); // 0.5235987755982989
Math.asin(1); // 1.5707963267948966 (pi/2)
Math.asin(2); // NaN
For values less than -1 or greater than 1, `Math.asin()` returns [`NaN`](../nan).
## Specifications
`asIntN`
67
79
68
No
54
14
67
67
68
48
14
9.0
## See also
- [`BigInt`](../bigint)
- [`BigInt.asUintN()`](asuintn)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
# Object.assign()
The `Object.assign()` method copies all [enumerable](propertyisenumerable) [own properties](hasownproperty) from one or more _source objects_ to a _target object_. It returns the target object.
## Syntax
Object.assign(target, ...sources)
### Parameters
`target`
The target object — what to apply the sources' properties to, which is returned after it is modified.
`sources`
The source object(s) — objects containing the properties you want to apply.
### Return value
The target object.
## Description
Properties in the target object are overwritten by properties in the sources if they have the same [key](keys). Later sources' properties overwrite earlier ones.
The `Object.assign()` method only copies _enumerable_ and _own_ properties from a source object to a target object. It uses `[[Get]]` on the source and `[[Set]]` on the target, so it will invoke [getters](../../functions/get) and [setters](../../functions/set). Therefore it _assigns_ properties, versus copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters.
For copying property definitions (including their enumerability) into prototypes, use [`Object.getOwnPropertyDescriptor()`](getownpropertydescriptor) and [`Object.defineProperty()`](defineproperty) instead.
Both [`String`](../string) and [`Symbol`](../symbol) properties are copied.
In case of an error, for example if a property is non-writable, a [`TypeError`](../typeerror) is raised, and the `target` object is changed if any properties are added before the error is raised.
**Note:** `Object.assign()` does not throw on [`null`](../null) or [`undefined`](../undefined) sources.
## Polyfill
This [polyfill](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill) doesn't support symbol properties, since ES5 doesn't have symbols anyway:
if (typeof Object.assign !== 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
## Examples
### Cloning an object
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
### Warning for Deep Clone
For deep cloning, we need to use alternatives, because `Object.assign()` copies property values.
If the source value is a reference to an object, it only copies the reference value.
function test() {
'use strict';
let obj1 = { a: 0 , b: { c: 0}};
let obj2 = Object.assign({}, obj1);
console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
obj1.a = 1;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
obj2.a = 2;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 0}}
obj2.b.c = 3;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 3}}
console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 3}}
// Deep Clone
obj1 = { a: 0 , b: { c: 0}};
let obj3 = JSON.parse(JSON.stringify(obj1));
obj1.a = 4;
obj1.b.c = 4;
console.log(JSON.stringify(obj3)); // { "a": 0, "b": { "c": 0}}
}
test();
### Merging objects
const o1 = { a: 1 };
const o2 = { b: 2 };
const o3 = { c: 3 };
const obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
### Merging objects with same properties
const o1 = { a: 1, b: 1, c: 1 };
const o2 = { b: 2, c: 2 };
const o3 = { c: 3 };
const obj = Object.assign({}, o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
The properties are overwritten by other objects that have the same properties later in the parameters order.
### Copying symbol-typed properties
const o1 = { a: 1 };
const o2 = { [Symbol('foo')]: 2 };
const obj = Object.assign({}, o1, o2);
console.log(obj); // { a : 1, [Symbol("foo")]: 2 } (cf. bug 1207182 on Firefox)
Object.getOwnPropertySymbols(obj); // [Symbol(foo)]
### Properties on the prototype chain and non-enumerable properties cannot be copied
const obj = Object.create({ foo: 1 }, { // foo is on obj's prototype chain.
bar: {
value: 2 // bar is a non-enumerable property.
},
baz: {
value: 3,
enumerable: true // baz is an own enumerable property.
}
});
const copy = Object.assign({}, obj);
console.log(copy); // { baz: 3 }
### Primitives will be wrapped to objects
const v1 = 'abc';
const v2 = true;
const v3 = 10;
const v4 = Symbol('foo');
const obj = Object.assign({}, v1, null, v2, undefined, v3, v4);
// Primitives will be wrapped, null and undefined will be ignored.
// Note, only string wrappers can have own enumerable properties.
console.log(obj); // { "0": "a", "1": "b", "2": "c" }
### Exceptions will interrupt the ongoing copying task
const target = Object.defineProperty({}, 'foo', {
value: 1,
writable: false
}); // target.foo is a read-only property
Object.assign(target, { bar: 2 }, { foo2: 3, foo: 3, foo3: 3 }, { baz: 4 });
// TypeError: "foo" is read-only
// The Exception is thrown when assigning target.foo
console.log(target.bar); // 2, the first source was copied successfully.
console.log(target.foo2); // 3, the first property of the second source was copied successfully.
console.log(target.foo); // 1, exception is thrown here.
console.log(target.foo3); // undefined, assign method has finished, foo3 will not be copied.
console.log(target.baz); // undefined, the third source will not be copied either.
### Copying accessors
const obj = {
foo: 1,
get bar() {
return 2;
}
};
let copy = Object.assign({}, obj);
console.log(copy);
// { foo: 1, bar: 2 }
// The value of copy.bar is obj.bar's getter's return value.
// This is an assign function that copies full descriptors
function completeAssign(target, ...sources) {
sources.forEach(source => {
let descriptors = Object.keys(source).reduce((descriptors, key) => {
descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
return descriptors;
}, {});
// By default, Object.assign copies enumerable Symbols, too
Object.getOwnPropertySymbols(source).forEach(sym => {
let descriptor = Object.getOwnPropertyDescriptor(source, sym);
if (descriptor.enumerable) {
descriptors[sym] = descriptor;
}
});
Object.defineProperties(target, descriptors);
});
return target;
}
copy = completeAssign({}, obj);
console.log(copy);
// { foo:1, get bar() { return 2 } }
## Specifications
`asUintN`
67
79
68
No
54
14
67
67
68
48
14
9.0
## See also
- [`BigInt`](../bigint)
- [`BigInt.asIntN()`](asintn)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN
# async function
An async function is a function declared with the `async` keyword, and the `await` keyword is permitted within them. The `async` and `await` keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.
Async functions may also be defined [as expressions](../operators/async_function).
## Syntax
async function name([param[, param[, ...param]]]) {
statements
}
### Parameters
`name`
The function's name.
`param`
The name of an argument to be passed to the function.
`statements`
The statements comprising the body of the function. The `await` mechanism may be used.
### Return value
A [`Promise`](../global_objects/promise) which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.
## Description
Async functions can contain zero or more [`await`](../operators/await) expressions. Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression. Use of `async` and `await` enables the use of ordinary `try` / `catch` blocks around asynchronous code.
**Note:** The `await` keyword is only valid inside async functions within regular JavaScript code. If you use it outside of an async function's body, you will get a [`SyntaxError`](../global_objects/syntaxerror).
`await` can be used on its own with [JavaScript modules.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
**Note:** The purpose of `async`/`await` is to simplify the syntax necessary to consume promise-based APIs. The behavior of `async`/`await` is similar to combining [generators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) and promises.
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.
For example, the following:
async function foo() {
return 1
}
...is similar to:
function foo() {
return Promise.resolve(1)
}
**Checking equality with `Promise.resolve` vs `async` return**
Even though the return value of an async function behaves as if it's wrapped in a `Promise.resolve`, they are not equivalent.
An async function will return a different _reference_, whereas `Promise.resolve` returns the same reference if the given value is a promise.
It can be a problem when you want to check the equality of a promise and a return value of an async function.
const p = new Promise((res, rej) => {
res(1);
})
async function asyncReturn() {
return p;
}
function basicReturn() {
return Promise.resolve(p);
}
console.log(p === basicReturn()); // true
console.log(p === asyncReturn()); // false
The body of an async function can be thought of as being split by zero or more await expressions. Top-level code, up to and including the first await expression (if there is one), is run synchronously. In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously.
For example:
async function foo() {
await 1
}
...is equivalent to:
function foo() {
return Promise.resolve(1).then(() => undefined)
}
Code after each await expression can be thought of as existing in a `.then` callback. In this way a promise chain is progressively constructed with each reentrant step through the function. The return value forms the final link in the chain.
In the following example, we successively await two promises. Progress moves through function `foo` in three stages.
1. The first line of the body of function `foo` is executed synchronously, with the await expression configured with the pending promise. Progress through `foo` is then suspended and control is yielded back to the function that called `foo`.
2. Some time later, when the first promise has either been fulfilled or rejected, control moves back into `foo`. The result of the first promise fulfillment (if it was not rejected) is returned from the await expression. Here `1` is assigned to `result1`. Progress continues, and the second await expression is evaluated. Again, progress through `foo` is suspended and control is yielded.
3. Some time later, when the second promise has either been fulfilled or rejected, control re-enters `foo`. The result of the second promise resolution is returned from the second await expression. Here `2` is assigned to `result2`. Control moves to the return expression (if any). The default return value of `undefined` is returned as the resolution value of the current promise.
async function foo() {
const result1 = await new Promise((resolve) => setTimeout(() => resolve('1')))
const result2 = await new Promise((resolve) => setTimeout(() => resolve('2')))
}
foo()
Note how the promise chain is not built-up in one go. Instead, the promise chain is constructed in stages as control is successively yielded from and returned to the async function. As a result, we must be mindful of error handling behavior when dealing with concurrent asynchronous operations.
For example, in the following code an unhandled promise rejection error will be thrown, even if a `.catch` handler has been configured further along the promise chain. This is because `p2` will not be "wired into" the promise chain until control returns from `p1`.
async function foo() {
const p1 = new Promise((resolve) => setTimeout(() => resolve('1'), 1000))
const p2 = new Promise((_,reject) => setTimeout(() => reject('2'), 500))
const results = [await p1, await p2] // Do not do this! Use Promise.all or Promise.allSettled instead.
}
foo().catch(() => {}) // Attempt to swallow all errors...
## Examples
### Async functions and execution order
function resolveAfter2Seconds() {
console.log("starting slow promise")
return new Promise(resolve => {
setTimeout(function() {
resolve("slow")
console.log("slow promise is done")
}, 2000)
})
}
function resolveAfter1Second() {
console.log("starting fast promise")
return new Promise(resolve => {
setTimeout(function() {
resolve("fast")
console.log("fast promise is done")
}, 1000)
})
}
async function sequentialStart() {
console.log('==SEQUENTIAL START==')
// 1. Execution gets here almost instantly
const slow = await resolveAfter2Seconds()
console.log(slow) // 2. this runs 2 seconds after 1.
const fast = await resolveAfter1Second()
console.log(fast) // 3. this runs 3 seconds after 1.
}
async function concurrentStart() {
console.log('==CONCURRENT START with await==');
const slow = resolveAfter2Seconds() // starts timer immediately
const fast = resolveAfter1Second() // starts timer immediately
// 1. Execution gets here almost instantly
console.log(await slow) // 2. this runs 2 seconds after 1.
console.log(await fast) // 3. this runs 2 seconds after 1., immediately after 2., since fast is already resolved
}
function concurrentPromise() {
console.log('==CONCURRENT START with Promise.all==')
return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then((messages) => {
console.log(messages[0]) // slow
console.log(messages[1]) // fast
})
}
async function parallel() {
console.log('==PARALLEL with await Promise.all==')
// Start 2 "jobs" in parallel and wait for both of them to complete
await Promise.all([
(async()=>console.log(await resolveAfter2Seconds()))(),
(async()=>console.log(await resolveAfter1Second()))()
])
}
sequentialStart() // after 2 seconds, logs "slow", then after 1 more second, "fast"
// wait above to finish
setTimeout(concurrentStart, 4000) // after 2 seconds, logs "slow" and then "fast"
// wait again
setTimeout(concurrentPromise, 7000) // same as concurrentStart
// wait again
setTimeout(parallel, 10000) // truly parallel: after 1 second, logs "fast", then after 1 more second, "slow"
#### await and parallelism
In `sequentialStart`, execution suspends 2 seconds for the first `await`, and then another second for the second `await`. The second timer is not created until the first has already fired, so the code finishes after 3 seconds.
In `concurrentStart`, both timers are created and then `await`ed. The timers run concurrently, which means the code finishes in 2 rather than 3 seconds, i.e. the slowest timer.
However, the `await` calls still run in series, which means the second `await` will wait for the first one to finish. In this case, the result of the fastest timer is processed after the slowest.
If you wish to safely perform two or more jobs in parallel, you must await a call to `Promise.all`, or `Promise.allSettled`.
**Warning:** The functions `concurrentStart` and `concurrentPromise` are not functionally equivalent.
In `concurrentStart`, if promise `fast` rejects before promise `slow` is fulfilled, then an unhandled promise rejection error will be raised, regardless of whether the caller has configured a catch clause.
In `concurrentPromise,` `Promise.all` wires up the promise chain in one go, meaning that the operation will fail-fast regardless of the order of rejection of the promises, and the error will always occur within the configured promise chain, enabling it to be caught in the normal way.
### Rewriting a Promise chain with an async function
An API that returns a [`Promise`](../global_objects/promise) will result in a promise chain, and it splits the function into many parts. Consider the following code:
function getProcessedData(url) {
return downloadData(url) // returns a promise
.catch(e => {
return downloadFallbackData(url) // returns a promise
})
.then(v => {
return processDataInWorker(v) // returns a promise
})
}
it can be rewritten with a single async function as follows:
async function getProcessedData(url) {
let v
try {
v = await downloadData(url)
} catch(e) {
v = await downloadFallbackData(url)
}
return processDataInWorker(v)
}
In the above example, notice there is no `await` statement after the `return` keyword, although that would be valid too: The return value of an `async function` is implicitly wrapped in [`Promise.resolve`](../global_objects/promise/resolve) - if it's not already a promise itself (as in this example).
**Note:** The implicit wrapping of return values in [`Promise.resolve`](../global_objects/promise/resolve) does not imply that `return await promiseValue` is functionally equivalent to `return promiseValue`.
Consider the following rewrite of the above code. It returns `null` if `processDataInWorker` rejects with an error:
async function getProcessedData(url) {
let v
try {
v = await downloadData(url)
} catch(e) {
v = await downloadFallbackData(url)
}
try {
return await processDataInWorker(v) // Note the `return await` vs. just `return`
} catch (e) {
return null
}
}
Writing `return processDataInWorker(v)` would have caused the [`Promise`](../global_objects/promise) returned by the function to reject, instead of resolving to `null` if `processDataInWorker(v)` rejects.
This highlights the subtle difference between `return foo;` and `return await foo;` — `return foo` immediately returns `foo` and never throws, even if `foo` is a Promise that rejects. `return await foo` will _wait_ for `foo` to resolve or reject if it's a Promise, and throws **before returning** if it rejects.
## Specifications
`AsyncFunction`
55
15
52
No
42
10.1
55
55
52
42
10.3
6.0
## See also
- [async function function](../statements/async_function)
- [async function expression](../operators/async_function)
- [`Function`](function)
- [function statement](../statements/function)
- [function expression](../operators/function)
- [Functions and function scope](../functions)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
# Symbol.asyncIterator
The `Symbol.asyncIterator` well-known symbol specifies the default AsyncIterator for an object. If this property is set on an object, it is an async iterable and can be used in a `for await...of` loop.
## Description
The `Symbol.asyncIterator` symbol is a builtin symbol that is used to access an object's `@@asyncIterator` method. In order for an object to be async iterable, it must have a `Symbol.asyncIterator` key.
Property attributes of `Symbol.asyncIterator`
Writable
no
Enumerable
no
Configurable
no
## Examples
### User-defined Async Iterables
You can define your own async iterable by setting the `[Symbol.asyncIterator]` property on an object.
const myAsyncIterable = {
async* [Symbol.asyncIterator]() {
yield "hello";
yield "async";
yield "iteration!";
}
};
(async () => {
for await (const x of myAsyncIterable) {
console.log(x);
// expected output:
// "hello"
// "async"
// "iteration!"
}
})();
When creating an API, remember that async iterables are designed to represent something _iterable_ — like a stream of data or a list —, not to completely replace callbacks and events in most situations.
### Built-in Async Iterables
There are currently no built-in JavaScript objects that have the `[Symbol.asyncIterator]` key set by default. However, WHATWG Streams are set to be the first built-in object to be async iterable, with `[Symbol.asyncIterator]` recently landing in the spec.
## Specifications
`Atomics`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`Atomic_operations_on_non-shared_buffers`
No
No
79
No
No
No
No
No
79
No
No
No
`add`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`and`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`compareExchange`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`exchange`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`isLockFree`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`load`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`notify`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
78
63
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
48-55
46-48
The `count` parameter defaults to `0` instead of the later-specified `+Infinity`.
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
63
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
48-55
46-48
The `count` parameter defaults to `0` instead of the later-specified `+Infinity`.
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`or`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`store`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`sub`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`wait`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
48-55
46-48
The method returns values `Atomics.OK`, `Atomics.TIMEDOUT`, and `Atomics.NOTEQUAL`, instead of the later-specified strings.
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
48-55
46-48
The method returns values `Atomics.OK`, `Atomics.TIMEDOUT`, and `Atomics.NOTEQUAL`, instead of the later-specified strings.
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
`xor`
68
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This was a temporary removal while mitigations were put in place.
79
16-17
Support was removed to mitigate [speculative execution side-channel attacks (Windows blog)](https://blogs.windows.com/msedgedev/2018/01/03/speculative-execution-mitigations-microsoft-edge-internet-explorer).
78
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
No
10.1-11.1
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
60-63
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
57
Support was disabled by default to mitigate [speculative execution side-channel attacks (Mozilla Security Blog)](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/).
55-57
46-55
No
10.3-11.3
No
Chrome disabled `SharedArrayBuffer` on January 5, 2018 to help reduce the efficacy of [speculative side-channel attacks](https://www.chromium.org/Home/chromium-security/ssca). This is intended as a temporary measure until other mitigations are in place.
## See also
- [`ArrayBuffer`](arraybuffer)
- [JavaScript typed arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
- [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)
- [parlib-simple](https://github.com/lars-t-hansen/parlib-simple) – a simple library providing synchronization and work distribution abstractions.
- [Shared Memory – a brief tutorial](https://github.com/tc39/ecmascript_sharedmem/blob/master/TUTORIAL.md)
- [A Taste of JavaScript's New Parallel Primitives – Mozilla Hacks](https://hacks.mozilla.org/2016/05/a-taste-of-javascripts-new-parallel-primitives/)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics
# await
The `await` operator is used to wait for a [`Promise`](../global_objects/promise). It can only be used inside an [`async function`](../statements/async_function) within regular JavaScript code; however it can be used on its own with [JavaScript modules.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
## Syntax
[rv] = await expression;
`expression`
A [`Promise`](../global_objects/promise) or any value to wait for.
`rv`
Returns the fulfilled value of the promise, or the value itself if it's not a `Promise`.
## Description
The `await` expression causes `async` function execution to pause until a `Promise` is settled (that is, fulfilled or rejected), and to resume execution of the `async` function after fulfillment. When resumed, the value of the `await` expression is that of the fulfilled `Promise`.
If the `Promise` is rejected, the `await` expression throws the rejected value.
If the value of the _expression_ following the `await` operator is not a `Promise`, it's converted to a [resolved Promise](../global_objects/promise/resolve).
An `await` splits execution flow, allowing the caller of the async function to resume execution. After the `await` defers the continuation of the async function, execution of subsequent statements ensues. If this `await` is the last expression executed by its function, execution continues by returning to the function's caller a pending `Promise` for completion of the `await`'s function and resuming execution of that caller.
## Examples
### Awaiting a promise to be fulfilled
If a `Promise` is passed to an `await` expression, it waits for the `Promise` to be fulfilled and returns the fulfilled value.
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
var x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
### Thenable objects
[`Thenable objects`](../global_objects/promise/then) will be fulfilled just the same.
async function f2() {
const thenable = {
then: function(resolve, _reject) {
resolve('resolved!')
}
};
console.log(await thenable); // resolved!
}
f2();
### Conversion to promise
If the value is not a `Promise`, it converts the value to a resolved `Promise`, and waits for it.
async function f3() {
var y = await 20;
console.log(y); // 20
}
f3();
### Promise rejection
If the `Promise` is rejected, the rejected value is thrown.
async function f4() {
try {
var z = await Promise.reject(30);
} catch(e) {
console.error(e); // 30
}
}
f4();
### Handling rejected promises
Handle rejected `Promise` without try block.
var response = await promisedFunction().catch((err) => { console.error(err); });
// response will be undefined if the promise is rejected
### Top level await
You can use the `await` keyword on its own (outside of an async function) within a [JavaScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). This means modules, with child modules that use `await`, wait for the child module to execute before they themselves run. All while not blocking other child modules from loading.
Here is an example of a simple module using the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and specifying await within the `export statement`. Any modules that include this will wait for the fetch to resolve before running any code.
// fetch request
const colors = fetch('../data/colors.json')
.then(response => response.json());
export default await colors;
# Warning: 08/09 is not a legal ECMA-262 octal constant
The JavaScript warning "08 (or 09) is not a legal ECMA-262 octal constant" occurs when `08` or `09` number literals are used. They can't be interpreted as an octal number.
## Message
Warning: SyntaxError: 08 is not a legal ECMA-262 octal constant.
Warning: SyntaxError: 09 is not a legal ECMA-262 octal constant.
## Error type
Warning. JavaScript execution won't be halted.
## What went wrong?
Decimal literals can start with a zero (`0`) followed by another decimal digit, but If all digits after the leading `0` are smaller than 8, the number is interpreted as an octal number. Because this is not the case with `08` and `09`, JavaScript warns about it.
Note that octal literals and octal escape sequences are deprecated and will present an additional deprecation warning. With ECMAScript 6 and later, the syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (`0o` or `0O)`. See the page about [lexical grammar](../lexical_grammar#octal) for more information.
## Examples
### Invalid octal numbers
08;
09;
// SyntaxError: 08 is not a legal ECMA-262 octal constant
// SyntaxError: "0"-prefixed octal literals and octal escape sequences
// are deprecated
### Valid octal numbers
Use a leading zero followed by the letter "o";
0O755;
0o644;
## See also
- [Lexical grammar](../lexical_grammar#octal)
- [SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated](deprecated_octal)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_octal
# RangeError: radix must be an integer
The JavaScript exception "radix must be an integer at least 2 and no greater than 36" occurs when the optional `radix` parameter of the [`Number.prototype.toString()`](../global_objects/number/tostring) or the [`BigInt.prototype.toString()`](../global_objects/bigint/tostring) method was specified and is not between 2 and 36.
## Message
RangeError: invalid argument (Edge)
RangeError: radix must be an integer at least 2 and no greater than 36 (Firefox)
RangeError: toString() radix argument must be between 2 and 36 (Chrome)
## Error type
[`RangeError`](../global_objects/rangeerror)
## What went wrong?
The optional `radix` parameter of the [`Number.prototype.toString()`](../global_objects/number/tostring) or the [`BigInt.prototype.toString()`](../global_objects/bigint/tostring) method was specified. Its value must be an integer (a number) between 2 and 36, specifying the base of the number system to be used for representing numeric values. For example, the decimal (base 10) number 169 is represented in hexadecimal (base 16) as A9.
Why is this parameter's value limited to 36? A radix that is larger than 10 uses alphabetical characters as digits; therefore, the radix can't be larger than 36, since the Latin alphabet (used by English and many other languages) only has 26 characters.
The most common radixes:
- 2 for [binary numbers](https://en.wikipedia.org/wiki/Binary_number),
- 8 for [octal numbers](https://en.wikipedia.org/wiki/Octal),
- 10 for [decimal numbers](https://en.wikipedia.org/wiki/Decimal),
- 16 for [hexadecimal numbers](https://en.wikipedia.org/wiki/Hexadecimal).
## Examples
### Invalid cases
(42).toString(0);
(42).toString(1);
(42).toString(37);
(42).toString(150);
// You cannot use a string like this for formatting:
(12071989).toString('MM-dd-yyyy');
### Valid cases
(42).toString(2); // "101010" (binary)
(13).toString(8); // "15" (octal)
(0x42).toString(10); // "66" (decimal)
(100000).toString(16) // "186a0" (hexadecimal)
## See also
- [`Number.prototype.toString()`](../global_objects/number/tostring)
- [`BigInt.prototype.toString()`](../global_objects/bigint/tostring)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_radix
# SyntaxError: invalid regular expression flag "x"
The JavaScript exception "invalid regular expression flag" occurs when the flags, defined after the second slash in regular expression literal, are not one of `g`, `i`, `m`, `s`, `u`, or `y`.
## Message
SyntaxError: Syntax error in regular expression (Edge)
SyntaxError: invalid regular expression flag "x" (Firefox)
SyntaxError: Invalid regular expression flags (Chrome)
## Error type
[`SyntaxError`](../global_objects/syntaxerror)
## What went wrong?
There are invalid regular expression flags in the code. In a regular expression literal, which consists of a pattern enclosed between slashes, the flags are defined after the second slash. They can also be defined in the constructor function of the [`RegExp`](../global_objects/regexp) object (second parameter). Regular expression flags can be used separately or together in any order, but there are only six of them in ECMAScript.
To include a flag with the regular expression, use this syntax:
var re = /pattern/flags;
or
var re = new RegExp('pattern', 'flags');
Regular expression flags
Flag
Description
g
Global search.
i
Case-insensitive search.
m
Multi-line search.
s
Allow . to match newlines (added in ECMAScript 2018)
u
Unicode; treat pattern as a sequence of Unicode code points
y
Perform a "sticky" search that matches starting at the current position in the target string. See sticky
## Examples
There are only six valid regular expression flags.
/foo/bar;
// SyntaxError: invalid regular expression flag "b"
Did you intend to create a regular expression? An expression containing two slashes is interpreted as a regular expression literal.
let obj = {
url: /docs/Web
};
// SyntaxError: invalid regular expression flag "W"
Or did you mean to create a string instead? Add single or double quotes to create a string literal.
let obj = {
url: '/docs/Web'
};
### Valid regular expression flags
See the table above for the six valid regular expression flags that are allowed in JavaScript.
/foo/g;
/foo/gims;
/foo/uy;
## See also
- [Regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
- [XRegEx flags](https://xregexp.com/flags/) – regular expression library that provides four new flags (`n`, `s`, `x`, `A`)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_regexp_flag
# SyntaxError: return not in function
The JavaScript exception "return (or yield) not in function" occurs when a `return` or `yield` statement is called outside of a [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions).
## Message
SyntaxError: 'return' statement outside of function (Edge)
SyntaxError: return not in function (Firefox)
SyntaxError: yield not in function (Firefox)
## Error type
[`SyntaxError`](../global_objects/syntaxerror).
## What went wrong?
A `return` or `yield` statement is called outside of a [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions). Maybe there are missing curly brackets somewhere? The `return` and `yield` statements must be in a function, because they end (or pause and resume) function execution and specify a value to be returned to the function caller.
## Examples
### Missing curly brackets
var cheer = function(score) {
if (score === 147)
return 'Maximum!';
};
if (score > 100) {
return 'Century!';
}
}
// SyntaxError: return not in function
The curly brackets look correct at a first glance, but this code snippet is missing a `{` after the first `if` statement. Correct would be:
var cheer = function(score) {
if (score === 147) {
return 'Maximum!';
}
if (score > 100) {
return 'Century!';
}
};
## See also
- `return`
- `yield`
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield
# Intl.Locale.prototype.baseName
The `Intl.Locale.prototype.baseName` property returns a substring of the `Locale`'s string representation, containing core information about the `Locale`.
## Description
An [`Intl.Locale`](../locale) object represents a parsed local and options for that locale. The `baseName` property returns basic, core information about the Locale in the form of a substring of the complete data string. Specifically, the property returns the substring containing the language, and the script and region if available.
`baseName` returns the `language ["-" script] ["-" region] *("-" variant)` subsequence of the [unicode_language_id grammar](https://www.unicode.org/reports/tr35/#Identifiers).
## Examples
### Basic Example
let myLoc = new Intl.Locale("fr-Latn-CA"); // Sets locale to Canadian French
console.log(myLoc.toString()); // Prints out "fr-Latn-CA-u-ca-gregory"
console.log(myLoc.baseName); // Prints out "fr-Latn-CA"
### Example with options in the input string
// Sets language to Japanese, region to Japan,
// calendar to Gregorian, hour cycle to 24 hours
let japan = new Intl.Locale("ja-JP-u-ca-gregory-hc-24");
console.log(japan.toString()); // Prints out "ja-JP-u-ca-gregory-hc-h24"
console.log(japan.baseName); // Prints out "ja-JP"
### Example with options that override input string
// Input string indicates language as Dutch and region as Belgium,
// but options object overrides the region and sets it to the Netherlands
let dutch = new Intl.Locale("nl-Latn-BE", {region: "NL"});
console.log(dutch.baseName); // Prints out "nl-Latn-NL"
## Specifications
`baseName`
74
79
75
No
62
14
74
74
79
53
14
11.0
## See also
- [`Intl.Locale`](../locale)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/baseName
# String.prototype.big()
**Deprecated**
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the [compatibility table](#browser_compatibility) at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The `big()` method creates a [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/big) HTML element that causes a string to be displayed in a big font.
**Note:** The <big> element has been removed in [HTML5](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5) and shouldn't be used anymore. Instead web developers should use [CSS](https://developer.mozilla.org/en-US/docs/Web/CSS) properties.
## Syntax
big()
### Return value
A string containing a [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/big) HTML element.
## Description
The `big()` method embeds a string in a `` element: "`str`".
## Examples
### Using big()
The following example uses string methods to change the size of a string:
var worldString = 'Hello, world';
console.log(worldString.small()); // Hello, world
console.log(worldString.big()); // Hello, world
console.log(worldString.fontsize(7)); // Hello, world
With the [`element.style`](https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style) object you can get the element's `style` attribute and manipulate it more generically, for example:
document.getElementById('yourElemId').style.fontSize = '2em';
## Specifications
`BigInt`
67
79
68
No
54
14
67
67
68
48
14
9.0
`BigInt`
67
79
68
No
54
14
67
67
68
48
14
9.0
`asIntN`
67
79
68
No
54
14
67
67
68
48
14
9.0
`asUintN`
67
79
68
No
54
14
67
67
68
48
14
9.0
`toLocaleString`
67
79
68
No
54
14
67
67
68
48
14
9.0
`toString`
67
79
68
No
54
14
67
67
68
48
14
9.0
`valueOf`
67
79
68
No
54
14
67
67
68
48
14
9.0
## See also
- [`Number`](number)
- [`Number.MAX_SAFE_INTEGER`](number/max_safe_integer)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
# BigInt64Array
The `BigInt64Array` typed array represents an array of 64-bit signed integers in the platform byte order. If control over byte order is needed, use [`DataView`](dataview) instead. The contents are initialized to `0n`. Once established, you can reference elements in the array using the object's methods, or by using standard array index syntax (that is, using bracket notation).
## Constructor
[`BigInt64Array()`](bigint64array/bigint64array)
Creates a new `BigInt64Array` object.
## Static properties
[`BigInt64Array.BYTES_PER_ELEMENT`](typedarray/bytes_per_element)
Returns a number value of the element size. `8` in the case of a `BigInt64Array`.
[`BigInt64Array.name`](typedarray/name)
Returns the string value of the constructor name. In the case of the `BigInt64Array` type, this is "BigInt64Array".
## Static methods
[`BigInt64Array.from()`](typedarray/from)
Creates a new `BigInt64Array` from an array-like or iterable object. See also [`Array.from()`](array/from).
[`BigInt64Array.of()`](typedarray/of)
Creates a new `BigInt64Array` with a variable number of arguments. See also [`Array.of()`](array/of).
## Instance properties
[`BigInt64Array.prototype.buffer`](typedarray/buffer)
Returns the [`ArrayBuffer`](arraybuffer) referenced by the `BigInt64Array`. This is fixed at construction time and thus **read only**.
[`BigInt64Array.prototype.byteLength`](typedarray/bytelength)
Returns the length (in bytes) of the `BigInt64Array` from the start of its [`ArrayBuffer`](arraybuffer). This is fixed at construction time and thus **read only.**
[`BigInt64Array.prototype.byteOffset`](typedarray/byteoffset)
Returns the offset (in bytes) of the `BigInt64Array` from the start of its [`ArrayBuffer`](arraybuffer). This is fixed at construction time and thus **read only.**
[`BigInt64Array.prototype.length`](typedarray/length)
Returns the number of elements hold in the `BigInt64Array`. This is fixed at construction time and thus **read only.**
## Instance methods
[`BigInt64Array.prototype.copyWithin()`](typedarray/copywithin)
Copies a sequence of array elements within the array. See also [`Array.prototype.copyWithin()`](array/copywithin).
[`BigInt64Array.prototype.entries()`](typedarray/entries)
Returns a new `Array Iterator` object that contains the key/value pairs for each index in the array. See also [`Array.prototype.entries()`](array/entries).
[`BigInt64Array.prototype.every()`](typedarray/every)
Tests whether all elements in the array pass the test provided by a function. See also [`Array.prototype.every()`](array/every).
[`BigInt64Array.prototype.fill()`](typedarray/fill)
Fills all the elements of an array from a start index to an end index with a static value. See also [`Array.prototype.fill()`](array/fill).
[`BigInt64Array.prototype.filter()`](typedarray/filter)
Creates a new array with all of the elements of this array for which the provided filtering function returns true. See also [`Array.prototype.filter()`](array/filter).
[`BigInt64Array.prototype.find()`](typedarray/find)
Returns the found value in the array if an element in the array satisfies the provided testing function, or `undefined` if not found. See also [`Array.prototype.find()`](array/find).
[`BigInt64Array.prototype.findIndex()`](typedarray/findindex)
Returns the found index in the array if an element in the array satisfies the provided testing function, or -1 if not found. See also [`Array.prototype.findIndex()`](array/findindex).
[`BigInt64Array.prototype.forEach()`](typedarray/foreach)
Calls a function for each element in the array. See also [`Array.prototype.forEach()`](array/foreach).
[`BigInt64Array.prototype.includes()`](typedarray/includes)
Determines whether a typed array includes a certain element, returning `true` or `false` as appropriate. See also [`Array.prototype.includes()`](array/includes).
[`BigInt64Array.prototype.indexOf()`](typedarray/indexof)
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also [`Array.prototype.indexOf()`](array/indexof).
[`BigInt64Array.prototype.join()`](typedarray/join)
Joins all elements of an array into a string. See also [`Array.prototype.join()`](array/join).
[`BigInt64Array.prototype.keys()`](typedarray/keys)
Returns a new `Array Iterator` that contains the keys for each index in the array. See also [`Array.prototype.keys()`](array/keys).
[`BigInt64Array.prototype.lastIndexOf()`](typedarray/lastindexof)
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also [`Array.prototype.lastIndexOf()`](array/lastindexof).
[`BigInt64Array.prototype.map()`](typedarray/map)
Creates a new array with the results of calling a provided function on every element in this array. See also [`Array.prototype.map()`](array/map).
[`BigInt64Array.prototype.reduce()`](typedarray/reduce)
Applies a function against an accumulator and each value of the array (from left-to-right) so as to reduce it to a single value. See also [`Array.prototype.reduce()`](array/reduce).
[`BigInt64Array.prototype.reduceRight()`](typedarray/reduceright)
Applies a function against an accumulator and each value of the array (from right-to-left) so as to reduce it to a single value. See also [`Array.prototype.reduceRight()`](array/reduceright).
[`BigInt64Array.prototype.reverse()`](typedarray/reverse)
Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also [`Array.prototype.reverse()`](array/reverse).
[`BigInt64Array.prototype.set()`](typedarray/set)
Stores multiple values in the typed array, reading input values from a specified array.
[`BigInt64Array.prototype.slice()`](typedarray/slice)
Extracts a section of an array and returns a new array. See also [`Array.prototype.slice()`](array/slice).
[`BigInt64Array.prototype.some()`](typedarray/some)
Returns true if at least one element in this array satisfies the provided testing function. See also [`Array.prototype.some()`](array/some).
[`BigInt64Array.prototype.sort()`](typedarray/sort)
Sorts the elements of an array in place and returns the array. See also [`Array.prototype.sort()`](array/sort).
[`BigInt64Array.prototype.subarray()`](typedarray/subarray)
Returns a new `BigUint64Array` from the given start and end element index.
[`BigInt64Array.prototype.values()`](typedarray/values)
Returns a new `Array Iterator` object that contains the values for each index in the array. See also [`Array.prototype.values()`](array/values).
[`BigInt64Array.prototype.toLocaleString()`](typedarray/tolocalestring)
Returns a localized string representing the array and its elements. See also [`Array.prototype.toLocaleString()`](array/tolocalestring).
[`BigInt64Array.prototype.toString()`](typedarray/tostring)
Returns a string representing the array and its elements. See also [`Array.prototype.toString()`](array/tostring).
[`BigInt64Array.prototype[@@iterator]()`](typedarray/@@iterator)
Returns a new `Array Iterator` object that contains the values for each index in the array.
## Examples
### Different ways to create a `BigInt64Array`
// From a length
var bigint64 = new BigInt64Array(2);
bigint64[0] = 42n;
console.log(bigint64[0]); // 42n
console.log(bigint64.length); // 2
console.log(bigint64.BYTES_PER_ELEMENT); // 8
// From an array
var arr = new BigInt64Array([21n,31n]);
console.log(arr[1]); // 31n
// From another TypedArray
var x = new BigInt64Array([21n, 31n]);
var y = new BigInt64Array(x);
console.log(y[0]); // 21n
// From an ArrayBuffer
var buffer = new ArrayBuffer(32);
var z = new BigInt64Array(buffer, 0, 4);
// From an iterable
var iterable = function*(){ yield* [1n, 2n, 3n]; }();
var bigint64 = new BigInt64Array(iterable);
// BigInt64Array[1n, 2n, 3n]
## Specifications
`BigInt64Array`
67
79
68
No
54
No
67
67
68
48
No
9.0
`BigInt64Array`
67
79
68
No
54
No
67
67
68
48
No
9.0
## See also
- [JavaScript typed arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
- [`BigUint64Array`](biguint64array)
- [`DataView`](dataview)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array
# BigUint64Array
The `BigUint64Array` typed array represents an array of 64-bit unsigned integers in the platform byte order. If control over byte order is needed, use [`DataView`](dataview) instead. The contents are initialized to `0n`. Once established, you can reference elements in the array using the object's methods, or by using standard array index syntax (that is, using bracket notation).
## Constructor
[`BigUint64Array()`](biguint64array/biguint64array)
Creates a new `BigUint64Array` object.
## Static properties
[`BigUint64Array.BYTES_PER_ELEMENT`](typedarray/bytes_per_element)
Returns a number value of the element size. `8` in the case of a `BigUint64Array`.
[`BigUint64Array.name`](typedarray/name)
Returns the string value of the constructor name. In the case of the `BigUint64Array` type this is "BigUint64Array".
## Static methods
[`BigUint64Array.from()`](typedarray/from)
Creates a new `BigUint64Array` from an array-like or iterable object. See also [`Array.from()`](array/from).
[`BigUint64Array.of()`](typedarray/of)
Creates a new `BigUint64Array` with a variable number of arguments. See also [`Array.of()`](array/of).
## Instance properties
[`BigUint64Array.prototype.buffer`](typedarray/buffer)
Returns the [`ArrayBuffer`](arraybuffer) referenced by the `BigUint64Array`. This is fixed at construction time and thus **read only**.
[`BigUint64Array.prototype.byteLength`](typedarray/bytelength)
Returns the length (in bytes) of the `BigUint64Array` from the start of its [`ArrayBuffer`](arraybuffer). This is fixed at construction time and thus **read only.**
[`BigUint64Array.prototype.byteOffset`](typedarray/byteoffset)
Returns the offset (in bytes) of the `BigUint64Array` from the start of its [`ArrayBuffer`](arraybuffer). This is fixed at construction time and thus **read only.**
[`BigUint64Array.prototype.length`](typedarray/length)
Returns the number of elements hold in the `BigUint64Array`. This is fixed at construction time and thus **read only.**
## Instance methods
[`BigUint64Array.prototype.copyWithin()`](typedarray/copywithin)
Copies a sequence of array elements within the array. See also [`Array.prototype.copyWithin()`](array/copywithin).
[`BigUint64Array.prototype.entries()`](typedarray/entries)
Returns a new `Array Iterator` object that contains the key/value pairs for each index in the array. See also [`Array.prototype.entries()`](array/entries).
[`BigUint64Array.prototype.every()`](typedarray/every)
Tests whether all elements in the array pass the test provided by a function. See also [`Array.prototype.every()`](array/every).
[`BigUint64Array.prototype.fill()`](typedarray/fill)
Fills all the elements of an array from a start index to an end index with a static value. See also [`Array.prototype.fill()`](array/fill).
[`BigUint64Array.prototype.filter()`](typedarray/filter)
Creates a new array with all of the elements of this array for which the provided filtering function returns true. See also [`Array.prototype.filter()`](array/filter).
[`BigUint64Array.prototype.find()`](typedarray/find)
Returns the found value in the array if an element in the array satisfies the provided testing function, or `undefined` if not found. See also [`Array.prototype.find()`](array/find).
[`BigUint64Array.prototype.findIndex()`](typedarray/findindex)
Returns the found index in the array if an element in the array satisfies the provided testing function, or -1 if not found. See also [`Array.prototype.findIndex()`](array/findindex).
[`BigUint64Array.prototype.forEach()`](typedarray/foreach)
Calls a function for each element in the array. See also [`Array.prototype.forEach()`](array/foreach).
[`BigUint64Array.prototype.includes()`](typedarray/includes)
Determines whether a typed array includes a certain element, returning `true` or `false` as appropriate. See also [`Array.prototype.includes()`](array/includes).
[`BigUint64Array.prototype.indexOf()`](typedarray/indexof)
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also [`Array.prototype.indexOf()`](array/indexof).
[`BigUint64Array.prototype.join()`](typedarray/join)
Joins all elements of an array into a string. See also [`Array.prototype.join()`](array/join).
[`BigUint64Array.prototype.keys()`](typedarray/keys)
Returns a new `Array Iterator` that contains the keys for each index in the array. See also [`Array.prototype.keys()`](array/keys).
[`BigUint64Array.prototype.lastIndexOf()`](typedarray/lastindexof)
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also [`Array.prototype.lastIndexOf()`](array/lastindexof).
[`BigUint64Array.prototype.map()`](typedarray/map)
Creates a new array with the results of calling a provided function on every element in this array. See also [`Array.prototype.map()`](array/map).
[`BigUint64Array.prototype.reduce()`](typedarray/reduce)
Apply a function against an accumulator and each value of the array (from left-to-right) so as to reduce it to a single value. See also [`Array.prototype.reduce()`](array/reduce).
[`BigUint64Array.prototype.reduceRight()`](typedarray/reduceright)
Applies a function against an accumulator and each value of the array (from right-to-left) so as to reduce it to a single value. See also [`Array.prototype.reduceRight()`](array/reduceright).
[`BigUint64Array.prototype.reverse()`](typedarray/reverse)
Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also [`Array.prototype.reverse()`](array/reverse).
[`BigUint64Array.prototype.set()`](typedarray/set)
Stores multiple values in the typed array, reading input values from a specified array.
[`BigUint64Array.prototype.slice()`](typedarray/slice)
Extracts a section of an array and returns a new array. See also [`Array.prototype.slice()`](array/slice).
[`BigUint64Array.prototype.some()`](typedarray/some)
Returns `true` if at least one element in this array satisfies the provided testing function. See also [`Array.prototype.some()`](array/some).
[`BigUint64Array.prototype.sort()`](typedarray/sort)
Sorts the elements of an array in place and returns the array. See also [`Array.prototype.sort()`](array/sort).
[`BigUint64Array.prototype.subarray()`](typedarray/subarray)
Returns a new `BigUint64Array` from the given start and end element index.
[`BigUint64Array.prototype.values()`](typedarray/values)
Returns a new `Array Iterator` object that contains the values for each index in the array. See also [`Array.prototype.values()`](array/values).
[`BigUint64Array.prototype.toLocaleString()`](typedarray/tolocalestring)
Returns a localized string representing the array and its elements. See also [`Array.prototype.toLocaleString()`](array/tolocalestring).
[`BigUint64Array.prototype.toString()`](typedarray/tostring)
Returns a string representing the array and its elements. See also [`Array.prototype.toString()`](array/tostring).
[`BigUint64Array.prototype[@@iterator]()`](typedarray/@@iterator)
Returns a new `Array Iterator` object that contains the values for each index in the array.
## Examples
### Different ways to create a `BigUint64Array`
// From a length
var biguint64 = new BigUint64Array(2);
biguint64[0] = 42n;
console.log(biguint64[0]); // 42n
console.log(biguint64.length); // 2
console.log(biguint64.BYTES_PER_ELEMENT); // 8
// From an array
var arr = new BigUint64Array([21n,31n]);
console.log(arr[1]); // 31n
// From another TypedArray
var x = new BigUint64Array([21n, 31n]);
var y = new BigUint64Array(x);
console.log(y[0]); // 21n
// From an ArrayBuffer
var buffer = new ArrayBuffer(32);
var z = new BigUint64Array(buffer, 0, 4);
// From an iterable
var iterable = function*(){ yield* [1n, 2n, 3n]; }();
var biguint64 = new BigUint64Array(iterable);
// BigUint64Array[1n, 2n, 3n]
## Specifications
`BigUint64Array`
67
79
68
No
54
No
67
67
68
48
No
9.0
`BigUint64Array`
67
79
68
No
54
No
67
67
68
48
No
9.0
## See also
- [JavaScript typed arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
- [`BigInt64Array`](bigint64array)
- [`DataView`](dataview)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array
# Function.prototype.bind()
The `bind()` method creates a new function that, when called, has its `this` keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
## Syntax
bind(thisArg)
bind(thisArg, arg1)
bind(thisArg, arg1, arg2)
bind(thisArg, arg1, ... , argN)
### Parameters
`thisArg`
The value to be passed as the `this` parameter to the target function `func` when the bound function is called. The value is ignored if the bound function is constructed using the [`new`](../../operators/new) operator. When using `bind` to create a function (supplied as a callback) inside a `setTimeout`, any primitive value passed as `thisArg` is converted to object. If no arguments are provided to `bind `, or if the `thisArg` is `null` or `undefined`, the `this` of the executing scope is treated as the `thisArg` for the new function.
`arg1, arg2, ...argN` Optional
Arguments to prepend to arguments provided to the bound function when invoking `func`.
### Return value
A copy of the given function with the specified `this` value, and initial arguments (if provided).
## Description
The `bind()` function creates a new **bound function**, which is an _exotic function object_ (a term from ECMAScript 2015) that wraps the original function object. Calling the bound function generally results in the execution of its wrapped function.
A bound function has the following internal properties:
`[[BoundTargetFunction]]`
The wrapped function object
`[[BoundThis]]`
The value that is always passed as `this` value when calling the wrapped function.
`[[BoundArguments]]`
A list of values whose elements are used as the first arguments to any call to the wrapped function.
`[[Call]]`
Executes code associated with this object. Invoked via a function call expression. The arguments to the internal method are a `this` value and a list containing the arguments passed to the function by a call expression.
When a bound function is called, it calls internal method `[[Call]]` on `[[BoundTargetFunction]]`, with following arguments `Call(boundThis, ...args)`. Where `boundThis` is `[[BoundThis]]`, `args` is `[[BoundArguments]]`, followed by the arguments passed by the function call.
A bound function may also be constructed using the [`new`](../../operators/new) operator. Doing so acts as though the target function had instead been constructed. The provided `this` value is ignored, while prepended arguments are provided to the emulated function.
## Examples
### Creating a bound function
The simplest use of `bind()` is to make a function that, no matter how it is called, is called with a particular `this` value.
A common mistake for new JavaScript programmers is to extract a method from an object, then to later call that function and expect it to use the original object as its `this` (e.g., by using the method in callback-based code).
Without special care, however, the original object is usually lost. Creating a bound function from the function, using the original object, neatly solves this problem:
this.x = 9; // 'this' refers to global 'window' object here in a browser
const module = {
x: 81,
getX: function() { return this.x; }
};
module.getX();
// returns 81
const retrieveX = module.getX;
retrieveX();
// returns 9; the function gets invoked at the global scope
// Create a new function with 'this' bound to module
// New programmers might confuse the
// global variable 'x' with module's property 'x'
const boundGetX = retrieveX.bind(module);
boundGetX();
// returns 81
### Partially applied functions
The next simplest use of `bind()` is to make a function with pre-specified initial arguments.
These arguments (if any) follow the provided `this` value and are then inserted at the start of the arguments passed to the target function, followed by whatever arguments are passed bound function at the time it is called.
function list() {
return Array.prototype.slice.call(arguments);
}
function addArguments(arg1, arg2) {
return arg1 + arg2
}
const list1 = list(1, 2, 3);
// [1, 2, 3]
const result1 = addArguments(1, 2);
// 3
// Create a function with a preset leading argument
const leadingThirtysevenList = list.bind(null, 37);
// Create a function with a preset first argument.
const addThirtySeven = addArguments.bind(null, 37);
const list2 = leadingThirtysevenList();
// [37]
const list3 = leadingThirtysevenList(1, 2, 3);
// [37, 1, 2, 3]
const result2 = addThirtySeven(5);
// 37 + 5 = 42
const result3 = addThirtySeven(5, 10);
// 37 + 5 = 42
// (the second argument is ignored)
### With `setTimeout()`
By default within [`window.setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout), the `this` keyword will be set to the [`window`](https://developer.mozilla.org/en-US/docs/Web/API/Window) (or `global`) object. When working with class methods that require `this` to refer to class instances, you may explicitly bind `this` to the callback function, in order to maintain the instance.
function LateBloomer() {
this.petalCount = Math.floor(Math.random() * 12) + 1;
}
// Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
window.setTimeout(this.declare.bind(this), 1000);
};
LateBloomer.prototype.declare = function() {
console.log(`I am a beautiful flower with ${this.petalCount} petals!`);
};
const flower = new LateBloomer();
flower.bloom();
// after 1 second, calls 'flower.declare()'
### Bound functions used as constructors
**Warning:** This section demonstrates JavaScript capabilities and documents some edge cases of the `bind()` method.
The methods shown below are not the best way to do things, and probably should not be used in any production environment.
Bound functions are automatically suitable for use with the [`new`](../../operators/new) operator to construct new instances created by the target function. When a bound function is used to construct a value, the provided `this` is ignored.
However, provided arguments are still prepended to the constructor call:
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function() {
return `${this.x},${this.y}`;
};
const p = new Point(1, 2);
p.toString();
// '1,2'
// not supported in the polyfill below,
// works fine with native bind:
const YAxisPoint = Point.bind(null, 0/*x*/);
const emptyObj = {};
const YAxisPoint = Point.bind(emptyObj, 0/*x*/);
const axisPoint = new YAxisPoint(5);
axisPoint.toString(); // '0,5'
axisPoint instanceof Point; // true
axisPoint instanceof YAxisPoint; // true
new YAxisPoint(17, 42) instanceof Point; // true
Note that you need not do anything special to create a bound function for use with [`new`](../../operators/new).
The corollary is that you need not do anything special to create a bound function to be called plainly, even if you would rather require the bound function to only be called using [`new`](../../operators/new).
// Example can be run directly in your JavaScript console
// ...continued from above
// Can still be called as a normal function
// (although usually this is undesired)
YAxisPoint(13);
`${emptyObj.x},${emptyObj.y}`;
// > '0,13'
If you wish to support the use of a bound function only using [`new`](../../operators/new), or only by calling it, the target function must enforce that restriction.
### Creating shortcuts
`bind()` is also helpful in cases where you want to create a shortcut to a function which requires a specific `this` value.
Take [`Array.prototype.slice()`](../array/slice), for example, which you want to use for converting an array-like object to a real array. You could create a shortcut like this:
const slice = Array.prototype.slice;
// ...
slice.apply(arguments);
With `bind()`, this can be simplified.
In the following piece of code, `slice()` is a bound function to the [`apply()`](apply) function of [`Function`](../function), with the `this` value set to the [`slice()`](../array/slice) function of `Array.prototype`. This means that additional `apply()` calls can be eliminated:
// same as "slice" in the previous example
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.apply.bind(unboundSlice);
// ...
slice(arguments);
## Polyfill
Because older browsers are generally also slower browsers, it is far more critical than most people recognize to create performance polyfills to make the browsing experience in outdated browsers slightly less horrible.
Thus, presented below are two options for `Function.prototype.bind()` polyfills:
1. The first one is much smaller and more performant, but does not work when using the `new` operator.
2. The second one is bigger and less performant, but it permits some usage of the `new` operator on bound functions.
Generally, in most code it's very rare to see `new` used on a bound function, so it is generally best to go with the first option.
// Does not work with `new (funcA.bind(thisArg, args))`
if (!Function.prototype.bind) (function(){
var slice = Array.prototype.slice;
Function.prototype.bind = function() {
var thatFunc = this, thatArg = arguments[0];
var args = slice.call(arguments, 1);
if (typeof thatFunc !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - ' +
'what is trying to be bound is not callable');
}
return function(){
var funcArgs = args.concat(slice.call(arguments))
return thatFunc.apply(thatArg, funcArgs);
};
};
})();
You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of `bind()` in implementations that do not natively support it.
// Yes, it does work with `new (funcA.bind(thisArg, args))`
if (!Function.prototype.bind) (function(){
var ArrayPrototypeSlice = Array.prototype.slice;
Function.prototype.bind = function(otherThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var baseArgs= ArrayPrototypeSlice.call(arguments, 1),
baseArgsLength = baseArgs.length,
fToBind = this,
fNOP = function() {},
fBound = function() {
baseArgs.length = baseArgsLength; // reset to default base arguments
baseArgs.push.apply(baseArgs, arguments);
return fToBind.apply(
fNOP.prototype.isPrototypeOf(this) ? this : otherThis, baseArgs
);
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
})();
Some of the many differences (there may well be others, as this list does not seriously attempt to be exhaustive) between this algorithm and the specified algorithm are:
- The partial implementation relies on [`Array.prototype.slice()`](../array/slice), [`Array.prototype.concat()`](../array/concat), [`Function.prototype.call()`](call) and [`Function.prototype.apply()`](apply), built-in methods to have their original values.
- The partial implementation creates functions that do not have immutable "poison pill" [`caller`](caller) and `arguments` properties that throw a [`TypeError`](../typeerror) upon get, set, or deletion. (This could be added if the implementation supports [`Object.defineProperty`](../object/defineproperty), or partially implemented \[without throw-on-delete behavior\] if the implementation supports the [`__defineGetter__`](../object/__definegetter__) and [`__defineSetter__`](../object/__definesetter__) extensions.)
- The partial implementation creates functions that have a `prototype` property. (Proper bound functions have none.)
- The partial implementation creates bound functions whose [`length`](length) property does not agree with that mandated by ECMA-262: it creates functions with `length` of `0`. A full implementation—depending on the length of the target function and the number of pre-specified arguments—may return a non-zero length.
- The partial implementation creates bound functions whose [`name`](name) property is not derived from the original function name. According to ECMA-262, name of the returned bound function should be "bound " + name of target function (note the space character).
If you choose to use this partial implementation, **you must not rely on those cases where behavior deviates from ECMA-262, 5th edition!** Thankfully, these deviations from the specification rarely (if ever) come up in most coding situations. If you do not understand any of the deviations from the specification above, then it is safe in this particular case to not worry about these noncompliant deviation details.
**If it's absolutely necessary and performance is not a concern**, a far slower (but more specification-compliant solution) can be found at .
## Specifications
`bind`
7
12
4
9
11.6
5.1
4
18
4
12
6
1.0
## See also
- [`Function.prototype.apply()`](apply)
- [`Function.prototype.call()`](call)
- [Functions](../../functions)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
# Bitwise AND (&)
The bitwise AND operator (`&`) returns a `1` in each bit position for which the corresponding bits of both operands are `1`s.
## Syntax
a & b
## Description
The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32 bit integer:
Before: 11100110111110100000000000000110000000000001
After: 10100000000000000110000000000001
Each bit in the first operand is paired with the corresponding bit in the second operand: _first bit_ to _first bit_, _second bit_ to _second bit_, and so on.
The operator is applied to each pair of bits, and the result is constructed bitwise.
The truth table for the AND operation is:
9 (base 10) = 00000000000000000000000000001001 (base 2)
--------------------------------
~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
Bitwise NOTing any number `x` yields `-(x + 1)`. For example, `~-5` yields `4`.
Note that due to using 32-bit representation for numbers both `~-1` and `~4294967295` (232-1) results in `0`.
## Examples
### Using bitwise NOT
~0; // -1
~-1; // 0
~1; // -2
## Specifications