Content deleted Content added
explain iterators better |
big change 2 Tags: nowiki added 2017 wikitext editor |
||
Line 192:
</syntaxhighlight>
Note: There is no built-in language literal for undefined. Thus {{code|lang=javascript|code=(x === undefined)}} is not a foolproof way to check whether a variable is undefined, because in versions before ECMAScript 5, it is legal for someone to write {{code|lang=javascript|code=var undefined = "I'm defined now";}}. A more robust approach is to compare using {{code|lang=javascript|code=(typeof x === 'undefined')}}.
Functions like this won't work as expected:
Line 205:
===Number===
Numbers are represented in binary as [[IEEE-754]] [[floating point]] doubles
This becomes an issue when comparing or formatting numbers. For example:
<syntaxhighlight lang="javascript">
console.log(0.2 + 0.1 === 0.3); // displays false
console.log(0.94 - 0.01); // displays 0.9299999999999999
</syntaxhighlight>
Line 226:
0xFF; // a hexadecimal integer equal to 255, digits represented by the ...
// ... letters A-F may be upper or lowercase
</syntaxhighlight>
There's also a numeric separator, {{mono|_}} (the underscore), introduced in ES2021:
<syntaxhighlight lang="javascript">
// Note: Wikipedia syntax doesn't support numeric seperators yet
1_000_000_000; // Used with big numbers
1_000_000.5; // Support with decimals
1_000e1_000; // Support with exponents
// Support with binary, octals and hex
0b0000_0000_0101_1011;
0o0001_3520_0237_1327;
0xFFFF_FFFF_FFFF_FFFE;
// But you can't use them next to a non-digit number part, or at the start or end
_12; // Variable is not defined (the underscore makes it a variable identifier)
12_; // Syntax error (cannot be at the end of numbers)
12_.0; // Syntax error (doesn't make sense to put a separator next to the decimal point)
12._0; // Syntax error
12e_6; // Syntax error (next to "e", a non-digit. Doesn't make sense to put a separator at the start)
1000____0000; // Syntax error (next to "_", a non-digit. Only 1 separator at a time is allowed
</syntaxhighlight>
Line 245 ⟶ 267:
These three special values correspond and behave as the [[IEEE-754]] describes them.
The Number constructor (used as a function), or a unary + or -, may be used to perform explicit numeric conversion:
<syntaxhighlight lang="javascript">
Line 259 ⟶ 281:
</syntaxhighlight>
However, NaN is not equal to itself:
<syntaxhighlight lang="javascript">
const nan = NaN;
console.log(NaN == NaN); // false console.log(NaN === NaN); // false
console.log
▲console.log(a == a); // false
// You can use the isNaN methods to check for NaN
console.log(isNaN("converted to NaN")); // true
console.log(isNaN(NaN)); // true
console.log(Number.isNaN("not converted")); // false
console.log(Number.isNaN(NaN)); // true
</syntaxhighlight>
===BigInt===
When dividing BigInts, the results are [[Truncation|truncated]].
=== String ===
Line 298 ⟶ 328:
</syntaxhighlight>
Applying the <!-- loose -->equality operator ("==") to two strings returns true, if the strings have the same contents, which means: of the same length and containing the same sequence of characters (case is significant for alphabets). Thus:
<syntaxhighlight lang="javascript">
Line 318 ⟶ 348:
</syntaxhighlight>
The {{mono|String}} constructor creates a string object (an object wrapping a string):
<syntaxhighlight lang="javascript">
Line 324 ⟶ 354:
</syntaxhighlight>
These objects have a {{mono|valueOf}} method returning the primitive string wrapped within them<!-- also works with regular strings -->:
<syntaxhighlight lang="javascript">
Line 343 ⟶ 373:
===Boolean===
[[JavaScript]] provides a [[Boolean data type]] with {{mono|true}} and {{mono|false}} literals. The {{mono|[[typeof]]}} operator returns the string {{mono|"boolean"}} for these [[primitive types]]. When used in a logical context, {{mono|0}}, {{mono|-0}}, {{mono|null}}, {{mono|NaN}}, {{mono|undefined}}, and the empty string ({{mono|""}}) evaluate as {{mono|false}} due to automatic [[type coercion]]. All other values (the [[complement (set theory)|complement]] of the previous list) evaluate as {{mono|true}}, including the strings {{mono|"0"}}, {{mono|"false"}} and any object.
=== Type conversion === Automatic type coercion by the equality comparison operators (<code>==</code> and <code>!=</code>) can be avoided by using the type checked comparison operators (<code>===</code> and <code>!==</code>). When type conversion is required, JavaScript converts {{mono|Boolean}}, {{mono|Number}}, {{mono|String}}, or {{mono|Object}} operands as follows:<ref>{{cite web | url=https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators | title=Comparison Operators - MDC Doc Center | publisher=Mozilla | date=5 August 2010 | access-date=5 March 2011}}</ref>
Line 494 ⟶ 527:
<syntaxhighlight lang="javascript">
let myArray;
myArray = [0, 1, , , 4, 5]; // array with length 6 and 6 elements, ...▼
// Array literals
myArray = new Array(0, 1, 2, 3, 4, 5); // array with length 6 and 6 elements▼
myArray = [1, 2]; // length of 2
myArray = [1, 2,]; // same array - You can also have an extra comma at the end
// It's also possible to not fill in parts of the array
▲myArray =
myArray = [0, 1, /* hole */, /* hole */, 4, 5,]; // same array
myArray = [0, 1, /* hole */, /* hole */, 4, 5, /* hole */,]; // length of 7
// With the constructor
myArray = new Array(365); // an empty array with length 365
</syntaxhighlight>
Line 587 ⟶ 630:
!Example!!Returned value<br />rounded to 5 digits!!Description
|-
| <tt>Math.abs(-2.3)</tt> || align=center|2.3 ||
|-
| <tt>Math.acos(Math.SQRT1_2)</tt> || align=center|0.78540 rad. = 45° || [[Arccosine]]
Line 742 ⟶ 785:
===Function===
Every function in JavaScript is an instance of the <tt>Function</tt> constructor<!-- There's also constructor for async / generator functions: (async()=>{}).constructor gets AsyncFunction, (async function*(){}).constructor gets AsyncGeneratorFunction, (function*(){}).constructor gets GeneratorFunction -->:
<syntaxhighlight lang="javascript">
Line 1,153 ⟶ 1,196:
</syntaxhighlight>
===
{| class="wikitable"
|-
| align="center" | <tt>??=</tt>
| Nullish assignment
|-
| align="center" | <tt><nowiki>||=</nowiki></tt>
| Logical Or assignment
|-
| align="center" | <tt>&&=</tt>
| Logical And assignment
|}
=== Bitwise ===
{{Expand section|date=April 2011}}
JavaScript supports the following '''binary [[Bitwise operation|bitwise operators]]''':
|