36

JavaScript introduction 1 ( Variables And Values )

Embed Size (px)

DESCRIPTION

First JavaScript presentation for 24/i

Citation preview

Page 1: JavaScript introduction 1 ( Variables And Values )
Page 2: JavaScript introduction 1 ( Variables And Values )

JAVASCRIPTVARIABLES AND VALUESVictor Perez

Page 3: JavaScript introduction 1 ( Variables And Values )

/ INTRODUCTION

Page 4: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Brendan Eich

● Mocha

● September, 1995 LiveScript

● December, 1995 JavaScript

● June, 1997 ECMAScript 1

● June, 1998 ECMAScript 2

● December, 1999 ECMAScript 3

● December, 2009 ECMAScript 5

● June, 2011 ECMAScript 5.1

● JScript

HISTORYINTRODUCTION

Page 5: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

JAVASCRIPTINTRODUCTION

document.getElementById()

navigator.battery.charging

new Array()

console.log()

process.on()

encodeURI()

JSON.parse()

new XMLHttpRequest()

DOM API

Device API

JavaScript

Plugin API / Host Object

Node.js API

JavaScript

JavaScript

DOM API

Page 6: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

GLOBAL OBJECTSINTRODUCTION

Value properties

Infinity, NaN and undefined

Function properties

eval(), isFinite(), isNaN(), parseFloat(), parseInt(), decodeURI(), decodeURIComponent(), encodeURI() and encodeURIComponent()

Deprecated: escape() and unescape()

Objects

Object(), Function(), Boolean(), Error(), EvalError(), RangeError(), ReferenceError(), SyntaxError(), TypeError(), URIError(), Number(), Math(), Date(), String(), RegExp(), Array(), JSON()

ECMAScript 6: Float32Array(), Float64Array(), Int8Array(), Int16Array(), Int32Array(), Uint8Array(), Uint16Array(), Uint32Array(), Uint8ClampedArray(), Symbol(), Map(), Set(), WeakMap(), WeakSet(), ArrayBuffer(), DataView(), Generator(), Promise(), Reflect(), and Proxy()

Function scope

arguments

Page 7: JavaScript introduction 1 ( Variables And Values )

/ VARIABLES

Page 8: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Must begin with:

○ a letter from the Unicode character set

○ $ dollar sign

○ _ underscore

● Subsequent characters can be:

○ letters and digits from the entire Unicode character set

○ dollar signs

○ underscores

● Reserved words are not allowed *next slide

IDENTIFIERSINTRODUCTION

Page 9: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

RESERVED WORDSINTRODUCTION

Keywords & literals

break, delete, function, return, typeof, case, do, if, switch, var, catch, else, in, this, void, continue, instanceof, throw, while, debugger, finally, new, with, default, for, null and try

literals: true and false

Future reserved words

class, enum, extends, super, const, export and import

Strict mode

implements, let, private, public, yield, interface, package, protected and static

also: arguments and eval

ECMAScript 3

abstract, double, goto, native, static, boolean, enum, implements, package, super, byte, export, import, private, synchronized, char, extends, int, protected, throws, class, final, interface, public, transient, const, float, long, short, volatile

Page 10: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Keyword var

● Declare multiple variables with the same var keyword

● Declared variables will contain the value undefined

● Initialize a variable

● Dynamic typing

● Repeated and omitted declarations

DECLARATIONVARIABLES

Page 11: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● global and function scope

SCOPEVARIABLES

Page 12: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● No block scoping

● Will be added in ECMAScript 6 via keyword let

HOISTINGVARIABLES

Page 13: JavaScript introduction 1 ( Variables And Values )

/ VALUES

Page 14: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄

V

DATA TYPESVALUES

Primitive

Number 25 25.5

String "string" 'also a string'

Boolean true false

Special

Undefined undefined void 0

Null null

Composite

Object {a: 1, b: "string", c: true} new String ()

Array [1, "string", true] new Array ()

Page 15: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Integer values and floating-point values

● double-precision 64-bit binary format IEEE 754 value

● Number.MAX_VALUE ± 1.7976931348623157e+308

● Number.MIN_VALUE ± 5e-324

● Integer between 2^53 and -2^53 (2^53 = 9007199254740992)

● Number.POSITIVE_INFINITY Infinity round(r) > Number.MAX_VALUE

● Number.NEGATIVE_INFINITY -Infinity round(r) < Number.MIN_VALUE

● Number.NaN NaN Not a Number

NUMBERSVALUES

Page 16: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

WORKING WITH NUMBERSVALUES

