Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman
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
More Objectives Create a string Escape a problematic or meaningful character within a string Create a Boolean Perform arithmetic with numbers Format numbers
More Objectives Use constants and methods in the Math object Access the characters in a string Manipulate strings Convert between numbers and strings
A Simple Statement var myVar = 'easy peasy';
Declaring Variables var myVar; var thing1, thing2;
[GLOBAL] globalVar A Quick Glance at Scope var globalVar; function f() { var localVar; // Can also use globalVar here! } f() localVar
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
Valid Names fullName num _myVar $myVar price1
Assigning Values var num; num = 23; var str = ‘Hello, world!’; var num = 23, str = ‘Hello, world!’;
Simple Value Types Numbers Strings Booleans
Numbers Never quoted Can include an optional single decimal Can have an initial +/- Do not contain commas Only a single number type in JavaScript.
Arithmetic + - * / % += -= *= /= var n = 1; n *= 4; // n = 4
Formatting Numbers var n = ; n.toFixed(); // 123 n.toFixed(1); // 123.5
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);
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);
Strings Always quoted Can be empty Can use single or double quotation marks
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
Slicing Strings var language = 'JavaScript'; language.slice(4); // Script language.slice(0,4); // Java language.slice(0,-6); // Java language.slice(-6); // Script
Escaping Characters 'I\'ve got an idea.' "Chapter 4, \"Simple Variable Types\"" \n \r \\
Concatenation var message = 'Hello'; message = message + ', World'; message += '!';
Booleans true false
Type Conversions parseFloat() parseInt() toString() var shipping = document.getElementById('shipping').value; // String, say '4.50' var total = 25.00; total += shipping; // String! ' ' total += parseFloat(shipping); // Works! 29.50
Special Values null undefined NaN Infinity