M.

Home / Software Development Content

JavaScript Programming Beginner Course

  1. Basics of Programming using JavaScript
  2. JavaScript Introduction
  3. JavaScript in HTML Page
  4. JavaScript Variables
  5. JavaScript Data Types
  6. JavaScript Arithmetic Operators
  7. JavaScript Assignment Operators
  8. JavaScript Functions
  9. JavaScript Conditional Statements
  10. JavaScript Loops
  11. JavaScript Comments
  12. JavaScript Objects
  13. JavaScript Strings
  14. JavaScript Arrays
  15. JavaScript JSON
  16. JavaScript Debugging
  17. JavaScript integration with HTML Pages
  18. HTML DOM Introduction
  19. JavaScript Reading the HTML
  20. JavaScript Updating the HTML
  21. JavaScript Reading the CSS
  22. JavaScript Updating the CSS
  23. JavaScript Events and EventListener

Basics of Programming using JavaScript

Overview:

Key Concepts:

Examples:


          // Variable declaration and assignment
          let message = "Hello, World!";
          
          // Function declaration
          function greet(name) {
            return "Hello, " + name + "!";
          }
          
          // Calling the function
          console.log(greet("Alice")); // Outputs: Hello, Alice!
          
          // Conditional statement
          if (message === "Hello, World!") {
            console.log("The message is correct.");
          }
          
          // Loop
          for (let i = 0; i < 5; i++) {
            console.log(i); // Outputs numbers 0 through 4
          }
          

Exercises:

  1. Write a function that returns the sum of two numbers.
  2. Create a loop that prints numbers 1 through 10.
  3. Write a conditional statement that checks if a variable holds a value greater than 10 and logs a message if true.

Real-World Scenario:


JavaScript Introduction

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html>
          <head>
            <title>JavaScript Introduction</title>
          </head>
          <body>
            <h1>Welcome to JavaScript</h1>
            <script>
              // JavaScript code
              console.log("Hello, JavaScript!");
            </script>
          </body>
          </html>
          

Exercises:

  1. Create an HTML file with an embedded JavaScript code that displays "Welcome to JavaScript" in the console.
  2. Modify the script to show an alert box with the message "Hello World!".

Real-World Scenario:


JavaScript in HTML Page

Overview:

Examples:

<!DOCTYPE html>
          <html>
          <head>
            <title>JavaScript in HTML</title>
          </head>
          <body>
            <h1>JavaScript in HTML</h1>
            <!-- Internal JavaScript -->
            <script>
              document.write("Hello from internal JavaScript!");
            </script>
            <!-- External JavaScript -->
            <script src="script.js"></script>
          </body>
          </html>
          

script.js


          console.log("Hello from external JavaScript!");
          

Exercises:

  1. Create an HTML file with internal JavaScript that changes the background color of the page.
  2. Link an external JavaScript file that displays the current date and time in the console.

Real-World Scenario:


JavaScript Variables

Overview:

Examples:


          // Variable declarations
          var oldVariable = "Old"; // Function scope
          let newVariable = "New"; // Block scope
          const constantVariable = "Constant"; // Block scope and read-only
          
          // Using variables
          console.log(oldVariable);
          console.log(newVariable);
          console.log(constantVariable);
          

Exercises:

  1. Declare three variables: one using var, one using let, and one using const. Print their values.
  2. Attempt to reassign a value to a const variable and observe the result.

Real-World Scenario:


JavaScript Data Types

Overview:

Examples:


          // Primitive data types
          let name = "Alice"; // String
          let age = 30; // Number
          let isStudent = true; // Boolean
          let address; // Undefined
          let score = null; // Null
          
          // Non-primitive data types
          let person = { name: "Alice", age: 30 }; // Object
          let numbers = [1, 2, 3, 4, 5]; // Array
          
          console.log(name, age, isStudent, address, score);
          console.log(person);
          console.log(numbers);
          

Exercises:

  1. Create variables of each data type and print their types using typeof.
  2. Build an object that represents a book with properties like title, author, and year.

Real-World Scenario:


JavaScript Arithmetic Operators

Overview:

Operators:

Examples:


          let a = 10;
          let b = 5;
          
          console.log(a + b);  // Addition: 15
          console.log(a - b);  // Subtraction: 5
          console.log(a * b);  // Multiplication: 50
          console.log(a / b);  // Division: 2
          console.log(a % b);  // Modulus: 0
          console.log(a ** b); // Exponentiation: 100000
          

Exercises:

  1. Create a script that calculates the area of a rectangle (length * width) and logs it to the console.
  2. Write a function that takes two numbers and returns their average using arithmetic operations.

Real-World Scenario:


JavaScript Assignment Operators

Overview:

Operators:

Examples:


          let x = 10;
          x += 5;  // Equivalent to x = x + 5, x is now 15
          x -= 3;  // Equivalent to x = x - 3, x is now 12
          x *= 2;  // Equivalent to x = x * 2, x is now 24
          x /= 4;  // Equivalent to x = x / 4, x is now 6
          x %= 5;  // Equivalent to x = x % 5, x is now 1
          

Exercises:

  1. Write a script that uses assignment operators to update the value of a variable representing a bank account balance.
  2. Create a function that uses different assignment operators to calculate the final price of a product after applying discounts and taxes.

Real-World Scenario:


JavaScript Functions

Overview:

Function Declaration:


          function functionName(parameters) {
            // Code to execute
          }
          

          function add(a, b) {
            return a + b;
          }
          
          console.log(add(5, 3)); // Outputs: 8
          

Function Expressions:


          const multiply = function(x, y) {
            return x * y;
          };
          
          console.log(multiply(4, 6)); // Outputs: 24
          

Arrow Functions:


          const divide = (a, b) => a / b;
          
          console.log(divide(12, 4)); // Outputs: 3
          

Exercises:

  1. Create a function that takes a name as a parameter and returns a greeting message.
  2. Write a function that calculates and returns the factorial of a number.

Real-World Scenario:


JavaScript Conditional Statements

Overview:

Types of Conditional Statements:

Examples:


          let day = 2;
          
          if (day === 1) {
            console.log("Monday");
          } else if (day === 2) {
            console.log("Tuesday");
          } else {
            console.log("Other day");
          }
          
          // Switch case example
          let fruit = "apple";
          switch (fruit) {
            case "apple":
              console.log("This is an apple.");
              break;
            case "banana":
              console.log("This is a banana.");
              break;
            default:
              console.log("Unknown fruit.");
          }
          

Exercises:

  1. Write a script that uses if...else statements to check if a number is positive, negative, or zero.
  2. Create a function that uses a switch statement to return the name of the month based on a number (1-12).

Real-World Scenario:


JavaScript Loops

Overview:

Types of Loops:

Examples:


          // For loop
          for (let i = 0; i < 5; i++) {
            console.log(i); // Outputs numbers 0 through 4
          }
          
          // While loop
          let j = 0;
          while (j < 5) {
            console.log(j); // Outputs numbers 0 through 4
            j++;
          }
          
          // Do...while loop
          let k = 0;
          do {
            console.log(k); // Outputs numbers 0 through 4
            k++;
          } while (k < 5);
          

Exercises:

  1. Write a for loop that prints the first 10 even numbers.
  2. Create a while loop that calculates the sum of numbers from 1 to 100.

Real-World Scenario:


JavaScript Comments

Overview:

Types of Comments:

Examples:


          // This is a single-line comment
          
          /*
            This is a multi-line comment
            It can span multiple lines
          */
          
          let x = 10; // Initialize x with 10
          

Exercises:

  1. Add comments to a piece of code to explain what each section does.
  2. Write a JavaScript function with comments explaining its purpose, parameters, and return value.

Real-World Scenario:


JavaScript Objects

Overview:

Key Concepts:

Examples:


          let car = {
            make: "Toyota",
            model: "Corolla",
            year: 2020,
            start: function() {
              return "Car started";
            }
          };
          
          console.log(car.make); // Outputs: Toyota
          console.log(car.start()); // Outputs: Car started
          

Exercises:

  1. Create an object that represents a book with properties for title, author, and publication year. Add a method to display the book's details.
  2. Write a function that takes an object representing a student and returns a formatted string with their name and grade.

Real-World Scenario:


JavaScript Strings

Overview:

Key Concepts:

Examples:


          let message = "   JavaScript is awesome!   ";
          console.log(message.trim()); // Outputs: JavaScript is awesome!
          console.log(message.toUpperCase()); // Outputs: JAVASCRIPT IS AWESOME!
          console.log(message.substring(0, 10)); // Outputs: JavaScript
          console.log(message.replace("awesome", "fantastic")); // Outputs: JavaScript is fantastic!
          

Exercises:

  1. Write a script that takes a string and logs its length, uppercase version, and the first 5 characters.
  2. Create a function that replaces all occurrences of a word in a string with another word.

Real-World Scenario:


JavaScript Arrays

Overview:

Key Concepts:

Examples:


          let fruits = ["apple", "banana", "cherry"];
          fruits.push("date");
          console.log(fruits); // Outputs: ["apple", "banana", "cherry", "date"]
          
          fruits.pop();
          console.log(fruits); // Outputs: ["apple", "banana", "cherry"]
          
          let slicedFruits = fruits.slice(1, 3);
          console.log(slicedFruits); // Outputs: ["banana", "cherry"]
          

Exercises:

  1. Write a script that creates an array of numbers, adds a new number, and removes the first number.
  2. Create a function that takes an array of strings and returns a new array with all strings converted to uppercase.

Real-World Scenario:


JavaScript JSON

Overview:

Key Concepts:

