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.
let keyword (case-sensitive).const keywordvar used instead of let
_ or $)age is not the same as Age)let, const, function, or return)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}.\' \" \& \n \t \\length is a property (not a method, as it is in Java)+ (see the code above)null and undefinedundefined: 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() methodindexOf() 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 value> < >= <= && || !=== !==== !==== and !== are strict equality tests; checks both type and valueboolean
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.map(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 */