<!DOCTYPE html> <html> <head> </head> <body> <script src="myscript.js"> </script> // external file </body> </html>In this case, you are including code from the file myscript.js.
// content of myscript.js document.write("hello"); alert("world");
<!DOCTYPE html> <html> <head> </head> <body> <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="./js/myscript.js"> </script> <script> alert('hello world'); </script> </body> </html>Here you see three different ways of including javascript
document.write('hello world'); alert('yikes');
var price = 10; // this is a single line of comment /* This is a multiline comment. Author: Jane Smith The code makes your website look great. You use this by ... */
Area of the square?
A variable is a container (basket) with a name. In the follwing example, x is a variable
var x = 25;
x = 8.99;This is different from testing whether two things are equal to each other
//It's okay if you don't understand this. We'll go over this in the future if(a == b) {... do something ... } // correct, tests whether a and b are equal if(a = b) {... do something ... } // incorrect, assigns value of b to a
var netPrice; //declare and name a variable "netPrice"
netPrice = 100; // assign 100 netPrice = 20; // overwrite variable netPrice with 20 alert(netPrice); // alert will print 20
var netPrice2 = 100; netPrice2 = 99; // overwrite variable netPrice2 with 99 alert(netPrice2); // alert will print 99
var producetName = "Leather Wallet"; // since it is in quotes, we know it is a character // and not two variable namesNow that we have the character string stored, we can use them as the value that is containedvar producetName = Leather Wallet;
alert(productName);
var theSum = 4 + 3;
theSum = 11;
var productCount = 2; var subtotal = 14.80; var shipping = 4.75; var total = subtotal + shipping; // equivalent to: var total = 14.80 + 4.75 subtotal = total * 0.15; var productPrice = subtotal / productCount;
productCount++; // equivalent to productCount = productCount + 1; productCount--; // equivalent to productCount = productCount - 1;In the textbook, there are other shortend uses of operators
total += 5; // equivalent to total = total + 5; total -= 2; // equivalent to total = total - 2;
var a = 1; var b = 2; var c = 3; var average = a + b + c / 3; // this will result in a + b + (c/3) average = (a + b + c) / 3;
var firstname = "Amanda"; var lastname = "Reed" var fullname = firstname + lastname; // alert(fullname) --> this will display the text AmandaReed fullname = firstname + " " + lastname; // alert(fullname) --> this will display Amanda Reed
var name = "Amanda"; var number = 3203; alert(name + number); // this will display Amanda3203
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 of division) | 10 / 3 | 1 |
var answer = prompt("question");So to see it in full...
var name; var color; var age; name = prompt("What is your name?"); alert(name + " it is nice to meet you"); color = prompt("What is your favorite color " + name + "?"); age = prompt("Can I ask you for your age?"); if(Number(age) < 10){ //if(+age < 10){ alert(age + " years old? That is young!"); } if(Number(age) >= 10){ alert(age + " years old? That is old!"); }