Examples:


          let jsonData = '{"name": "Bob", "age": 25}';
          let dataObject = JSON.parse(jsonData);
          
          console.log(dataObject.name); // Outputs: Bob
          
          let newUser = { name: "Charlie", age: 35 };
          let jsonOutput = JSON.stringify(newUser);
          
          console.log(jsonOutput); // Outputs: {"name":"Charlie","age":35}
          

Exercises:

  1. Parse a JSON string representing a list of products and log each product's name.
  2. Convert a JavaScript object with user details into a JSON string and log it.

Real-World Scenario:


JavaScript Debugging

Overview:

Key Techniques:

Examples:


          function divide(a, b) {
            if (b === 0) {
              console.error("Cannot divide by zero.");
              return;
            }
            return a / b;
          }
          
          console.log(divide(10, 2)); // Outputs: 5
          console.log(divide(10, 0)); // Outputs: Cannot divide by zero.
          


JavaScript Integration with HTML Pages

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>JavaScript Integration</title>
            <script src="app.js"></script>
          </head>
          <body>
            <button onclick="showMessage()">Click Me</button>
          </body>
          </html>
          

          // app.js
          function showMessage() {
            alert("Hello from external script!");
          }
          

Exercises:

  1. Create an HTML page that uses both internal and external JavaScript to display messages and perform actions.
  2. Write a script that dynamically changes the content of a webpage based on user interaction.

Real-World Scenario:


HTML DOM Introduction

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>DOM Example</title>
          </head>
          <body>
            <h1 id="header">Hello, World!</h1>
            <script>
              // Access element by ID
              let header = document.getElementById('header');
              header.textContent = "Hello, DOM!";
            </script>
          </body>
          </html>
          

Exercises:

  1. Modify the content of an HTML element using JavaScript based on user interaction.
  2. Create a script that changes the style of an element when a button is clicked.

Real-World Scenario:


JavaScript Reading the HTML

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Read HTML</title>
          </head>
          <body>
            <p id="description" data-info="Additional Info">This is a paragraph.</p>
            <script>
              let paragraph = document.getElementById('description');
              console.log(paragraph.textContent); // Outputs: This is a paragraph.
              console.log(paragraph.getAttribute('data-info')); // Outputs: Additional Info
            </script>
          </body>
          </html>
          

Exercises:

  1. Write a script to read and log the content and attributes of various HTML elements.
  2. Create a function that extracts and displays information from an HTML element based on user interaction.

Real-World Scenario:


JavaScript Updating the HTML

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Update HTML</title>
          </head>
          <body>
            <div id="container"></div>
            <script>
              let container = document.getElementById('container');
              container.textContent = "New content added dynamically.";
              
              let newElement = document.createElement('p');
              newElement.textContent = "This is a new paragraph.";
              container.appendChild(newElement);
            </script>
          </body>
          </html>
          

Exercises:

  1. Create a script that updates the content and attributes of HTML elements based on user input.
  2. Write a function that dynamically adds new elements to a webpage, such as a list of items.

Real-World Scenario:


JavaScript Reading the CSS

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Read CSS</title>
            <style>
              #box {
                width: 100px;
                height: 100px;
                background-color: blue;
              }
            </style>
          </head>
          <body>
            <div id="box"></div>
            <script>
              let box = document.getElementById('box');
              let styles = getComputedStyle(box);
              console.log(styles.width); // Outputs: 100px
              console.log(styles.backgroundColor); // Outputs: rgb(0, 0, 255)
            </script>
          </body>
          </html>
          

Exercises:

  1. Write a script to read and log the computed styles of various HTML elements.
  2. Create a function that checks and reports whether an element has a certain style applied.

Real-World Scenario:


JavaScript Updating the CSS

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Update CSS</title>
            <style>
              #box {
                width: 100px;
                height: 100px;
                background-color: red;
              }
              .blue-background {
                background-color: blue;
              }
            </style>
          </head>
          <body>
            <div id="box"></div>
            <button onclick="changeStyle()">Change Style</button>
            <script>
              function changeStyle() {
                let box = document.getElementById('box');
                box.style.width = '200px';
                box.style.height = '200px';
                box.classList.add('blue-background');
              }
            </script>
          </body>
          </html>
          

Exercises:

  1. Create a script that updates the background color and dimensions of an HTML element based on user input.
  2. Write a function that toggles a CSS class on an element to apply different styles.

Real-World Scenario:


JavaScript Events

Overview:

Key Concepts:

Examples:

<!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>JavaScript Events</title>
          </head>
          <body>
            <button id="myButton">Click Me</button>
            <script>
              let button = document.getElementById('myButton');
              button.addEventListener('click', function() {
                alert('Button was clicked!');
              });
            </script>
          </body>
          </html>
          

Exercises:

  1. Write a script that handles a form submission event to prevent the default action and display a message instead.
  2. Create a function that responds to keyboard events by logging the key pressed.

Real-World Scenario: