Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Javascript ch2

553 views

Published on

book review of Head-First Javascript ch2

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Javascript ch2

  1. 1. HFJS – BookReview Ch2Brady Cheng
  2. 2. Agenda Data Type Variables Constants Naming Rules for Identifiers Add Operations Type Casting NaN
  3. 3. Data Type Three data types in JS  Text  Number  Boolean
  4. 4. Variables Use keyword “var” to tell JS it‟s a variable Use “=” to give variable its value var name = „brady‟; Init value of keyword identifier variable
  5. 5. Constant Use keyword “const” to tell JS it‟s a constant Use “=” to give constant its value const PI = 3.14;
  6. 6. Naming Rules Naming rules for identifiers  Must be meaningful  First letter can be _, $, and letters  Other letters can be _, $, numbers, and letters const _name = „brady‟; (O) var 1age = 26; (X) const $sex = „male‟; (O) const location = „Taiwan‟ (O)
  7. 7. Add Operations Use in numbers, it means addition Use in texts, it means concat 1 + 2 = 3 „1‟ + „2‟ = 12
  8. 8. Type Casting  Here we discuss how to cast strings to numbers  parseInt : convert text to int  parseFloat : convert text to float„1‟ + „2‟ = 12„1.1‟ + „2.2‟ = 1.12.2parseInt(„1‟) + parseInt(„2‟) = 3parseInt(„1 feet‟) + parseInt(„2 inch‟) = 3 //ignore feet and inchparseInt(„1.1‟) + parseInt(„2.2‟) = 3 //ignore .1 and .2parseFloat(„1.1‟) + parseFloat(„2.2‟) = 3.3parseInt(„$1‟) = NaN //not a number
  9. 9. NaN What is NaN  Not a number  Usually caused by  Uninitialized vaiables  Ex. var a; 1+a = NaN  Error casting  Ex. parseInt(„nan‟) = NaN

×