01 IntroductionToJavaScript
01 IntroductionToJavaScript
Generalities
Lexical Structure
Numbers
Strings
Booleans
All types can be converted to booleans 0, NaN, null, undefined, "" are converted to false
Everything else is converted to true
Functions
Objects
Arrays
Define arrays using constructor: new Array(), new Array(5), new Array('a',2,'three') Define array literals: ['one','two',3], [1,,,4] Specific methods: join, concat, push, pop, shift, reverse, sort, splice and others Iterate using length property or for/in
null is the value of a variable with no value undefined is the evaluation of an identifier that was never declared or the value of a variable that was never assigned a value
Date
4 ways to instantiate a Date object: new Date() current date/time new Date(milliseconds) given timestamp new Date(string) standard formatted date time new Date(Y,M,D,h,m,s,millis) separate fields Getters and setters each field
Parsing standard time format strings Locale-sensitive formatting to string
Regular Expressions
Usage through RegExp methods exec, test and String methods match, replace, split and search
Errors
JavaScript interpreter throws an Error object when a runtime error occurs Predefined errors types are Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError Error handling can be done with try / catch / finally Error objects usually have as properties: name and message
Type Conversion
JavaScript tries to convert types used in a context that require other types
Context in which value is used String Number Boolean Object NaN false Error Undefined value "undefined" null "null" 0 false Error Nonempty string As is Numeric value of string or NaN true String object 0 false String object Empty string As is 0 "0" false Number object As is NaN "NaN" false Number object As is "Infinity" true Infinity As is Number object true Negative infinity "-Infinity" As is Number object true Any other number String value of number As is Number object true "true" 1 As is Boolean object false "false" 0 As is Boolean object toString( ) Object valueOf(), toString(), or NaN true As is Value
Types Recap
Difference between "4"*"4" and "4"+"4" typeof(true) and typeof(new Boolean(true)) Type and value of !!new Object() null == undefined typeof(hmm) and typeof(new String(hmm)) 5 != "5" 100 == true new String("coffee") == "coffee" 100 + true NaN == NaN