JavaScript: Values, Types and Variables

1,190 views

Published on

Learn about JavaScript types and comparisons and how to declare and use variables.

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,190
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section, String Operations\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section, Object Comparisons\n
  • DEMO: rest of section, Object Comparisons\n
  • DEMO: rest of section, Object Comparisons\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • \n
  • \n
  • \n
  • \n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • DEMO: Implicit Type Conversions, Equality Conversions\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • JavaScript: Values, Types and Variables

    1. 1. Values, Types, and Variables Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    2. 2. Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    3. 3. Objectives• Learn about JavaScript types and comparisons Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    4. 4. Objectives• Learn about JavaScript types and comparisons• Understand both implicit and explicit type conversions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    5. 5. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    6. 6. Introduction to Values, Types, and Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    7. 7. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    8. 8. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    9. 9. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    10. 10. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types  Variables Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    11. 11. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types  Variables• JavaScript may seem limited and confining Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    12. 12. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types  Variables• JavaScript may seem limited and confining  Weakly typed language, so variables are untyped Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    13. 13. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types  Variables• JavaScript may seem limited and confining  Weakly typed language, so variables are untyped  Object-oriented language Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    14. 14. Introduction to Values, Types, and• Handling data is the fundamental task of programming languages  Values  Types  Variables• JavaScript may seem limited and confining  Weakly typed language, so variables are untyped  Object-oriented language• Type system is definitely one of the good parts of the language Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    15. 15. JavaScript Types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    16. 16. JavaScript Types• Defines types you can use to work with values of different kinds Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    17. 17. JavaScript Types• Defines types you can use to work with values of different kinds• Categorizing types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    18. 18. JavaScript Types• Defines types you can use to work with values of different kinds• Categorizing types  Primitive and object types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    19. 19. JavaScript Types• Defines types you can use to work with values of different kinds• Categorizing types  Primitive and object types  With or without methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    20. 20. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    21. 21. Primary Types in JavaScript Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    22. 22. Primary Types in JavaScript Type Category Methods Mutability number Primitive Yes Immutable string Primitive Yes Immutable Boolean Primitive Yes Immutable null Primitive No Immutableundefined Primitive No Immutable object Object Yes Mutable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    23. 23. Primary Types in JavaScript Type Category Methods Mutability number Primitive Yes Immutable string Primitive Yes Immutable Boolean Primitive Yes Immutable null Primitive No Immutable undefined Primitive No Immutable object Object Yes Mutable• Bad part is that it will liberally convert values from one type to another Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    24. 24. Primary Types in JavaScript Type Category Methods Mutability number Primitive Yes Immutable string Primitive Yes Immutable Boolean Primitive Yes Immutable null Primitive No Immutable undefined Primitive No Immutable object Object Yes Mutable• 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
    25. 25. JavaScript Numbers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    26. 26. JavaScript Numbers• All numbers are 64-bit floating-point values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    27. 27. JavaScript Numbers• All numbers are 64-bit floating-point values  IEEE 754 standard Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    28. 28. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    29. 29. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    30. 30. 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, you’ll lose precision Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    31. 31. Integer Literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    32. 32. Integer Literals• Represent either as base 10 or hexadecimal Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    33. 33. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    34. 34. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    35. 35. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    36. 36. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    37. 37. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4• ECMAScript doesn’t support octal (base 8) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    38. 38. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4• ECMAScript doesn’t support octal (base 8)  Some JavaScript implementations support Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    39. 39. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4• ECMAScript doesn’t support octal (base 8)  Some JavaScript implementations support  Number starts with a leading zero, followed by digits from 0 to 7 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    40. 40. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4• ECMAScript doesn’t support octal (base 8)  Some JavaScript implementations support  Number starts with a leading zero, followed by digits from 0 to 7  Code won’t be portable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    41. 41. Integer Literals• Represent either as base 10 or hexadecimal• All these are valid base 10 values  5, 100, 123456789, 0, -100• Valid hex values  0x1a5f, 0X1A5F, 0x10cd4• ECMAScript doesn’t support octal (base 8)  Some JavaScript implementations support  Number starts with a leading zero, followed by digits from 0 to 7  Code won’t be portable  ECMAScript 5/Strict forbids Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    42. 42. Real Number Literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    43. 43. Real Number Literals• Express either as real number or exponential Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    44. 44. Real Number Literals• Express either as real number or exponential• Real numbers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    45. 45. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56. Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    46. 46. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56.  Omit either integral or fractional part Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    47. 47. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56.  Omit either integral or fractional part• Exponential numbers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    48. 48. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56.  Omit either integral or fractional part• Exponential numbers  1.23e12 // 1.23 x 1012 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    49. 49. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56.  Omit either integral or fractional part• Exponential numbers  1.23e12 // 1.23 x 1012  1.23e-12 // 1.23 x 10-12 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    50. 50. Real Number Literals• Express either as real number or exponential• Real numbers  1.0, 1.349201942, .3333333333, 125959403.39201488, 56.  Omit either integral or fractional part• Exponential numbers  1.23e12 // 1.23 x 1012  1.23e-12 // 1.23 x 10-12  5.90849203e5 // 5.90849203 x 105 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    51. 51. Arithmetic in JavaScript Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    52. 52. Arithmetic in JavaScript• Expected set of arithmetic operators Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    53. 53. Arithmetic in JavaScript• Expected set of arithmetic operators  Including + (addition), - (subtraction), * (multiplication), / (division), and % (modulo) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    54. 54. Arithmetic in JavaScript• Expected set of arithmetic operators  Including + (addition), - (subtraction), * (multiplication), / (division), and % (modulo)  No integral division Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    55. 55. Arithmetic in JavaScript• Expected set of arithmetic operators  Including + (addition), - (subtraction), * (multiplication), / (division), and % (modulo)  No integral division• Math object for other operations Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    56. 56. JavaScript Strings Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    57. 57. JavaScript Strings• Ordered, immutable sequence of 16-bit Unicode characters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    58. 58. JavaScript Strings• Ordered, immutable sequence of 16-bit Unicode characters  From zero to as many as will fit in memory Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    59. 59. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    60. 60. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    61. 61. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    62. 62. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    63. 63. 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• Delimit with single or double " quotes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    64. 64. 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• Delimit with single or double " quotes  Can’t mix to delimit string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    65. 65. 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• Delimit with single or double " quotes  Can’t mix to delimit string  Can embed quote not used to delimit string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    66. 66. String Delimiters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    67. 67. String Delimiters• Either string is valid Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    68. 68. String Delimiters• Either string is valid  "Dont use that food to feed the dog, she said. Its been recalled by the manufacturer." Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    69. 69. String Delimiters• Either string is valid  "Dont use that food to feed the dog, she said. Its been recalled by the manufacturer."  "Don"t use that food to feed the dog," she said. "It"s been recalled by the manufacturer." Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    70. 70. String Delimiters• Either string is valid  "Dont use that food to feed the dog, she said. Its been recalled by the manufacturer."  "Don"t use that food to feed the dog," she said. "It"s been recalled by the manufacturer."• Not valid Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    71. 71. String Delimiters• Either string is valid  "Dont use that food to feed the dog, she said. Its been recalled by the manufacturer."  "Don"t use that food to feed the dog," she said. "It"s been recalled by the manufacturer."• Not valid  "Dont 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
    72. 72. String Literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    73. 73. String Literals• All the following are valid string literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    74. 74. String Literals• All the following are valid string literals  "" // an empty string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    75. 75. String Literals• All the following are valid string literals  "" // an empty string  // another empty string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    76. 76. String Literals• All the following are valid string literals  "" // an empty string  // another empty string  "Don Kiely" Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    77. 77. String Literals• All the following are valid string literals  "" // an empty string  // another empty string  "Don Kiely"  1.414 // a number represented as a string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    78. 78. String Literals• All the following are valid string literals  "" // an empty string  // another empty string  "Don Kiely"  1.414 // a number represented as a string  "My password is beetle$%^Bailey392" Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    79. 79. Line Breaks in String Literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    80. 80. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    81. 81. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line  ECMAScript 5 allows break with at end of line Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    82. 82. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line  ECMAScript 5 allows break with at end of line• Problem in ECMAScript 3 but fine in 5: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    83. 83. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line  ECMAScript 5 allows break with at end of line• Problem in ECMAScript 3 but fine in 5: "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal." Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    84. 84. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line  ECMAScript 5 allows break with at end of line• Problem in ECMAScript 3 but fine in 5: "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."• Following is invalid…see the difference? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    85. 85. Line Breaks in String Literals• ECMAScript 3 requires string literal on one line  ECMAScript 5 allows break with at end of line• Problem in ECMAScript 3 but fine in 5: "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."• Following is invalid…see the difference? "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal." Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    86. 86. Escape Sequences in String Literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    87. 87. Escape Sequences in String Literals• Include characters that have special meaning by using an escape sequence Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    88. 88. Escape Sequences in String Literals• Include characters that have special meaning by using an escape sequence  Backslash identifies the special character Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    89. 89. Escape Sequences in String Literals• Include characters that have special meaning by using an escape sequence  Backslash identifies the special character  "Dont use that food to feed the dog," she said. "Its been recalled by the manufacturer."‘ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    90. 90. JavaScript Escape Sequences Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    91. 91. JavaScript Escape SequencesEscape Sequence Description Single quote or apostrophe " Double quote Backslash 0 Nul character (that’s a backslash-zero) b Backspace f Form feed n Newline r Carriage return t Horizontal tab v Vertical tab xXX The Latin-1 character specified by the two uXXXX hexadecimal digits XX between 00 and ff. For example, The Unicode character specified by the four the copyrightdigits dddd. For example, the π symbol is hexadecimal symbol is xa9. Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    92. 92. JavaScript Boolean Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    93. 93. JavaScript Boolean Values• Represents true or false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    94. 94. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    95. 95. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    96. 96. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    97. 97. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    98. 98. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    99. 99. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) "" (empty string) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    100. 100. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) "" (empty string) NaN Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    101. 101. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) "" (empty string) NaN null Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    102. 102. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) "" (empty string) NaN null undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    103. 103. JavaScript Boolean Values• Represents true or false  Usually the result of some logical comparison• JavaScript can convert any value to a Boolean  These are all false 0 (zero) -0 (negative zero) "" (empty string) NaN null undefined  All other values are true Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    104. 104. Truthy and Falsy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    105. 105. Truthy and Falsy• Any value that converts to true is truthy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    106. 106. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    107. 107. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    108. 108. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    109. 109. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    110. 110. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false• These are generally equivalent Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    111. 111. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false• These are generally equivalent  if (result !== 0) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    112. 112. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false• These are generally equivalent  if (result !== 0)  if (result) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    113. 113. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false• These are generally equivalent  if (result !== 0)  if (result)• But be careful! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    114. 114. Truthy and Falsy• Any value that converts to true is truthy• Any value that converts to false is falsy• When JavaScript expects a Boolean value  Truthy value works like true  Falsy value works like false• These are generally equivalent  if (result !== 0)  if (result)• But be careful!  The result variable could hold other truthy or falsy values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    115. 115. Boolean Methods and Logical Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    116. 116. Boolean Methods and Logical• Boolean values have two methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    117. 117. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false" Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    118. 118. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false"  valueOf() returns true of false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    119. 119. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false"  valueOf() returns true of false• Logical operators Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    120. 120. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false"  valueOf() returns true of false• Logical operators  && AND Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    121. 121. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false"  valueOf() returns true of false• Logical operators  && AND  || OR Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    122. 122. Boolean Methods and Logical• Boolean values have two methods  toString() returns "true" or "false"  valueOf() returns true of false• Logical operators  && AND  || OR  ! NOT Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    123. 123. Special Values: null and undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    124. 124. Special Values: null and undefined• null Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    125. 125. Special Values: null and undefined• null  Language keyword Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    126. 126. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    127. 127. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type  Absence of a value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    128. 128. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type  Absence of a value• undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    129. 129. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type  Absence of a value• undefined  Also means absence of value, but in a deeper way Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    130. 130. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type  Absence of a value• undefined  Also means absence of value, but in a deeper way  Predefined global variable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    131. 131. Special Values: null and undefined• null  Language keyword  An object, but considered a separate type  Absence of a value• undefined  Also means absence of value, but in a deeper way  Predefined global variable  Type is undefined (literally, that’s its type) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    132. 132. undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    133. 133. undefined• JavaScript uses in a few different ways Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    134. 134. undefined• JavaScript uses in a few different ways  Variable that has been declared but not yet assigned a value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    135. 135. 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 doesn’t provide a value for Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    136. 136. 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 doesn’t provide a value for  Property that doesn’t exist on an object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    137. 137. 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 doesn’t provide a value for  Property that doesn’t exist on an object  Array element that doesn’t exist Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    138. 138. 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 doesn’t provide a value for  Property that doesn’t exist on an object  Array element that doesn’t exist  Function that doesn’t return a value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    139. 139. 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 doesn’t provide a value for  Property that doesn’t exist on an object  Array element that doesn’t exist  Function that doesn’t return a value• undefined is read/write in ECMAScript 3, read- only in ECMAScript 5 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    140. 140. Use null or undefined? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    141. 141. Use null or undefined?• Both mean absence of value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    142. 142. Use null or undefined?• Both mean absence of value  Can often use interchangeably Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    143. 143. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    144. 144. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    145. 145. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined• Best advice Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    146. 146. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined• Best advice  Use undefined to represent system-level, unexpected, or erroneous absence of value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    147. 147. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined• 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    148. 148. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined• 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    149. 149. Use null or undefined?• Both mean absence of value  Can often use interchangeably  In fact, null == undefined  But null !== undefined• 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
    150. 150. Type Comparisons Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    151. 151. Type Comparisons• JavaScript compares primitive values and object values differently Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    152. 152. Type Comparisons• JavaScript compares primitive values and object values differently  Primitive values by value Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    153. 153. Type Comparisons• JavaScript compares primitive values and object values differently  Primitive values by value  Object values by reference Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    154. 154. Object Comparisons Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    155. 155. Object Comparisons• JavaScript compares objects by reference Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    156. 156. Object Comparisons• JavaScript compares objects by reference  Equal only if the two variables hold a reference to the same object in memory Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    157. 157. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    158. 158. Type Conversions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    159. 159. Type Conversions• Feature that makes JavaScript easy and hard Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    160. 160. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    161. 161. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    162. 162. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want• Automatic type conversions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    163. 163. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want• Automatic type conversions  Convert to truthy or falsy when expects Boolean Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    164. 164. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want• Automatic type conversions  Convert to truthy or falsy when expects Boolean  Converts anything to a string when expects string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    165. 165. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want• Automatic type conversions  Convert to truthy or falsy when expects Boolean  Converts anything to a string when expects string  Tries to convert to a number when expects number Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    166. 166. Type Conversions• Feature that makes JavaScript easy and hard  Does everything it can to implicitly convert values to make useable in a statement  Doesn’t always do what you want• Automatic type conversions  Convert to truthy or falsy when expects Boolean  Converts anything to a string when expects string  Tries to convert to a number when expects number• Also supports explicit type conversions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    167. 167. Explicit Type Conversions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    168. 168. Explicit Type Conversions• You can do it yourself explicitly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    169. 169. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    170. 170. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    171. 171. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code• Various ways Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    172. 172. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code• Various ways  Wrapper objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    173. 173. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code• Various ways  Wrapper objects  Type methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    174. 174. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code• Various ways  Wrapper objects  Type methods  Global functions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    175. 175. Explicit Type Conversions• You can do it yourself explicitly  Use when implicit conversions don’t work for you  Or you want cleaner and clearer code• Various ways  Wrapper objects  Type methods  Global functions  …among others Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    176. 176. Formatting Methods and ParsingFunctions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    177. 177. Formatting Methods and ParsingFunctions• Don’t have much control with other conversion techniques Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    178. 178. Formatting Methods and ParsingFunctions• Don’t have much control with other conversion techniques  Get a reasonable value in the type you want Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    179. 179. Formatting Methods and ParsingFunctions• Don’t have much control with other conversion techniques  Get a reasonable value in the type you want• JavaScript has specialized methods and functions that give you control Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    180. 180. Conversions Using Wrapper Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    181. 181. Conversions Using Wrapper Objects• Number, String, Boolean, and Object wrapper objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    182. 182. Conversions Using Wrapper Objects• Number, String, Boolean, and Object wrapper objects  Provide properties and methods for corresponding types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    183. 183. Conversions Using Wrapper Objects• Number, String, Boolean, and Object wrapper objects  Provide properties and methods for corresponding types  Creates a String object and uses length property Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    184. 184. Conversions Using Wrapper Objects• Number, String, Boolean, and Object wrapper objects  Provide properties and methods for corresponding types  Creates a String object and uses length property o "AppDev".length Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    185. 185. Conversions Using Wrapper Objects• Number, String, Boolean, and Object wrapper objects  Provide properties and methods for corresponding types  Creates a String object and uses length property o "AppDev".length• Can use these functions for explicit conversion Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    186. 186. Number Formatting Methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    187. 187. Number Formatting Methods• Give more control than toString() Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    188. 188. Number Formatting Methods• Give more control than toString()  toFixed() Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    189. 189. Number Formatting Methods• Give more control than toString()  toFixed()  toPrecision() Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    190. 190. Number Formatting Methods• Give more control than toString()  toFixed()  toPrecision()  toExponential() Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    191. 191. Number Formatting Methods• Give more control than toString()  toFixed()  toPrecision()  toExponential()• All three round number or pad with zeros as necessary Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    192. 192. Using Variables Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    193. 193. Using Variables• Way to temporarily store bits of data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    194. 194. Using Variables• Way to temporarily store bits of data• Necessity in almost every language Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    195. 195. Using Variables• Way to temporarily store bits of data• Necessity in almost every language• JavaScript variables are a bit different Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    196. 196. Variable Declaration Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    197. 197. Variable Declaration• Not required, but you should do it Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    198. 198. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    199. 199. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    200. 200. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    201. 201. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    202. 202. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName;• Undefined until initialized Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    203. 203. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName;• Undefined until initialized• Can declare and initialize in one statement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    204. 204. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName;• Undefined until initialized• Can declare and initialize in one statement  var count = 0; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    205. 205. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName;• Undefined until initialized• Can declare and initialize in one statement  var count = 0;  var minMsgID = 32629; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    206. 206. Variable Declaration• Not required, but you should do it• Untyped, so simple: use var with identifier  var count;  var minMsgID;  var lastName;• Undefined until initialized• Can declare and initialize in one statement  var count = 0;  var minMsgID = 32629;  var lastName = "Kiely"; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    207. 207. Variable Declaration Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    208. 208. Variable Declaration• Multiple variables in single var statement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    209. 209. Variable Declaration• Multiple variables in single var statement  var count = 0, minMsgID = 32629, lastName = "Kiely"; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    210. 210. Variable Declaration• Multiple variables in single var statement  var count = 0, minMsgID = 32629, lastName = "Kiely";• Break onto multiple lines Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    211. 211. Variable Declaration• Multiple variables in single var statement  var count = 0, minMsgID = 32629, lastName = "Kiely";• Break onto multiple lines  var count = 0, minMsgID = 32629, lastName = "Kiely"; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    212. 212. Variable Declaration• Multiple variables in single var statement  var count = 0, minMsgID = 32629, lastName = "Kiely";• Break onto multiple lines  var count = 0, minMsgID = 32629, lastName = "Kiely";• Declare and initialize within loops Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    213. 213. Variable Declaration• Multiple variables in single var statement  var count = 0, minMsgID = 32629, lastName = "Kiely";• Break onto multiple lines  var count = 0, minMsgID = 32629, lastName = "Kiely";• Declare and initialize within loops  for (var i = 2; i <= 36; i++) console.log("Base " + i + ": " + num.toString(i)); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    214. 214. Variable Cautions and Gotchas Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    215. 215. Variable Cautions and Gotchas• Variable can hold any type during execution Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    216. 216. Variable Cautions and Gotchas• Variable can hold any type during execution  var count = 0; <program code> count = "Don"; <more program code> count = false <yet more code> count = "The Duke of Windsor"; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    217. 217. Variable Cautions and Gotchas• Variable can hold any type during execution  var count = 0; <program code> count = "Don"; <more program code> count = false <yet more code> count = "The Duke of Windsor";• Be careful splitting across multiple lines! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    218. 218. Variable Cautions and Gotchas• Variable can hold any type during execution  var count = 0; <program code> count = "Don"; <more program code> count = false <yet more code> count = "The Duke of Windsor";• Be careful splitting across multiple lines!  var count = 0 minMsgID = 32629, lastName = "Kiely"; Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    219. 219. Variable Scope Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    220. 220. Variable Scope• Scope is section of code where variable is defined and accessible Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    221. 221. Variable Scope• Scope is section of code where variable is defined and accessible• Two scopes in JavaScript Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    222. 222. Variable Scope• Scope is section of code where variable is defined and accessible• Two scopes in JavaScript  Global Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    223. 223. Variable Scope• Scope is section of code where variable is defined and accessible• Two scopes in JavaScript  Global  Local Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    224. 224. Variable Scope• Scope is section of code where variable is defined and accessible• Two scopes in JavaScript  Global  Local• Local variable has function scope in JavaScript Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    225. 225. Variable Hoisting Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    226. 226. Variable Hoisting• Function scope has an important implication Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    227. 227. Variable Hoisting• Function scope has an important implication• All local variables declared within a function Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    228. 228. Variable Hoisting• Function scope has an important implication• All local variables declared within a function  Visible throughout the body of the function Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    229. 229. 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 @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    230. 230. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    231. 231. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    232. 232. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    233. 233. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!• Learn more about JavaScript on SlideShare! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    234. 234. 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

    ×