JavaScript Data Types

19
JavaScript Data Types (part 1) Charles Russell Bennu Bird Media

description

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

Transcript of JavaScript Data Types

Page 1: JavaScript Data Types

JavaScript Data Types (part 1)

Charles Russell

Bennu Bird Media

Page 2: JavaScript Data Types

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

Page 3: JavaScript Data Types

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 20typeof val

Page 4: JavaScript Data Types

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 represents the intentional absence of any object value (ECMA-262, 11.4.11). http://kiro.me/blog/typeof_null.html

Page 5: JavaScript Data Types

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

Page 6: JavaScript Data Types

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

Page 7: JavaScript Data Types

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

● New operator gives you an empty set.● The rest of the primitives data types I talk about have an Object wrapper of the

same name● For some operations the primtive gets converted to a coresponding Object for the operation

then converted back to a primitive

Page 8: JavaScript Data Types

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 ""● null● NaN

– Everything else is truthy● Including the strings "0" and "false"

Page 9: JavaScript Data Types

Next Data Types (part 2)

Page 10: JavaScript Data Types

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

Page 11: JavaScript Data Types

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()

Page 12: JavaScript Data Types

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

Page 13: JavaScript Data Types

The Math Object

● The Math Object is automatically created and is globally available

● Methods include● floor()● round()● sin()● cos()● tan()● random()● And many more

Page 14: JavaScript Data Types

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

Page 15: JavaScript Data Types

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

Page 16: JavaScript Data Types

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)

Page 17: JavaScript Data Types

Some String Properties and Methods

● String()● length● substr()● toLowerCase()● toUpperCase()● split()● splice()

Page 18: JavaScript Data Types

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

Page 19: JavaScript Data Types

Next: A Demo and then Operators