Lecture # 2 - Introduction To Javascript
Lecture # 2 - Introduction To Javascript
JS <3
9 CS-213 Advance Programming
Linking to a JavaScript file: script
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
} JS
function changeText() {
//grab or initialize text here
integers and real numbers are the same type (no int
vs. double)
same operators: + - * / % ++ -- = += -= *= /= %=
similar precedence to Java
many operators auto-convert types: "2" * 3 is 6
// single-line comment
/* multi-line comment */
JS
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
JS
identical structure to Java's if/else statement
JavaScript allows almost anything as a condition
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
} JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS