You are on page 1of 26

WEB-007 JavaScript

ver. 1.0

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
WEB-007 JavaScript

Data types
ver. 1.0

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data types
• Overview
• Number
• String
• Boolean
• Function
• Object
• Array
• null and undefined values
• Object wrappers for primitive types
• Data type conversion

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Overview

• JavaScript recognizes the following five types of primitive values:

• Numbers – 42, 3.1415

• Logical (Boolean) – true/false

• Strings – “Hello”

• null

• undefined

• Objects and functions are the other fundamental elements in the


language

• You can think of objects as named containers for values, and functions
as procedures that your application can perform

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Overview

• JavaScript is a dynamically typed language

• Variables do not have type

• Data types are converted automatically as needed during script


execution

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Numbers

• All numbers in JavaScript are double-precision floating-point numbers,


that is, the 64-bit encoding of numbers specified by the IEEE 754
standard—commonly known as “doubles”

• Literals represent values in JavaScript

• Integers can be expressed in decimal (base 10), hexadecimal (base


16), and octal (deprecated)

• Decimal integer literal consists of a sequence of digits without a leading


0 (zero)

• Leading 0 (zero) on an integer literal indicates it is in octal

• Leading 0x (or 0X) indicates hexadecimal

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Numbers

• A floating-point literal can have the following parts:

• A decimal integer which can be signed (preceded by "+" or "-”)

• A decimal point (".”)

• A fraction (another decimal number)

• An exponent

• The syntax is:

• [(+|-)][digits][.digits][(E|e)[(+|-)]digits]

3.14
.333333333333333333
6.02e23 // 6.02 x 1023
1.4738223E-32 // 1.4738223 x 10-32

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Numbers

• Floating-point numbers are inaccurate

0.1 + 0.2 // 0.30000000000000004


((1 / 3) + 5 - 5) * 3 // 0.9999999999999991

• There are special values:

• NaN – not a number

• +Infinity

• -Infinity

• Use functions isNaN() and isFinite() to check these values

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Numbers
Special values

Constant Meaning
Infinity Special value to represent infinity
NaN Special not-a-number value
Number.MAX_VALUE Largest representable number
Number.MIN_VALUE Smallest (closest to zero) representable
number
Number.NaN Special not-a-number value
Number.POSITIVE_INFINITY Special value to represent infinity
Number.NEGATIVE_INFINITY Special value to represent negative infinity

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Strings

• The String global object is a constructor for strings, or a sequence of


characters

• A string is a sequence of zero or more Unicode characters enclosed


within single or double quotes (' or ")

• "" – empty string

• "a" – string with single character a

• The backslash character (\) has a special purpose in JavaScript strings.


Combined with the character that follows it, it represents a character
that is not otherwise representable within the string
'It\'s my \'example\''

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Strings
Escape sequences

Sequence Character represented


\0 The NUL character (\u0000)
\b Backspace (\u0008)
\t Horizontal tab (\u0009)
\n Newline (\u000A)
\v Vertical tab (\u000B)
\f Form feed (\u000C)
\r Carriage return (\u000D)
\" Double quote (\u0022)
\' Apostrophe or single quote (\u0027)
\\ Backslash (\u005C)

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Strings
Escape sequences

Sequence Character represented


\xXX The Latin-1 character
\uXXXX The Unicode codepoint

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Strings

• The first character in a string is character with index 0

• Working with strings


msg = "Hello, " + "world";
len = s.length;
lastChar = s.charAt(s.length-1)
sub = s.substring(1,4);
i = s.indexOf('a');

• Strings are immutable

• Operator == compares the values of strings

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Booleans
• The boolean data type has only two values

• true

• false

• Boolean values are generally the result of comparisons you make in


your JavaScript programs

• 0, -0, null, false, NaN, undefined, or the empty string ("") – false

• All other values, including any object or the string "false“ - true

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Functions

• A function is a piece of executable code that is defined by a JavaScript


program or predefined by the JavaScript implementation
function square(x) {
return x * x;
}

• In JavaScript, functions are values that can be manipulated by


JavaScript code

var square = function (x) {


return x * x;
}

• Functions defined in this way are sometimes called lambda function

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Objects

• An object is a collection of named values. These named values are


usually referred to as properties of the object

• Objects can be created using:

• new operator var o1 = new Object();

• Literal var o2 = {};

• Object.create var o3 = Object.create(null);

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Arrays

• An array is a collection of data values, just as an object is. While each


data value contained in an object has a name, each data value in an
array has a number, or index

• Array can be created using:

• new operator var a = new Array(1, 2, 3);

• Literal var a = [1, 2, 3];

• Function Array var a = Array(1, 2, 3);

• If only a single number passed to the Array( ), it specifies the length of


the array

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
null and undefined values
null

• null is a special value that indicates no value

• null is a value that represents no object

• null is a unique value

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
null and undefined values
undefined

• undefined value returned when you use either a variable that has been
declared but never had a value assigned to it, or an object property that
does not exist

• null and the undefined value are distinct

• the == equality operator considers them to be equal

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Object wrappers

• A corresponding object class is defined for strings, numbers and


booleans primitive data types

• String class

• Number class

• Boolean class

• These classes are wrappers around the primitive data types

• A wrapper contains the same primitive data value, but it also defines
properties and methods that can be used to manipulate that data

• JavaScript will automatically wrap/unwrap value when needed

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Object wrappers
// primitive value
var s = "primitive";

// object of String class


var s = new String("object");

Primitive value can be explicitly convert to object


// Number
var n = Object(42);
// Boolean
var f = Object(false);
// String
var s = Object("string");

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data type conversion
Value Context

String Number Boolean Object

undefined "undefined" NaN false Error

null "null" 0 false Error


Numeric
Nonempty String
As is value of true
string object
string or NaN
String
Empty string As is 0 false
object

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data type conversion
Value Context

String Number Boolean Object


Number
0 "0" As is false
object
NaN "NaN" As is false Number
object
Infinity "Infinity" As is true Number
object
Negative "-Infinity" As is true Number
infinity object
Any other String value of As is true Number
number number object

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data type conversion

Value Context

String Number Boolean Object


true "true" 1 As is Boolean
object
false "false" 0 As is Boolean
object
Object toString( ) valueOf( ), true As is
toString( ), or
NaN
true "true" 1 As is Boolean
object

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data type conversion

• The object is converted to a number by calling valueOf(). If the return is


not a number, then call toString () and the result is converted to a
number

• In string context use toString(), in the numerical - valueOf(), however, if


by the valueOf() is defined, the operation of concatenation is invoked
this method with subsequent conversion to a string, instead of toString()

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Data types
• Overview
• Number
• String
• Boolean
• Function
• Object
• Array
• null and undefined values
• Object wrappers for primitive types
• Data type conversion

http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved

You might also like