document.write('hello world'); alert('yikes');
Web browsers have various commands to make things happen called functions . They are like verbs in a sentence.
alert();is a function
document.write(); prompt();
are also a functions. You can identify a function by the use of parenthesis.
Often, you pass an argument to the function inside the parenthesis.
alert('hello world');Functions generally do something complicated (than what you would expect a single line of code to do). In the above example,
hello world
is an argument
5 // integer numbers 5.25 // floating point numbers (fractional numbers) 10.3333333 // floating point numbers -100 // negative integer number -2.301 // negative floating point numberTo print the total value of 5 plus 15, you could use the + operator (you'll learn about operators later) and do something like this:
document.write(5 + 15);
'hello world' // is a string
"Internet Class" // is a string
'1' // is a string (not a number)
"as;kl29/P(983a" // is a string
hello world // is NOT a sting
// Read "Putting Quotes into Strings" (p28) for details
"Jane said "good day!" to John" // this is not correct
'Jane said "good day!" to John' // this is correct
"Jane said \"good day!\" to John" // this is correct
true // special keyword representing a boolean value true false // special keyword representing a boolean value falseStrictly speaking, in most programming languages true/false is interpreted as NOT OFF (true) or OFF. Thus any non-zero value is interpreted as true and a zero is interpreted as false.
Area of the square?
A variable is a container (basket) with a name. In the follwing example, x is a variable
alert('Hi Bob');The above statement only makes sense if everyone who visits the website is named Bob. You'd want your web page to present different names 'Hi Paul', 'Hi Jane'...etc. You want to do something like this:
alert('Hi ' + x);
var score; //declare and name a variable "score"var is a keyword that creates (declares) the variable. The second part of the statement names the variable, score.
var my_score; // declare my_score = 0; // the symbol "=" means assign. So, assign 0 to "score".You can shorten the above two lines into one
var your_score = 0; // declare and assign value your_score = 1; // reuse the variableYou can store strings, numbers, and Boolean values
/* numbers */ var myAge = 25; var myTemp = 70.5; /* strings */ var firstName = 'Peter'; var lastName = 'Parker'; var myAddress = "3203 SE Woodstock Ave"; var myCity = "Portland"; /* boolean: either true or false (or 1 or 0) */ var student = true;Once you assigned a value to a variable, you can access the value stored simply by using the variable's name.
alert(firstName); // This will print the string stored - Peter alert('firstName'); // Note that this will print the string 'firstName'
operator | what it does | expression | result |
---|---|---|---|
+ | addition | 5 + 1 | 6 |
+ | concatenation | "Reed" + " " + "College" | "Reed College" |
- | subtraction | 4 - 2 | 2 |
* | multiplication | 2 * 3 | 6 |
/ | division | 6 / 3 | 2 |
% | modulus (remainder) | 10 / 3 | 1 |
var price = 10; var itemsOrdered = 15; var totalCost = price * itemsOrdered;
Note that not all operations are equal. *, /, % take precidence over + and -.
4 + 5 * 10 // is equivalent to 4 + (5 * 10)To make sure the math works out the way you intend, use parentheses to group operations
(4 + 5) * 10Variables are reusable.
var score = 0; score = 10; score = score + 100; //score will have 110 after this score = score * 100; //score will have 11000 after this score = score / 100; //score will have 110 after thisIn fact this kind of operation is so common there are a shortcuts like these:
socre += 10; // score = score + 10 score -= 10; // score = score - 10; score *= 10; // score = score * 10; score /= 10; // score = score / 10; score++; // score = score + 1; score--; // score = score - 1;
var firstName = 'John'; var lastName = 'Smith'; var fullName = firstName + lastName; // JohnSmith var fullName = firstName + ' ' + lastName // John Smith
Here, the + operator convers the number to a string and concatenate it with the other string. (Otherwise it woudl try to add 10 to a string, which doesn't make sense).
var score = 10; var message = 'You scored ' + score + ' points.'; // You scored 10 points.
This is called automatic type conversion and can be tricky when adding.
var price = '5'; // user input is usually a string var shippingCost = 20; var totalCost = price + shippingCost; // 520To prevent this, put a + sign with no space inbetween before the variable. This tells the interpreter to try to convert the string to a number.
var price = '5';
var shippingCost = 20;
var totalCost = +price + shippingCost; // 25
or you could use the Number() function.
var price = '5'; var shippingCost = 20; var totalCost = Number(price) + shippingCost; //Since there are no concatenation when using the * multiplication operator, the JavaScript will convert it to a number whenever possible.