Section 03 Summary
Section 03 Summary
'use strict';
Strict mode creates visible errors where JavaScript would otherwise fail silently. For example:
Without strict mode, this code would silently create a new global variable hasDriverLicense (with
the typo) and the condition would never be true. With strict mode, JavaScript throws an error, making
the bug obvious.
Strict mode also reserves certain keywords for future JavaScript features and prevents using them as
variable names, like:
function logger() {
console.log('My name is Jonas!');
}
Functions can receive input data (parameters) and return output data:
In this example:
apples and oranges are parameters (placeholders)
5, 0, 2, and 4 are arguments (actual values)
The function processes these inputs and returns a string
Functions help us write DRY code (Don't Repeat Yourself), which is an important clean code principle.
Lecture 4: Function Declarations vs. Expressions
JavaScript offers multiple ways to create functions. The two main types are function declarations and
function expressions.
Function Declaration:
function calcAge1(birthYear) {
return 2037 - birthYear;
}
Function Expression:
The main difference between them is that function declarations can be called before they're defined
in the code (due to a process called hoisting), while function expressions cannot:
// This works
const age1 = calcAge1(1991);
function calcAge1(birthYear) {
return 2037 - birthYear;
}
Both have their place in JavaScript, and which one you use often comes down to personal preference.
Lecture 5: Arrow Functions
Arrow functions were introduced in ES6 as a shorter form of function expressions, especially useful
for simple one-line functions:
console.log(yearsUntilRetirement(1991, 'Jonas'));
console.log(fruitProcessor(2, 3));
In this example, the fruitProcessor function calls the cutPieces function twice. This approach
follows the DRY principle - if we later need to change how fruits are cut (e.g., cut into 3 pieces instead
of 4), we only need to change it in one place.
Lecture 7: Reviewing Functions
Let's review the key concepts we've learned about functions:
if (retirement >= 0) {
return `${firstName} retires in ${retirement} years!`;
} else {
return `${firstName} is already retired `; 🎉
}
};
console.log(yearsUntilRetirement(1991, 'Jonas'));
console.log(yearsUntilRetirement(1969, 'Mark'));
// Instead of this:
const friend1 = 'Michael';
const friend2 = 'Steven';
const friend3 = 'Peter';
console.log(friends[0]); // Michael
console.log(friends[2]); // Peter
const ages = [
calcAge(years[0]),
calcAge(years[1]),
calcAge(years[years.length - 1]),
];
console.log(ages); // [47, 70, 19]
Lecture 10: Basic Array Operations (Methods)
JavaScript provides built-in methods to manipulate arrays:
Adding elements:
// Add to end
friends.push('Jay');
console.log(friends); // ['Michael', 'Steven', 'Peter', 'Jay']
// Add to beginning
friends.unshift('John');
console.log(friends); // ['John', 'Michael', 'Steven', 'Peter', 'Jay']
Removing elements:
Finding elements:
console.log(friends.indexOf('Steven')); // 1
console.log(friends.indexOf('Bob')); // -1 (not found)
const jonas = {
firstName: 'Jonas',
lastName: 'Schmedtmann',
age: 2037 - 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
};
This is called an object literal syntax - we're literally writing down the object content.
Objects allow us to:
Give each piece of data a name (key or property name)
Store unstructured and related data
Access data by its name rather than position
Unlike arrays, the order of properties in objects doesn't matter when retrieving data.
Lecture 13: Dot vs. Bracket Notation
There are two ways to access object properties:
Dot Notation:
const jonas = {
firstName: 'Jonas',
lastName: 'Schmedtmann',
age: 2037 - 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
};
console.log(jonas.lastName); // 'Schmedtmann'
Bracket Notation:
console.log(jonas['lastName']); // 'Schmedtmann'
Bracket notation is useful when we don't know which property to access until runtime:
if (jonas[interestedIn]) {
console.log(jonas[interestedIn]);
} else {
console.log(
'Wrong request! Choose between firstName, lastName, age, job and
friends.',
);
}
const jonas = {
firstName: 'Jonas',
lastName: 'Schmedtmann',
birthYear: 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: true,
getSummary: function () {
return `${this.firstName} ${
this.lastName
} is a ${this.calcAge()}-year old ${this.job}. He has ${
this.friends.length
} friends and ${this.hasDriversLicense ? 'a' : 'no'} driver's
license.`;
},
};
Functions attached to objects are called methods. We can call them like this:
console.log(jonas.calcAge()); // 46
console.log(jonas.age); // 46
In object methods, the this keyword refers to the object calling the method. This allows methods to
access and manipulate the object's other properties.
The calcAge method not only returns the age but also creates a new age property on the object,
saving us from recalculating it every time.
Lecture 16: Iteration: The for Loop
Loops allow us to automate repetitive tasks. The for loop is commonly used when you know exactly
how many iterations you need:
const jonasArray = [
'Jonas',
'Schmedtmann',
2037 - 1991,
'teacher',
['Michael', 'Peter', 'Steven'],
];
const types = [];
console.log(types);
const jonasArray = [
'Jonas',
'Schmedtmann',
2037 - 1991,
'teacher',
['Michael', 'Peter', 'Steven'],
];
In this example, the inner loop (with rep) executes completely for each iteration of the outer loop
(with exercise), resulting in a total of 15 repetitions (3 exercises × 5 repetitions).
Lecture 19: The while Loop
The while loop is more versatile than the for loop because it only requires a condition:
let rep = 1;
while (rep <= 10) {
console.log(`Lifting weights repetition ${rep} `);
rep++;
}
The while loop is particularly useful when you don't know in advance how many iterations you need:
In this example, we keep rolling a die until we get a 6. Since we can't predict when we'll roll a 6, a
while loop is more appropriate than a for loop.