JavaScript Basics_ Comprehensive Guide
JavaScript Basics_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Examples: 3
3. Functions 3
Syntax: 3
4. Control Flow 3
Conditional Statements: 3
Loops: 4
5. Arrays and Objects 4
Arrays: 4
Objects: 4
Examples 4
Example 1: Calculate the Sum of Two Numbers 4
Example 2: Check Even or Odd 4
Exercises 5
Exercise 1: Greet a User 5
Solution: 5
Exercise 2: Find the Largest Number 5
Solution: 5
Multiple-Choice Questions 5
Question 1: 5
Question 2: 5
Question 3: 6
Advanced Example: Create a To-Do List 6
JavaScript is a versatile programming language essential for creating dynamic and interactive
web applications. This guide introduces JavaScript fundamentals, including variables, data
types, operators, functions, and control flow, with examples, exercises, and quiz questions.
What is JavaScript?
JavaScript is a programming language that runs in the browser and allows developers to add
interactivity, handle events, and manipulate web page content.
Variables
Variables store data that can be used and modified in your program.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
var isStudent = true; // Boolean
Data Types
2. Operators
Examples:
● Arithmetic Operators: +, -, *, /, %
● Comparison Operators: ==, ===, !=, <, >, <=, >=
● Logical Operators: &&, ||, !
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a > b && b > 0); // true
3. Functions
Syntax:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Hello, Alice!
4. Control Flow
Conditional Statements:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
console.log("You are a minor.");
}
Loops:
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
Arrays:
Objects:
let person = {
name: "John",
age: 30
};
console.log(person.name); // John
Examples
4
}
}
console.log(isEven(10)); // Even
console.log(isEven(7)); // Odd
Exercises
Write a function that accepts a name as input and prints a greeting message.
Solution:
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice");
Write a function that takes three numbers as arguments and returns the largest.
Solution:
function findLargest(a, b, c) {
return Math.max(a, b, c);
}
console.log(findLargest(5, 10, 8)); // 10
Multiple-Choice Questions
Question 1:
1. var
2. let
3. const
4. Both 1 and 2
Question 2:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
Which operator checks both value and type?
1. ==
2. !=
3. ===
4. <=
Answer: 3. ===
Explanation: The === operator checks for strict equality, considering both value and type.
Question 3:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
const listItem = document.createElement("li");
listItem.textContent = task;
taskList.appendChild(listItem);
taskInput.value = "";
}
}
</script>
</body>
</html>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis