Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry.

Similar presentations


Presentation on theme: "Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry."— Presentation transcript:

1 Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman

2 Objectives Declare a simple variable Recognize valid and invalid variable names Assign a simple value to a variable Begin to understand the concept of scope Create a number

3 More Objectives Create a string Escape a problematic or meaningful character within a string Create a Boolean Perform arithmetic with numbers Format numbers

4 More Objectives Use constants and methods in the Math object Access the characters in a string Manipulate strings Convert between numbers and strings

5 A Simple Statement var myVar = 'easy peasy';

6 Declaring Variables var myVar; var thing1, thing2;

7 [GLOBAL] globalVar A Quick Glance at Scope var globalVar; function f() { var localVar; // Can also use globalVar here! } f() localVar

8 Variable Names Must start with a letter, the underscore, or a dollar sign Can contain any combination of letters, underscores, and numbers Cannot use spaces, hyphens, or punctuation Cannot be a reserved word Are case-sensitive

9 Valid Names fullName num _myVar $myVar price1

10 Assigning Values var num; num = 23; var str = ‘Hello, world!’; var num = 23, str = ‘Hello, world!’;

11 Simple Value Types Numbers Strings Booleans

12 Numbers Never quoted Can include an optional single decimal Can have an initial +/- Do not contain commas Only a single number type in JavaScript.

13 Arithmetic + - * / % += -= *= /= ++ -- var n = 1; n *= 4; // n = 4

14 Formatting Numbers var n = 123.45; n.toFixed(); // 123 n.toFixed(1); // 123.5

15 Example var total; var quantity = document.getElementById('quantity').value; var price = document.getElementById('price').value; var tax = document.getElementById('tax').value; var discount = document.getElementById('discount').value; total = quantity * price; tax /= 100; tax++; total *= tax; total -= discount; // Format the total: total = total.toFixed(2);

16 The Math Object PI E abs() ceil() cos() floor() max() min() pow() round() random() sin() var area = Math.PI * radius * radius; var volumn = 4/3 * Math.PI * Math.pow(radius, 3);

17 Strings Always quoted Can be empty Can use single or double quotation marks

18 String Characters length charAt() indexOf() lastIndexOf() var name = 'Larry Ullman'; name.length; // 12 name.charAt(11); // n name.indexOf('a'); // 1 name.indexOf('man'); // 9 name.lastIndexOf('a'); // 10

19 Slicing Strings var language = 'JavaScript'; language.slice(4); // Script language.slice(0,4); // Java language.slice(0,-6); // Java language.slice(-6); // Script

20 Escaping Characters 'I\'ve got an idea.' "Chapter 4, \"Simple Variable Types\"" \n \r \\

21 Concatenation var message = 'Hello'; message = message + ', World'; message += '!';

22 Booleans true false

23 Type Conversions parseFloat() parseInt() toString() var shipping = document.getElementById('shipping').value; // String, say '4.50' var total = 25.00; total += shipping; // String! '25.004.50' total += parseFloat(shipping); // Works! 29.50

24 Special Values null undefined NaN Infinity


Download ppt "Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry."

Similar presentations


Ads by Google