JS Course
JS Course
1 Intro to Javascript
CS380
CS380
21/02/2024
CS380
CS380
21/02/2024
What is Javascript?
5
CS380
What is Javascript?
6
CS380
21/02/2024
Javascript vs Java
7
similarities:
both are interpreted, not compiled
both are relaxed about syntax, rules, and types
CS380
21/02/2024
differences:
JS is more object-oriented: noun.verb(), less
procedural: verb(noun)
JS focuses on user interfaces and interacting with a
document; PHP is geared toward HTML output and
file/form processing
JS code runs on the client's browser; PHP code runs
on the web server
JS <3
CS380
CS380
21/02/2024
Event-driven programming
11
CS380
Event-driven programming
13
CS380
Buttons
14
CS380
21/02/2024
JavaScript functions
15
function name() {
statement ;
statement ;
...
statement ;
} JS
function myFunction() {
alert("Hello!");
alert("How are you?");
} JS
Event handlers
16
Accessing elements:
document.getElementById
19
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
} JS
CS380
Accessing elements:
document.getElementById
20
CS380
21/02/2024
Preetify
22
function changeText() {
//grab or initialize text here
CS380
21/02/2024
CS380
Variables
24
Number type
25
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
CS380
// single-line comment
/* multi-line comment */
JS
Java/JS/PHP: // comment
PHP: # comment
CS380
21/02/2024
Math object
27
CS380
Logical operators
29
"5.0" == 5 is true
CS380
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
CS380
21/02/2024
Boolean type
31
CS380
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
CS380
21/02/2024
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
CS380
Popup boxes
34
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
CS380
21/02/2024
Arrays
35
CS380
Array methods
36
String type
37