Course outline
Module 1
Module 1: Introduction to JavaScript
Introduction to JavaScript
Knowledge check
Q1. What is JavaScript primarily used for?
- A. Styling web pages
- B. Adding interactivity to web pages ✓
- C. Storing data on a server
- D. Creating databases
Why: JavaScript is primarily used to add interactivity and dynamic behavior to web pages.
Q2. Which of the following is a valid JavaScript variable name?
- A. 2variable
- B. variable-name ✓
- C. variable name
- D. var
Why: Variable names must start with a letter, underscore, or dollar sign, and cannot contain hyphens.
Q3. What does the console.log() function do in JavaScript?
- A. It displays a message in a dialog box
- B. It logs a message to the browser console ✓
- C. It changes the background color of a webpage
- D. It plays a sound
Why: console.log() is used to print messages to the browser console for debugging purposes.
Q4. Which symbol is used to assign a value to a variable in JavaScript?
Why: The equals sign (=) is used to assign a value to a variable in JavaScript.
Q5. What is the correct syntax to declare a function in JavaScript?
- A. function myFunction() { ... } ✓
- B. myFunction() { ... }
- C. function: myFunction() { ... }
- D. def myFunction() { ... }
Why: The correct syntax to declare a function in JavaScript is 'function myFunction() { ... }'.
Module 2
Module 2: Understanding Variables and Data Types
Understanding Variables and Data Types
Knowledge check
Q1. What is the correct way to declare a variable in JavaScript?
- A. var myVar = 10; ✓
- B. var myVar = 10;
- C. myVar = 10;
- D. variable myVar = 10;
Why: Variables in JavaScript are declared using the var, let, or const keywords. The correct syntax is var myVar = 10;.
Q2. Which of the following is a valid data type in JavaScript?
- A. Integer
- B. String ✓
- C. Float
- D. Decimal
Why: JavaScript has several data types, including String, Number, Boolean, Null, Undefined, and more. Integer and Float are not specific data types in JavaScript.
Q3. What is the result of the following expression: 5 + '5'?
- A. 10
- B. 55 ✓
- C. Error
- D. NaN
Why: When a number is added to a string, JavaScript converts the number to a string and concatenates them, resulting in '55'.
Q4. Which keyword is used to declare a constant in JavaScript?
- A. var
- B. let
- C. const ✓
- D. constant
Why: The const keyword is used to declare a constant in JavaScript, which cannot be reassigned after initialization.
Q5. What is the correct way to write a comment in JavaScript?
- A. // This is a comment ✓
- B. /* This is a comment */
- C. -- This is a comment
- D. # This is a comment
Why: In JavaScript, single-line comments are written using //, and multi-line comments are written using /* ... */.
Module 3
Module 3: Writing Your First Function
Writing Your First Function
Knowledge check
Q1. What is a function in JavaScript?
- A. A variable that stores data
- B. A block of code that performs a specific task ✓
- C. A type of loop
- D. A way to declare a class
Why: A function is a block of code that performs a specific task when called.
Q2. How do you define a function in JavaScript?
- A. Using the function keyword ✓
- B. Using the var keyword
- C. Using the let keyword
- D. Using the const keyword
Why: In JavaScript, functions are defined using the function keyword followed by the function name and parentheses.
Q3. What is the syntax for calling a function?
- A. functionName() ✓
- B. functionName
- C. function functionName()
- D. functionName: value
Why: To call a function, you use the function name followed by parentheses.
Q4. What is the purpose of parameters in a function?
- A. To declare variables outside the function
- B. To pass values into the function ✓
- C. To return a value from the function
- D. To define the function name
Why: Parameters are used to pass values into a function when it is called.
Q5. What does the return statement do in a function?
- A. It ends the function execution and sends a value back ✓
- B. It declares a variable inside the function
- C. It starts a loop inside the function
- D. It prints a message to the console
Why: The return statement is used to exit a function and optionally return a value to the caller.
Module 4
Module 4: Working with Arrays and Objects
Working with Arrays and Objects
Knowledge check
Q1. What is an array in JavaScript?
- A. A collection of key-value pairs
- B. A list of values stored in a single variable ✓
- C. A function that performs a specific task
- D. A data type that represents true or false
Why: An array is a list of values stored in a single variable, indexed by numerical positions.
Q2. How do you access the third element in an array called fruits?
- A. fruits[2] ✓
- B. fruits(3)
- C. fruits[3]
- D. fruits.third
Why: Arrays are zero-indexed, so the third element is at index 2.
Q3. Which of the following is a valid way to declare an object in JavaScript?
- A. let person = [name: 'John', age: 30];
- B. let person = {name: 'John', age: 30}; ✓
- C. let person = (name: 'John', age: 30);
- D. let person = <name: 'John', age: 30>
Why: Objects in JavaScript are declared using curly braces {} with key-value pairs.
Q4. What does the following code do? let numbers = [1, 2, 3]; numbers.push(4);
- A. It removes the last element from the array
- B. It adds the number 4 to the end of the array ✓
- C. It replaces the first element with 4
- D. It inserts 4 at the beginning of the array
Why: The push() method adds one or more elements to the end of an array.
Q5. Which method is used to remove the last element from an array?
- A. pop() ✓
- B. shift()
- C. unshift()
- D. splice()
Why: The pop() method removes the last element from an array and returns that element.
Module 5
Module 5: Handling User Input with Events
Handling User Input with Events
Knowledge check
Q1. Which event is triggered when a user clicks on an HTML element?
- A. onmouseover
- B. onclick ✓
- C. onchange
- D. onload
Why: The onclick event is triggered when a user clicks on an HTML element.
Q2. Which of the following is used to get the value of an input field in JavaScript?
- A. document.getElementById("inputId").value
- B. document.getElementById("inputId")
- C. document.querySelector("input").value
- D. Both A and C ✓
Why: Both methods can be used to get the value of an input field.
Q3. What does the 'onsubmit' event do?
- A. Triggers when a form is submitted ✓
- B. Triggers when a button is clicked
- C. Triggers when a page is loaded
- D. Triggers when an input field changes
Why: The 'onsubmit' event is triggered when a form is submitted.
Q4. Which of the following is NOT an event handler?
- A. onmouseover
- B. onclick
- C. onalert ✓
- D. onchange
Why: 'onalert' is not a valid event handler in JavaScript.
Q5. What is the purpose of the 'event.preventDefault()' method?
- A. To stop the default action of an event ✓
- B. To trigger an event
- C. To remove an event listener
- D. To log an event to the console
Why: The 'event.preventDefault()' method is used to stop the default action of an event.
Module 6
Module 6: Basic DOM Manipulation
Basic DOM Manipulation
Knowledge check
Q1. What does DOM stand for?
- A. Document Object Model ✓
- B. Dynamic Object Management
- C. Data Object Manipulation
- D. Digital Operating Module
Why: DOM stands for Document Object Model, which represents the structure of HTML documents as a tree of objects.
Q2. Which method is used to select an element by its ID in the DOM?
- A. document.querySelector()
- B. document.getElementById() ✓
- C. document.getElementsByClassName()
- D. document.getElementsByName()
Why: document.getElementById() is the correct method to select an element by its ID in the DOM.
Q3. Which of the following is used to change the content of an HTML element?
- A. innerHTML
- B. textContent
- C. innerText
- D. All of the above ✓
Why: innerHTML, textContent, and innerText can all be used to change the content of an HTML element, though they have slight differences in behavior.
Q4. What is the purpose of the 'style' property in DOM manipulation?
- A. To change the HTML structure
- B. To modify the CSS styles of an element ✓
- C. To add new elements to the DOM
- D. To remove elements from the DOM
Why: The 'style' property is used to modify the CSS styles of an element directly in the DOM.
Q5. Which method is used to add a new element to the DOM?
- A. document.createElement() ✓
- B. document.createNode()
- C. document.addHTML()
- D. document.newElement()
Why: document.createElement() is the correct method to create a new element, which can then be added to the DOM.
Module 7
Module 7: Debugging and Common Errors
Debugging and Common Errors
Knowledge check
Q1. What is the purpose of using console.log() in JavaScript?
- A. To declare variables
- B. To display information in the browser console for debugging ✓
- C. To perform mathematical calculations
- D. To style web pages
Why: console.log() is used to print messages to the browser console, which helps developers debug their code by showing the current state of variables and program flow.
Q2. Which of the following is a common error when using variables in JavaScript?
- A. Using uppercase letters
- B. Forgetting to declare a variable ✓
- C. Using numbers as variable names
- D. Adding comments to the code
Why: Forgetting to declare a variable can lead to unexpected behavior or errors, especially in strict mode, as the variable may be treated as a global variable or cause a ReferenceError.
Q3. What does the error 'ReferenceError: variable is not defined' mean?
- A. The variable was declared but not used
- B. The variable is being used before it is declared
- C. The variable is not declared anywhere in the code ✓
- D. The variable is declared but has no value
Why: A ReferenceError occurs when a variable is used in the code but has not been declared anywhere, making it inaccessible to the JavaScript engine.
Q4. Which of the following is a valid way to declare a variable in JavaScript?
- A. var myVar
- B. myVar = 10
- C. Both A and B ✓
- D. Neither A nor B
Why: In JavaScript, you can declare a variable using 'var' or assign a value to a variable without using 'var', though the latter is not considered good practice in modern JavaScript.
Q5. What is the main purpose of debugging in JavaScript?
- A. To write faster code
- B. To find and fix errors in the code ✓
- C. To make the code look better
- D. To increase the size of the code
Why: Debugging is the process of identifying and resolving errors or bugs in the code to ensure it runs as intended.