JavaScript Data Types

350 views
273 views

Published on

This the slide stack for the two videos on Data types in my YouTube series on JavaScript. The videos are at https://www.youtube.com/watch?v=UAtJXkGggOU and https://www.youtube.com/watch?v=H2sjsGZyYaw

Published in: Internet
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
350
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JavaScript Data Types

  1. 1. JavaScript Data Types (part 1) Charles Russell Bennu Bird Media
  2. 2. JavaScript is a Typed language ● Weakly typed does not mean type unaware ● There are 6 types in JavaScript ● Boolean ● Number ● String ● Null ● Undefined ● Object – Complex Data type – Array
  3. 3. typeof Operator Type val Returns Undefined 'undefined' Null 'object' Boolean 'boolean' Number 'number' String 'string' Object (native and does not implement [[Call]]) 'object' Object (native or host and does implement [[Call]]) 'function' Object (host and does not implement [[Call]]) Implementation-defined except may not be "undefined", "boolean", "number", or "string". ECMA 262 Table 20 typeof val
  4. 4. But Null is not an object ● In chapter 8 of the standard Null is declared to be a primitive type. ● typeof returns object on null because the standard says to ● The only short justification I have seen is from Kiro Risk – The reasoning behind this is that null, in contrast with undefined, was (and still is) often used where objects appear. In other words, null is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". In fact, the ECMAScript specification defines null as the primitive value that
  5. 5. Null ● As just discussed this is the intentional absence of any value . ● Has only one value null ● Any variable that is set to null is not undefined ● It just has no value
  6. 6. Undefined defined ● The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined. ECMA 262 5.1
  7. 7. Objects an introduction ● Everything that is not a primitive type a is an object type (remember this it is important later) ● Properties are charcteristics ● Methods are actions ● In JavaScript unlike other languages, objects are dynamic ● They can be changed modified and extended at runtime ● They can have any value except undefined ● Represented as name value pairs in code
  8. 8. Boolean ● Can have one of two values ● true ● false ● Boolean() ● methods – toString() – valueOf() ● Falsey – Values that when checked evaluate to false ● 0 or -0 ● EmptyString ""
  9. 9. Next Data Types (part 2)
  10. 10. Number ● Represented internally as a floating point ● This has some implications. – Decimals values are approximations – 0.1 + 0.2 ● Expected Value 0.3 ● Returned value 0.30000000000000004 – Problem not unique to JavaScript ● All languages that impliment floating point have the issue ● Technical reason at The reason in detail is at http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html for those with a technical or mathematical disposition. ● When dealing with money covert to cents do the math then convert back
  11. 11. Number (cont) ● NaN ● NaN stands for Not a Number – But it is if typeof reports it as a Number – This is the result of bad calculation such as division by 0 – NaN is not equal to anything even NaN – Any calculations with NaN results in NaN ● IsNaN() will check to see if a value is NaN ● Number() ● toExponential() ● toFixed() ● toPrecision()
  12. 12. Number(yes a little more) ● parseInt(value, radix) ● Another way to convert a string to a number ● Radix – Base 10 the number system we all use is not the only one ● Octal base 8 – 8 digits 0...7 – 10 = 8, 11=9, 12=10, …. ● Hexadecimal Base 16 – 15 digits 0..9 then A..F – A = 10, B = 11, … F=15, 10=16, 11=17, …. – Leading with 0 makes value ocal, 0x hexadecimal – Older browsers the value defaulted to octal so use the radix can be value 1..32 10 would be the radix that we Humans like the best
  13. 13. The Math Object ● The Math Object is automatically created and is globally available ● Methods include ● floor() ● round() ● sin() ● cos() ● tan() ● random() ● And many more
  14. 14. Strings ● 0 or more characters represented as two hex digits ● UTF-16, for older browsers UCS2 ● There is no char data type as in other languages ● Here the equivilent is a string of length 1 ● Empty string ● X=““ is a string of length 0 in other words typeof returns string
  15. 15. Strings(cont) ● String literals ● Are surrounded by Double " or single quotes ' ● Quotes can not be nested in quotes of the same type – 'this will "work" fine' – 'this will 'not work' at all' ● toString() ● Available on most wrapper type converts values to their string equivalents ● Can't handle null or undefined
  16. 16. Strings (yea theres more) ● Escape characters ● allows you to use some special characters – ' allows you to use ' inside of a single quoted string – “ allows you to use double quotes in a double quoted string – n newline – t tab – backslash (escaping the escape)
  17. 17. Some String Properties and Methods ● String() ● length ● substr() ● toLowerCase() ● toUpperCase() ● split() ● splice()
  18. 18. Summary ● JavaScript primatives are Null, Undefined, Boolean, Number, String and Object ● The typeof operator can tell you the object type of a variable ● There are wrapper Objects for most primatives ● These wrapper object have useful methods ● Most things in JavaScript are objects
  19. 19. Next: A Demo and then Operators

×