Introduction to JavaScript
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
March 14, 2025

As an interpreted programming language, JS is great for interacting with a line at a time (similar to Python, but very different than Java). Where do we start?
The easiest way to dive in is with the Chrome browser’s Console tab in the same inspector tool we’ve used to inspect our HTML/CSS (press F12).

console.log() command is used to output values to the browser console, most often used to debug JS programs.System.out.println in Java or print in Python.
var name = expression; // define a variable globally
let name = expression; // define a variable with limited scope
const name = expression; // define a constant (cannot change) let keyword (case-sensitive).const keywordvar used instead of let
_ or $)age is not the same as Age)let, const, function, or return)let level = 23; // number
let accuracyRate = 0.99; // number
let name = "ITM WebProg"; // string
let flag = true; // boolean
let temps = [55, 60, 57.5];// array
typeof(name); // 'string'Note
Type conversion isn’t always what you expect as you will see in a moment.
int vs. double). All numbers in JS are floating point numbers.+ - * / % ++ -- = += -= *= /= %= and similar precedence to Java."2" * 3 is 6NaN(“Not a Number”) is a return value from operations that have an undefined numerical result (e.g. dividing a String by a Number).let name = "Yuna Kim"; // "Yuna Kim"
let fName = name.substring(0, name.indexOf(" ")); // "Yuna"
let len = nickName.length; // 8
let sport = 'Figure Skating'; // can use "" or ''
let message = `${name}, practices ${sport}`; // `` template literals
console.log(message) // 'Yuna Kim, practices Figure Skating'${expression}.let count = 10; // 10
let stringedCount = "" + count; // "10"
let puppyCount = count + " puppies, yay!"; // "10 puppies, yay!"
let magicNum = parseInt("42 is the answer"); // 42
let mystery = parseFloat("Am I a number?"); // NaN \' \" \& \n \t \\length is a property (not a method, as it is in Java)+ (see the code above)null and undefinedlet foo = null;
let bar = 9;
let baz;
/* At this point in the code,
* foo is null
* bar is 9
* baz is undefined
*/undefined: declared but has not yet been assigned a valuenull: exists, but was specifically assigned an empty value or null.
Note
This takes some time to get used to, and remember this slide if you get confused later.
let name = []; // empty array
let names = [value, value, ..., value]; // pre-filled
names[index] = value; // store element
let types = ["Electric", "Water", "Fire"];
let pokemon = []; // []
pokemon[0] = "Pikachu"; // ["Pikachu"]
pokemon[1] = "Squirtle";// ["Pikachu", "Squirtle"]
pokemon[3] = "Gengar"; // ["Pikachu", "Squirtle", undefined, "Gengar"]
pokemon[3] = "Abra"; // ["Pikachu", "Squirtle", undefined, "Abra"]length property (grows as needed when elements are added)string.split() methodarray.indexOf(element, fromIndex)
array.includes(element, fromIndex)
let pokemon = ["Pikachu", "Squirtle", "Gengar", "Abra"];
let index = pokemon.indexOf("Gengar");
console.log(index); // 2
console.log(pokemon.includes("Squirtle")); // true
console.log(pokemon.includes("Jigglypuff")); // falseindexOf() method is useful for finding the first index of a specific element within an array
includes() method is a simple and efficient way to check if an array contains a specific valueconst condition = true
const condition2 = true
if(condition) {
console.log("The condition is true")
} else if (condition2) {
console.log("The condition2 is true")
} else {
console.log("The condition and condition2 are false")
}> < >= <= && || !=== !==== !==== and !== are strict equality tests; checks both type and valuelet iLikeWebProg = true;
let myGrade = "A+" > 0; // false
if ("web programming is great") {
console.log("true");
}
if (0) { /* false */ }
//converting a value into a `boolean` explicitly:
let boolValue = Boolean(otherValue);
let boolValue = !!(otherValue); //double notboolean
0, 0.0, NaN, "", null, and undefinedfor and while statement// template
function name(params) {
statement;
return;
}
// defining a function
function greet(name) {
console.log(`Hello ${name}`);
}
//anonymous function
const sum = function (num1, num2) {
return num1 + num2;
};
// calling a function
greet('WebProg'); // Hello Webprog
console.log(sum(3, 4)); // 7function keyword.() => expression
param => expression
(param) => expression
(param1, paramN) => expression
() => {
statements
}
param => {
statements
}
(param1, paramN) => {
statements
}const numbers = [1, 2, 3, 4, 5]
const mapTransformation = numbers.map(el => el * 10) //10,20,30,40,50
const forEachTransformation = []
numbers.forEach(el => {
forEachTransformation.push(el * 10)
})
console.log(mapTransformation) // 10,20,30,40,50
console.log(forEachTransformation) // 10,20,30,40,50map(function) directly returns a new array with the applied transformation.forEach(function) method calls a function for each element in an array.let person = {
name: "Philip J. Fry", // string
age: 23, // number
"weight": 172.5, // number
friends: ["Farnsworth", "Hermes", "Zoidberg"], // array
};
person.age;
person["weight"]; // 172.5
person.friends[2]; // "Zoidberg"
let propertyName = "name";
console.log(person[propertyName]); // "Philip J. Fry"?.const user = {
name: "John",
address: {
street: "Main Street",
},
};
const otherUser = {
name: "Jane",
};
console.log(user.address?.street); // Main Street
console.log(otherUser.address?.street); // undefined
// without optional chaining:
console.log(user.address.street); // Main Street
console.log(otherUser.address.street); // TypeError: Cannot read properties of undefined (reading 'street')DOM manipulation and event-driven programming with JavaScript


Web Programming
Comments
Comments in HTML and CSS
<!-- comment -->/* comment */