Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.

Similar presentations


Presentation on theme: "Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies."— Presentation transcript:

1 Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies

2 Objectives In this chapter you will: Study data types Use expressions and arithmetic, assignment, comparison, conditional, and logical operators Work with strings

3 Data Types A data type is a specific category of information that a variable contains. A variable’s specific data type is very important because the data type helps determine how much memory the computer will allocate for the data stored in the variable. The data type also governs the kinds of operations that can be performed on a variable.

4 Data Types Data types that can only be assigned a single value are called primitive types. Primitive types: Integer, Floating-point number, Boolean, String, Undefined, and Null. JavaScript supports three reference data types: Functions, Objects, and Arrays. Assigning a null value to a variable indicates that the variable does not contain a value.

5 Data Types Integer numbersPositive or negative numbers with no decimal places Floating-point numbers Positive or negative numbers with decimal places BooleanA logical value of true or false StringText such as “Hello World” UndefinedA variable that has never had a value assigned to it, has not been declared, or does not exist NullAn empty value

6 Data Types Strongly-typed programming languages require programmers to declare the data type of variables. Loosely-typed programming languages do not require the programmer to declare the data type of variables. JavaScript is a loosely-typed programming language.

7 Data Types The data type of a variable can be determined using the typeof() operator. JavaScript supports two numeric data types: Integers and Floating-point numbers. An Integer is a positive or negative number with no decimal place. Integer values can range from –9007199254740990 to 9007199254740990

8 Data Types A Floating-point numbers contain decimal places. Exponential notation, or scientific notation, is a shortened format for writing very large numbers, or numbers with many decimal places. The value of 10 is written with an uppercase or lowercase E.

9 Data Types 2.0e11 means “two times 10 to the eleventh power”. Floating-point values that exceed the largest positive value of +- 1.7976931348623157 x 10^ 308 result in a special value of Infinity. Floating-point values that exceed the smallest negative value of +- 5 x 10 ^ -324 result in a value of –Infinity.

10 Data Types A Boolean value is a logical value of true or false. An Array contains a set of data represented as a single variable name. Arrays are represented in JavaScript by the Array object. Array() is a constructor function. Each piece of data in the Array is called an element.

11 Data Types The numbering of elements in the array starts at zero. A specific array element is referred to by enclosing its index number in brackets at the end of the array name. In JavaScript, the values assigned to an array element can be different data types.

12 Data Types When an array is created with the Array() constructor function, declaring the number of array elements is optional. The size of an array can change dynamically. The number of elements in the array can be determined using the length property. Values can be assigned to array elements when the array is first created.

13 Expressions and Operators An expression is a combination of literal values, variables, operators, and other expressions that can be evaluated by the JavaScript interpreter to produce a result. Operands are variables and literals contained in an expression. Operators are symbols used in expressions to manipulate operands.

14 Expressions and Operators A binary operator requires an operand before and after the operator. A unary operator requires a single operand either before or after the operator. Arithmetic operators are used to perform mathematical calculations. The prefix operator is placed before a variable.

15 Expressions and Operators Operator Type Description ArithmeticUsed for performing mathematical calculations AssignmentAssigns values to variables ComparisonCompares operands and returns a Boolean value LogicalUsed for performing Boolean operations on Boolean operands StringPerforms operations on strings

16 Expressions and Operators Operator Type Description SpecialUsed for various purposes that includes the conditional, instance of, in, delete, void, new, this, typeof and comma operators.

17 Expressions and Operators OperatorDescription + (addition)Adds two operands - (subtraction)Subtracts one operand from another * (multiplication)Multiplies one operand by another / (division)Divides one operand by another % (modulus)Divides one operand by another and returns the remainder

18 Expressions and Operators The postfix operator is placed after a variable. The increment (++) and decrement (--) unary operators can be used as prefix and postfix operators. Assignment operators are used for assigning a value to a variable. –Examples of assignment operators: =, +=, -=, *=, /=, and %=.

19 Expressions and Operators OperatorDescription ++ (increment)Increases an operand by a value of 1 -- (decrement)Decreases an operand by a value of 1 - (negation)Returns the opposite value of an operand

20 Expressions and Operators Comparison operators are used to compare two operands and determine if one numeric value is greater than another. –Examples of comparison operators: ==, ===, !=, !==, >, =, <=. The conditional operator executes one of two expressions, based on the result of a conditional expression. A conditional expression returns a Boolean value and determines whether to execute a conditional or looping statement.

21 Expressions and Operators The syntax of a conditional operator is: conditional expression ? expression1: expression2 Logical operators are used for comparing two Boolean operands for equality. –Examples of logical operators: &&, ||, and !

22 Expressions and Operators Operator precedence is the order of priority in which operations in an expression are evaluated. Expressions are evaluated from left to right, with the highest priority precedence evaluated first. If all the operators in the expression have the same priority of precedence, then the expression is evaluated from left to right.

23 Expressions and Operators Order of precedence from highest to lowest: –Parentheses/brackets/dot ( () [].) –Negation/increment (! - ++ -- typeof void) –Multiplication/division/modulus (* / %) –Addition/subtraction (+ -) –Comparison ( >=) –Equality (== !=) –Logical and (&&) –Logical or (||)

24 Expressions and Operators –Assignment operators (= += -= *= /= %=) –Comma (,)

25 Strings The escape character is a special character that tells the interpreter that the character following it has a special purpose. The escape character in JavaScript is ‘\’. The combination of the escape character with other characters is called an escape sequence. –Examples of escape sequences: \n, \b, \f, \\, \”…

26 Strings Escape SequenceCharacter \bBackspace \fForm feed \nNew line \rCarriage return \tHorizontal tab \’Single quotation mark \”Double quotation mark \\Backslash

27 Strings The concatenation operator for strings is +. The += assignment operator can be used to combine two strings. The JavaScript String object contains methods for manipulating text strings. Commonly used methods are: big(), blink(), bold(), charAt(index), fixed(), fontColor(color), fontSize(size), indexOf(text, index), italics()…

28 Strings The length property determines the number of characters in the string. Parsing refers to the act of extracting characters of substrings from a larger string.

29 Summary A data type is the specific category of information that a variable contains. Data types that can be assigned only a single value are called primitive types. Reference, or composite, data types can contain multiple values or complex types of information. Programming languages that require you to declare the data types of variables are called strongly-typed programming languages. Programming languages that do not require you to declare the data types of variables are called loosely- typed programming languages.

30 Summary An Integer is a positive or negative number with no decimal places. A Floating-point number is a number that contains decimal places or is written using exponential notation. A Boolean value is a logical value of true or false. An Array is a variable that contains a set of data represented by a single variable name. An expression is a combination of literal values, variables, operators, and other expressions that can be evaluated by the JavaScript interpreter to produce a result.

31 Summary Operands are variables and literals contained in an expression. Operators are symbols used in expressions to manipulate operands. Arithmetic operators are operators that are used to perform mathematical calculations, such as addition, subtraction, multiplication, and division, in JavaScript. Assignment operators are operators that are used for assigning a value to a variable. Comparison operators are operators that are used to compare two operands to determine if one numeric value is greater than another.

32 Summary The conditional operator executes one of two expressions based on the results of a conditional expression. Logical operators are used for comparing two Boolean operands for equality. Operator precedence is the order of priority in which operations in an expression are evaluated. An escape character is a special character that tells the compiler or interpreter that the character following it has a special purpose. Parsing refers to the act of extracting characters or substrings from a larger string. The JavaScript String object contains methods for manipulating text strings.


Download ppt "Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies."

Similar presentations


Ads by Google