Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
Learn about JavaScript types and comparisons Understand both implicit and explicit type conversions See how to declare and use variables, as well as their scope
JavaScript Types
Defines types you can use to work with values of different kinds Categorizing types
Primitive and object types With or without methods Mutable or immutable
Bad part is that it will liberally convert values from one type to another Special === operator
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
JavaScript Numbers
All numbers are 64-bit floating-point values
IEEE 754 standard Numbers as large as 1.7976931348623157x10308 to as small as 5x10-324 Integers between -9,007,199,254,740,992 (-253) and 9,007,199,254,740,992 (253), inclusive If you work with numbers outside integer range, youll lose precision
Integer Literals
Represent either as base 10 or hexadecimal All these are valid base 10 values
5, 100, 123456789, 0, -100
Exponential numbers
1.23e12 // 1.23 x 1012 1.23e-12 // 1.23 x 10-12 5.90849203e5 // 5.90849203 x 105
Arithmetic in JavaScript
Expected set of arithmetic operators
Including + (addition), - (subtraction), * (multiplication), / (division), and % (modulo) No integral division
JavaScript Strings
Ordered, immutable sequence of 16-bit Unicode characters
From zero to as many as will fit in memory Consist of letters, numbers, punctuation, and spaces Zero-based indexing Empty string () has length of zero No special type to represent a single character
String Delimiters
Either string is valid
"'Don't use that food to feed she said. 'It's been recalled manufacturer.'" '"Don"t use that food to feed she said. "It"s been recalled manufacturer."' the dog,' by the the dog," by the
Not valid
'"Don't use that food to feed the dog," she said. "It has been recalled by the manufacturer."'
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
String Literals
All the following are valid string literals
"" '' "Don Kiely" '1.414' // an empty string // another empty string
\"
\\ \0 \b \f \n
Double quote
Backslash Nul character (thats a backslash-zero) Backspace Form feed Newline
\r
\t \v \xXX \uXXXX
Carriage return
Horizontal tab Vertical tab The Latin-1 character specified by the two hexadecimal digits XX between 00 and ff. For example, the copyright symbol is \xa9. Learn More @ http://www.learnnowonline.com The Unicode character specified by the four Copyright by Application Developers Training Company hexadecimal digits dddd. For example, the symbol is
But be careful!
The result variable could hold other truthy or falsy values
Logical operators
&& AND || OR ! NOT
undefined
Also means absence of value, but in a deeper way Predefined global variable Type is undefined (literally, thats its type)
undefined
JavaScript uses in a few different ways
Variable that has been declared but not yet assigned a value Function parameter that the calling code doesnt provide a value for Property that doesnt exist on an object Array element that doesnt exist Function that doesnt return a value
Best advice
Use undefined to represent system-level, unexpected, or erroneous absence of value Use null to represent a program-level normal, or expected absence of value
In other words, use null when you need to express missing value
Let JavaScript manage use of undefined
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Type Comparisons
JavaScript compares primitive values and object values differently
Primitive values by value Object values by reference
Object Comparisons
JavaScript compares objects by reference
Equal only if the two variables hold a reference to the same object in memory If hold a reference to two different objects, even if objects are the exact same, they are not equal
Type Conversions
Feature that makes JavaScript easy and hard
Does everything it can to implicitly convert values to make useable in a statement Doesnt always do what you want
Various ways
Wrapper objects Type methods Global functions among others
JavaScript has specialized methods and functions that give you control
"AppDev".length
Using Variables
Way to temporarily store bits of data Necessity in almost every language JavaScript variables are a bit different
Variable Declaration
Not required, but you should do it Untyped, so simple: use var with identifier
var count; var minMsgID; var lastName;
Variable Declaration
Multiple variables in single var statement
var count = 0, minMsgID = 32629, lastName = "Kiely";
Variable Scope
Scope is section of code where variable is defined and accessible Two scopes in JavaScript
Global Local
Variable Hoisting
Function scope has an important implication All local variables declared within a function
Visible throughout the body of the function
Side effect: all local variables are visible even before they are declared
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn more about JavaScript on SlideShare!
Introduction to JavaScript