Computer Science
Grade 8
20 min
Introduction to JavaScript
Introduction to JavaScript
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what JavaScript is and explain its role in making websites interactive.
Declare and initialize variables using `let` and `const`.
Identify and use basic data types: strings, numbers, and booleans.
Write a simple JavaScript program that uses `console.log()` to display output in the browser's developer console.
Perform basic arithmetic operations (+, -, *, /) on number variables.
Get user input using the `prompt()` function and store it in a variable.
Link an external JavaScript file to an HTML document using the `<script>` tag.
Ever wonder how websites become interactive, with games, quizzes, and cool animations? 🤔 That's the magic of JavaScript!
In this lesson, you'll learn the basics of JavaScript, the programming language...
2
Key Concepts & Vocabulary
TermDefinitionExample
VariableA named container for storing data values. Think of it like a labeled box where you can put information that your program can use or change.`let playerScore = 100;` Here, `playerScore` is the variable, and it holds the number `100`.
Data TypeThe type of data a variable can hold. The three basic types are strings (text), numbers, and booleans (true/false).`let name = "Ada";` (String), `let age = 14;` (Number), `let isStudent = true;` (Boolean).
StringA sequence of characters, like text, enclosed in single ('') or double ("") quotes.`let greeting = "Hello, world!";`
NumberA numerical value, including integers and decimals. Numbers are written without quotes.`let temperature = 21.5;` or `let itemsInCart = 3;`
BooleanA data...
3
Core Syntax & Patterns
Variable Declaration
`let variableName = value;` or `const constantName = value;`
Use `let` for variables whose value might change later in the program. Use `const` for constants whose value will never change. Every complete statement should end with a semicolon `;`.
Linking JavaScript to HTML
<script src="script.js"></script>
To make your JavaScript code run on a webpage, you must link the file. Place this tag inside your HTML file, usually right before the closing `</body>` tag, to ensure the HTML page loads first.
Getting User Input
let userInput = prompt("Your question for the user");
Use the `prompt()` function to display a dialog box that asks the user for input. The text the user types is always returned as a string and st...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A user runs this code: `let val1 = prompt("Enter first number:"); let val2 = prompt("Enter second number:"); let result = val1 + val2; console.log("The result is: " + result);`. If they enter `7` and then `3`, what is the exact output in the console and why?
A."The result is: 10" because JavaScript adds the numbers.
B."The result is: 73" because `prompt` returns strings, and the `+` operator concatenates them.
C.An error, because you cannot add strings.
D."The result is: NaN" because the input from prompt is not a number.
Challenging
You are debugging the 'Simple Age Calculator'. The code is: `let birthYear = Number(prompt("Year you were born:")); let age = 2024 - birthYear; console.log(age);`. A user reports the output is `NaN`. What did the user most likely type into the prompt?
A.2010
B.0
C."2010"
D.Two Thousand Ten
Challenging
Analyze the following code. What will be the two separate lines of output in the console? `let a = 5; let b = "5"; const c = 10; console.log(a + c); console.log(b + c);`
A.15, then 15
B.510, then 510
C.15, then 510
D.Error on the second line
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free