JavaScript for Beginners
JavaScript for Beginners
Welcome to the world of JavaScript! This tutorial is designed to help beginners learn
JavaScript from scratch. We'll cover the fundamentals, provide coding examples, and
include quiz questions to test your understanding. Let's get started!
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
JavaScript Tutorial for Beginners 1
Introduction to JavaScript 3
Why Learn JavaScript? 3
Setting Up Your Environment 3
Your First JavaScript Program 4
Basics of JavaScript Syntax 4
Comments 4
Case Sensitivity 5
Semicolons 5
Variables and Data Types 5
Declaring Variables 5
Data Types 5
Quiz Question 6
Operators 6
Arithmetic Operators 6
Assignment Operators 7
Comparison Operators 7
Quiz Question 7
Conditional Statements 8
if Statement 8
if...else Statement 8
if...else if...else Statement 8
switch Statement 8
Example 9
Quiz Question 9
Loops 10
for Loop 10
while Loop 10
do...while Loop 11
Quiz Question 11
Functions 11
Declaring a Function 11
Calling a Function 12
Example 12
Quiz Question 12
Arrays 13
Creating an Array 13
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
Accessing Array Elements 13
Array Methods 13
Quiz Question 13
Objects 14
Creating an Object 14
Accessing Object Properties 14
Adding/Updating Properties 14
Methods in Objects 14
Quiz Question 15
DOM Manipulation 15
Selecting Elements 15
Changing Content 16
Event Listeners 16
Quiz Question 16
Introduction to JavaScript
JavaScript is a versatile programming language commonly used to create interactive
effects within web browsers. It's one of the core technologies of the World Wide Web,
alongside HTML and CSS.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
Your First JavaScript Program
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript Program</title>
</head>
<body>
<h1>Hello, World!</h1>
<script>
alert('Hello, World!');
</script>
</body>
</html>
Save the file as index.html and open it in your browser. You should see an alert box
displaying "Hello, World!".
Comments
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
Multi-line comment:
/*
This is a
multi-line comment
*/
Case Sensitivity
Semicolons
Semicolons are used to separate statements. While they are optional in many cases, it's
good practice to include them.
Declaring Variables
Example:
Data Types
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
● String: Text data ('Hello', "World")
● Number: Numeric data (42, 3.14)
● Boolean: Logical data (true, false)
● Null: No value (null)
● Undefined: A declared variable without a value (undefined)
● Object: Complex data structures
Quiz Question
A. var
B. let
C. const
D. variable
Answer: C. const
Operators
Operators perform operations on variables and values.
Arithmetic Operators
● Addition (+)
● Subtraction (-)
● Multiplication (*)
● Division (/)
● Modulus (%): Remainder of division
Example:
let sum = 10 + 5; // 15
let product = 10 * 5; // 50
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
Assignment Operators
● =: Assigns value
● +=: Adds and assigns
● -=: Subtracts and assigns
Example:
let x = 10;
x += 5; // x is now 15
Comparison Operators
● Equal (==)
● Strict Equal (===)
● Not Equal (!=)
● Greater than (>)
● Less than (<)
Example:
Quiz Question
let result = 10 % 3;
A. 0
B. 1
C. 3
D. 7
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Answer: B. 1
Conditional Statements
Conditional statements perform different actions based on different conditions.
if Statement
if (condition) {
// code to execute if condition is true
}
if...else Statement
if (condition) {
// code if true
} else {
// code if false
}
switch Statement
switch(expression) {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
8
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example
let score = 85;
Quiz Question
let num = 7;
if (num % 2 === 0) {
console.log('Even');
} else {
console.log('Odd');
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
9
A. Even
B. Odd
C. Error
D. undefined
Answer: B. Odd
Loops
Loops are used to execute a block of code repeatedly.
for Loop
for (initialization; condition; increment) {
// code to execute
}
Example:
while Loop
while (condition) {
// code to execute
}
Example:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
10
let i = 0;
while (i < 5) {
console.log('Iteration number ' + i);
i++;
}
do...while Loop
do {
// code to execute
} while (condition);
Quiz Question
A. 4
B. 5
C. 6
D. Infinite
Answer: B. 5
Functions
Functions are reusable blocks of code that perform a specific task.
Declaring a Function
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
11
function functionName(parameters) {
// code to execute
}
Calling a Function
functionName(arguments);
Example
function greet(name) {
return 'Hello, ' + name + '!';
}
Quiz Question
function add(a, b) {
return a + b;
}
console.log(add(5, 7));
A. 12
B. 57
C. undefined
D. Error
Answer: A. 12
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
12
Arrays
Arrays are used to store multiple values in a single variable.
Creating an Array
let fruits = ['Apple', 'Banana', 'Cherry'];
Array Methods
Example:
fruits.push('Date');
console.log(fruits); // ['Apple', 'Banana', 'Cherry', 'Date']
Quiz Question
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
13
A. 3
B. 4
C. 5
D. Undefined
Answer: B. 4
Objects
Objects are collections of key-value pairs.
Creating an Object
let person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
Adding/Updating Properties
person.job = 'Developer';
person['age'] = 31;
Methods in Objects
let calculator = {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
14
add: function(a, b) {
return a + b;
}
};
Quiz Question
Q7: How do you access the age property of the student object?
let student = {
name: 'Emily',
age: 22
};
A. student.age
B. student('age')
C. student[age]
D. student::age
Answer: A. student.age
DOM Manipulation
DOM (Document Object Model) manipulation allows you to interact with and modify web
pages.
Selecting Elements
● document.getElementById()
● document.getElementsByClassName()
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
15
● document.querySelector()
● document.querySelectorAll()
Example:
<!DOCTYPE html>
<html>
<body>
<script>
let element = document.getElementById('demo');
element.style.color = 'red';
</script>
</body>
</html>
Changing Content
Event Listeners
button.addEventListener('click', function() {
// code to execute on click
});
Quiz Question
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
16
A. element.addEventListener()
B. element.onEvent()
C. element.listenEvent()
D. element.triggerEvent()
Answer: A. element.addEventListener()
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
17