JavaScript introduction 1 ( Variables And Values )
-
Upload
victor-verhaagen -
Category
Technology
-
view
552 -
download
0
description
Transcript of JavaScript introduction 1 ( Variables And Values )
JAVASCRIPTVARIABLES AND VALUESVictor Perez
/ INTRODUCTION
⁄ ⁄ ⁄
● 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
⁄ ⁄ ⁄
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
⁄ ⁄ ⁄
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
/ VARIABLES
⁄ ⁄ ⁄
● 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
⁄ ⁄ ⁄
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
⁄ ⁄ ⁄
● 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
⁄ ⁄ ⁄
● global and function scope
SCOPEVARIABLES
⁄ ⁄ ⁄
● No block scoping
● Will be added in ECMAScript 6 via keyword let
HOISTINGVARIABLES
/ 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 ()
⁄ ⁄ ⁄
● 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
⁄ ⁄ ⁄
WORKING WITH NUMBERSVALUES
● Math https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
⁄ ⁄ ⁄
● Sequences of unsigned 16-bit values
● Immutable
● UTF-16
● String.length
○ Surrogate pair
TEXTVALUES
⁄ ⁄ ⁄
WORKING WITH STRINGSVALUES
⁄ ⁄ ⁄
● true
● false
● 3 == 1 false
● 3 == 3 true
BOOLEANSVALUES
⁄ ⁄ ⁄
● 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
⁄ ⁄ ⁄
● Create
● mutable
● Comparison
○ Not by value
○ By reference
OBJECTS (BASIC)VALUES
⁄ ⁄ ⁄
● string inherits String object
● number inherits Number object
● boolean inherits Boolean object
● Immutable
WRAPPER OBJECTSVALUES
⁄ ⁄ ⁄
● Specialized object
● Zero-based
● 32-bit indexes
○ From 0 up to 4294967294 ( 2^32 - 2)
● Max elements 4294967295 ( 2^32 - 1)
○ RangeError
● mutable
ARRAYSVALUES
⁄ ⁄ ⁄
WORKING WITH ARRAYSVALUES
⁄ ⁄ ⁄
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
/ TYPE CONVERSIONS
⁄ ⁄ ⁄
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()
⁄ ⁄ ⁄
EXPLICITTYPE CONVERSIONS
● Number()
○ parseInt(string, radix)○ parseFloat(string)
● String()
● Boolean()
● Object()
⁄ ⁄ ⁄
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)
⁄ ⁄ ⁄
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("")
⁄ ⁄ ⁄
BOOLEANSTYPE CONVERSIONS
Value Number String Object
true 1 true new Boolean(true)
false 0 false new Boolean() or new Boolean(false);
⁄ ⁄ ⁄
UNDEFINED & NULLTYPE CONVERSIONS
● Can’t be convert to a Object
Value Number String Boolean
undefined NaN "undefined" false
null 0 "null” false
⁄ ⁄ ⁄
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
⁄ ⁄ ⁄
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
/ QUESTIONS?
THANKS@[email protected]