Day 16: Control Structures and Functions
Quiz ahead1. Introduction to JavaScript as a Programming Language
Introduction to JavaScript as a Programming Language. Let's focus, in this lesson, a bit more on the programming aspect of Javascript. JavaScript is a versatile programming language primarily used for creating interactive and dynamic content on websites. It allows you to manipulate HTML and CSS, enabling rich user experiences.
Like all programming languages, JavaScript is built on fundamental concepts that every programmer needs to understand:Variables, Data Types, Operators, etc.
2. Control Structures
Control structures direct the flow of execution in your code based on certain conditions. They enable your program to make decisions and repeat actions.
Conditional Statements
Used to execute code based on whether a condition is true or false. Common conditional statements include if
, else
if
, and else
.
Example:
let age = 18; // Age variable
if (age >= 18) {
console.log("You are eligible to vote."); // Executes if condition is true
} else {
console.log("You are not eligible to vote."); // Executes if condition is false
}
Switch Statement
A switch statement is another way to handle multiple conditions.
Example:
let fruit = "apple";
switch (fruit) {
case "apple":
console.log("You chose an apple!");
break; // Exits the switch after the match
case "banana":
console.log("You chose a banana!");
break;
default:
console.log("Unknown fruit.");
}
Loops
Used to repeat a block of code multiple times until a specified condition is met. There are several types of loops.
- For Loop: Ideal when you know the number of iterations.
for (let i = 0; i < 5; i++) {
console.log("Count: " + i); // Logs 0 to 4
}
- While Loop: Continues as long as a condition is true.
let count = 0;
while (count < 5) {
console.log("Count: " + count);
count++; // Increment count by 1
}
- Do...While Loop: Executes the block of code at least once, then checks the condition.
let number = 0;
do {
console.log("Number: " + number);
number++;
} while (number < 5);
3. Functions
Functions are reusable blocks of code that perform a specific task. They help to organize code, making it more readable and easier to maintain. They allow you to write code once and reuse it, reducing redundancy and potential errors.
This is the most common way to define a function.
function greet(name) {
return "Hello, " + name + "!"; // Combines "Hello," with the provided name
}
console.log(greet("Alice")); // Calls the function and logs: Hello, Alice!
- Function Expression: Functions can also be defined as expressions and stored in variables.
const greet = function(name) {
return "Hello, " + name + "!";
};
console.log(greet("Bob")); // Logs: Hello, Bob!
- Arrow Functions: A concise way to write functions introduced in ES6.
const greet = (name) => "Hello, " + name + "!";
console.log(greet("Charlie")); // Logs: Hello, Charlie!
- Immediately Invoked Function Expression (IIFE): This is a function that runs as soon as it is defined.
(function(name) {
console.log("Hello, " + name + "!");
})("David"); // Logs: Hello, David!
Assignment 16
Write a Function:
Create a function called checkEvenOdd
that takes a number as an argument.
Inside the function, check if the number is even or odd. Return "Even" if it is even and "Odd" if it is odd.
Loop Through Numbers:
Use a loop to test the function checkEvenOdd
with numbers from 1 to 10. Log the results to the console.
Expected output
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd
6 is Even
7 is Odd
8 is Even
9 is Odd
10 is Even
Take Quiz