ES6 – The Primitives



ES6 – The Primitives

0 1


future-of-javascript-presentation

ES6: The Primitives

On Github noah-freitas / future-of-javascript-presentation

ES6

The Primitives

Goal

  • Review ECMAScript Version 6 extensions and changes to the primitive values, with:
    • Support in current environments
    • Function types
    • Simple code examples

Number

  • Octal Literals

    Base 8 number literal syntax

    0o10 === 8 && 0O10 === 8
  • Binary Literals

    Base 2 number literal syntax

    0b10 === 2 && 0B10 === 2
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.isFinite
    Number -> Boolean

    Returns false for Infinity and NaN, true otherwise

    Number.isFinite(Infinity)  === false
    Number.isFinite(-Infinity) === false
    Number.isFinite(NaN)       === false
    Number.isFinite(1)         === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.isInteger
    Number -> Boolean

    Returns true if the input is an integer, false otherwise.

    Number.isInteger(Infinity)  === false
    Number.isInteger(-Infinity) === false
    Number.isInteger(NaN)       === false
    Number.isInteger(1.1)       === false
    Number.isInteger(1)         === true
    Number.isInteger(0)         === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.isSafeInteger
    Number -> Boolean

    Returns true for integers between -253 and 253, false for other values.

    Number.isSafeInteger(-Math.pow(2, 53))     === false
    Number.isSafeInteger(-Math.pow(2, 53) + 1) === true
    Number.isSafeInteger(Math.pow(2, 53))      === false
    Number.isSafeInteger(Math.pow(2, 53) - 1)  === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.isNaN
    a -> Boolean

    Returns true for NaN, false otherwise.

    Number.isNaN(NaN) === true
    Number.isNaN(0)   === false
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.EPSILON

    The value 2-52. Very loosely, the smallest difference that can be represented as a Number.

    Number.EPSILON  === Math.pow(2, -52) === 2.220446049250313e-16
    0.1 + 0.2       !== 0.3
    0.1 + 0.2 - 0.3 === 5.551115123125783e-17
    0.1 + 0.2 - 0.3 <   Number.EPSILON
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Number

  • Number.MIN_SAFE_INTEGER

    The number -253-1 or -9007199254740991

  • Number.MAX_SAFE_INTEGER

    The number 253-1 or 9007199254740991

  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • Template Strings

    A new type of string literal allowing:

    • Value interpolation
    • Multi-line strings
    • String processing functions (tagged template strings)
    var name = 'bob';
    `my name is ${name}` === 'my name is bob'
    `Here is a 
    Multi-line 
    string`
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • Unicode code point escapes

    Allows all Unicode characters to be represented by a single code point value.

    '\u{1D306}' === '\uD834\uDF06'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.fromCodePoint
    ...[Number] -> String

    Returns a string composed of the code point arguments.

    String.fromCodePoint(55, 78) === '7N'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.raw
    [String], ...[*] -> String

    A built in template string tag. Returns a string with all escape sequences rendered literally.

    String.raw`A\nB` !== 'A\nB'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.codePointAt
    @String, Number -> Number

    Returns the UTF-16 encoded code point value at the given index of the receiving string.

    String.fromCodePoint('01234'.codePointAt(3)) === '3'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.normalize
    @String, String -> String

    Returns the "Unicode Normalization Form" of the receiving string, according to the specified form.

    '\u1E9B\u0323'.normalize('NFKC') !== '\u1E9B\u0323'.normalize('NFD')
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.repeat
    @String, Number -> String

    Returns a new string formed by concatenating the receiving string with it self the specified number of times.

    'abc'.repeat(4) === 'abcabcabcabc'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.startsWith
    @String, String -> Boolean

    Returns true if the receiving string begins with the passed string, false otherwise.

    'abcdefg'.startsWith('abc')  === true
    ' abcdefg'.startsWith('abc') === false
    'abcdefg'.startsWith('')     === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.endsWith
    @String, String -> Boolean

    Returns true if the receiving string ends with the passed string, false otherwise.

    'abcdefg'.endsWith('efg')  === true
    'abcdefg '.endsWith('efg') === false
    'abcdefg'.endsWith('')     === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

String

  • String.prototype.includes
    @String, String, Number -> Boolean

    Returns true if the receiving string has a substring matching the passed string, beginning at the passed index; false otherwise.

    'abcdefg'.includes('efg')    === true
    'abcdefg'.includes('abc', 4) === false
    'abcdefg'.includes('')       === true
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol
    String -> Symbol

    Returns a new Symbol with the passed string label. The label is not unique.

    Symbol('abc') !== Symbol('abc')
    var prop  = Symbol(),
        obj   = {};
    obj[prop] = 'My value';
    
    obj[prop]     === 'My value'
    obj[Symbol()] === undefined
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol.for
    String -> Symbol

    Returns the shared symbol from the global symbol registry for the provided string.

    Symbol.for('abc') === Symbol.for('abc')
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol.keyFor
    Symbol -> String

    Returns the string key for the passed symbol in the global symbol registry.

    Symbol.keyFor(Symbol.for('abc')) === 'abc'
  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol.hasInstance

    Customizes instanceof operator.

  • Symbol.isConcatSpreadable

    Customizes Array.prototype.concat.

  • Symbol.iterator

    Customizes for...of loops.

  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol.species

    Reference to an object's constructor function.

  • Symbol.toPrimitive

    Customizes internal ToPrimitive operation.

  • Symbol.toStringTag

    Customizes object's stringification.

  • Full
  • Beta
  • Flag
  • None
  • Non-Standard

Symbol

  • Symbol.unscopables

    Customizes with operator.

  • Full
  • Beta
  • Flag
  • None
  • Non-Standard