● Math https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Page 17: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Sequences of unsigned 16-bit values

● Immutable

● UTF-16

● String.length

○ Surrogate pair

TEXTVALUES

Page 18: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

WORKING WITH STRINGSVALUES

Page 19: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● true

● false

● 3 == 1 false

● 3 == 3 true

BOOLEANSVALUES

Page 20: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● keyword null

● undefined

○ not initialized

○ default return value of a function

○ function parameters for which no argument is supplied

● null & undefined don’t have any properties

UNDEFINED & NULLVALUES

Page 21: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Create

● mutable

● Comparison

○ Not by value

○ By reference

OBJECTS (BASIC)VALUES

Page 22: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● string inherits String object

● number inherits Number object

● boolean inherits Boolean object

● Immutable

WRAPPER OBJECTSVALUES

Page 23: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

● Specialized object

● Zero-based

● 32-bit indexes

○ From 0 up to 4294967294 ( 2^32 - 2)

● Max elements 4294967295 ( 2^32 - 1)

○ RangeError

● mutable

ARRAYSVALUES

Page 24: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

WORKING WITH ARRAYSVALUES

Page 25: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

TYPEOFVALUES

Value Result Notes

Undefined "undefined"

Null "object" bug http://www.2ality.com/2013/10/typeof-null.html

Boolean "boolean"

Number "number"

String "string"

Function object "function" Implements [[Call]] in ECMAScript terms

Any other object "object"

Other

Array "object" Array.isArray()

/a/ "object" / "function" in some browser "object" is conform to ECMAScript 5.1

IE < 9: typeof alert "object" Most old IE host objects where objects and not functions

Page 26: JavaScript introduction 1 ( Variables And Values )

/ TYPE CONVERSIONS

Page 27: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

AUTOMATICALLYTYPE CONVERSIONS

● To number

○ +anyType or -anyType

○ anyType -, *, / or % anyType

○ boolean, number or null + boolean, number or null

● To string

○ anyType + anyType ( as long both aren't a number, boolean or null)

● To boolean

○ !anyType

● Comparison operators ( except the strict equality )

○ valueOf() and toString()

Page 28: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

EXPLICITTYPE CONVERSIONS

● Number()

○ parseInt(string, radix)○ parseFloat(string)

● String()

● Boolean()

● Object()

Page 29: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

NUMBERSTYPE CONVERSIONS

Value String Boolean Object

0 "0" false new Number() or new Number(0)

-0 "0" false new Number(-0)

NaN "NaN" false new Number(NaN)

Infinity "Infinity" true new Number(Infinity)

-Infinity "-Infinity" true new Number(-Infinity)

1 ( any number > 0 ) "1" true new Number(1)

-1 ( any number < 0) "-1" true new Number(-1)

Page 30: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

STRINGSTYPE CONVERSIONS

Value Number Boolean Object

"24" 24 true new String("24")

"foo" NaN true new String("foo");

"" ( empty string ) 0 false new String() or new String("")

Page 31: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

BOOLEANSTYPE CONVERSIONS

Value Number String Object

true 1 true new Boolean(true)

false 0 false new Boolean() or new Boolean(false);

Page 32: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

UNDEFINED & NULLTYPE CONVERSIONS

● Can’t be convert to a Object

Value Number String Boolean

undefined NaN "undefined" false

null 0 "null” false

Page 33: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

OBJECTSTYPE CONVERSIONS

● From object to string

○ toString()

○ valueOf()

● From object to number

○ valueOf()

○ toString()

● From object to boolean always true

Value Number String Boolean

new Object() NaN "[object Object]" true

Page 34: JavaScript introduction 1 ( Variables And Values )

⁄ ⁄ ⁄

STANDARD BUILT-IN OBJECTSTYPE CONVERSIONS

Value String Number

new Date() "Tue Apr 22 2014 12:13:49 GMT+0200"* http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.2

1398165557386Date.getTime()

new RegExp('^foo', 'i') "/^foo/i" NaN

new Array() (empty array) ""Array.join()

0

new Array([24]) (array with a single element) "24”Array.join()

24 or NaN if the value can’t be cast to a number

new Array([24, "i"]) "24,i”Array.join()

NaN

function (x, y) { return x + y; } "function (x, y) { return x + y; }"* http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.2

NaN

new Error("foo")new TypeError("foo")

"Error: foo""TypeError: foo"

NaN

Page 35: JavaScript introduction 1 ( Variables And Values )

/ QUESTIONS?

Page 36: JavaScript introduction 1 ( Variables And Values )

THANKS@[email protected]