Lecture Summary 4
Prepared by: Anthony Larrain
Introduction to JavaScript
- HTML is a markup language.
- JavaScript is a programming language.
- A programming language is a collection of words and symbols that can be combined specifying instructions that a computer can execute.
- You can use the JavaScript language to add Dynamic content to your web pages.
- Dynamic webpages can change their appearance each time you visit the page or when the user interacts with the page.
- A static page looks the same and behaves the same each time you load it in the browser.
- With HTML we can only write static pages.
- To use JavaScript in your web pages you embed the JavaScript code in the HTML file between an openning and closing
script
tag.
First Example
First JavaScript Example
Here you will find all sorts of far out and groovy stuff I am into.
Basic Terms and Features
- JavaScript is case sensitive
document and Document,
Are different words.
';'
- Words that we make up ...
name
. - Words that have special meaning in the language. These are called reserved words or key words.
var
- Words that other programmers make up.
document prompt write
"Enter your name"
is an example of a String
prompt
is a built-in word that comes with the JavaScript language that you can use to pop-up a window
to get data from the user.prompt(.......
=
is called the assignment operator. This is not the equal symbol from Math.var name = prompt("Enter your name", "");
will
- Create a variable called
name
- Pop-up a dialog box that asks for your name.
- Return the string that you enter and assign it to variable
name
First JavaScript Example
Here you will find all sorts of far out and groovy stuff I am into.
document.write("Hello Class");
' + '
has multiple meanings in JavaScript. One usage is for string concatenation.document.write("Hello " + "Class");
var name = "Class"; document.write("Hello " + name);
The above would display
Hello Class
Not
Hello name
document.write(...
command will write text to then HTML file, you can add HTML tags to the text.Hello Class
);JavaScript Variables
- A variable is a location in computer memory that contains data.
- The name of a Javascript variable must begin with a letter or the underscore character ' _ ' and consist entirely of letters,digits or the underscore character.
- Variable names cannot contain spaces.
- Valid variable names.
temperature count my_old_school just4u
2cool4school #lookout first number
var temperature;
temperature = 68.99
Arithmetic Operators
- Can use arithmetic operators to perform computations on number or variables.
+ addition - subtraction * multiplication / division % remainder
var first, second, result; first = 10; second = 5; result = first + second // result is 15 result = first + 10 // result is 20 result = first / second // result is 2 result = second * 3 // result is 15 result = result + 1 // result is 16 result = first % 8 // result is 2
- Evaluate the operators within parentheses first. If parentheses are nested, evaluate the operators in the inner most pair first.
- Next do, multiplication, division and modulus in the order they appear from left to right.
- Then do, Addition and subtraction from left to right.
var first, second, result; first = 10; second = 5; result = first + 3 * 4 // result is 22 result = (first + 3) * 4 // result is 52
Temperature Program
Write a Javascript program that prompts the user for a temperature in Fahrenheit and converts it to Celsius. The formula is
celsius = 5/9 * (fahrenheit - 32)