Computer Science
Grade 8
20 min
JavaScript Fundamentals
JavaScript Fundamentals
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Declare and initialize variables using `let` and `const`.
Identify and use the three primary data types: String, Number, and Boolean.
Write a basic function that accepts parameters and returns a value.
Use `console.log()` to display output and debug simple programs.
Create and access elements in a simple array.
Explain how JavaScript makes web pages interactive.
Ever wondered how a website knows your name or how a simple click can change what you see on a page? 🤔 That's the magic of JavaScript!
In this lesson, you'll learn the basic building blocks of JavaScript, the programming language of the web. We'll explore how to store information, make decisions, and create reusable blocks of code that bring websites to life.
Real-World Applicati...
2
Key Concepts & Vocabulary
TermDefinitionExample
VariableA container for storing data values. In JavaScript, we use `let` for variables that can change and `const` for variables that stay constant.`let score = 100;` or `const playerName = 'Alex';`
Data TypeThe type of data a variable can hold. The most common types are String (text), Number (numeric values), and Boolean (true or false).`'Hello World'` is a String, `42` is a Number, `true` is a Boolean.
FunctionA reusable block of code designed to perform a specific task. Functions can take inputs (called parameters) and can return an output.`function greet(name) { return 'Hello, ' + name; }`
ArrayA special variable that can hold more than one value at a time, stored in an ordered list. Each item in an array has an index, starting from...
3
Core Syntax & Patterns
Variable Declaration Syntax
`let variableName = value;` or `const constantName = value;`
Use `let` when you expect the variable's value to change later in the program. Use `const` for values that should never change, like a player's birth date or a game's title.
Function Definition Syntax
`function functionName(parameter1, parameter2) { // code to run
return result; }`
Use this structure to create a named, reusable block of code. The `return` keyword sends a value back out of the function when it's finished.
String Concatenation
`'string1' + ' ' + variableName`
The plus sign `+` is used to join (concatenate) strings together. It's often used to combine text with the values stored in variables to create dynamic messages....
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 programmer writes this code, but nothing appears in the console. What is the most likely reason? `function calculateTotal(base, bonus) { return base + bonus; } let finalScore = calculateTotal(100, 20);`
A.The `console.log()` command was never used to display the result.
B.The function was not called correctly.
C.The variables `base` and `bonus` were not declared.
D.The code has a syntax error and cannot run.
Challenging
Analyze the following two code blocks. Which one will cause an error and why?
Block A: `const teamName = 'Eagles'; teamName = 'Hawks';`
Block B: `let score = 10; score = 20;`
A.Block A, because you cannot reassign a value to a `const` variable.
B.Block B, because you cannot reassign a value to a `let` variable.
C.Both will cause an error.
D.Neither will cause an error.
Challenging
You are given the code: `let values = [10, 'ten', true];`. What are the data types of `values[0]`, `values[1]`, and `values[2]` respectively?
A.Number, Number, Boolean
B.String, String, String
C.Number, String, Boolean
D.Array, Array, Array
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free