Webbo3 Level 2 · JavaScript Development · Day 1 of 20
The JavaScript Mental Model
Variables, types, operators and template literals: the vocabulary every line of JavaScript you ever write is made of.
By the end of today you can
- Explain what JavaScript actually adds to a page you already built
- Choose between
constandletwithout guessing - Understand the main JavaScript value types and identify what you are holding
- Use operators to calculate, compare and work with values
- Build readable strings with template literals
- Read your own output in the browser Console instead of guessing
Last month you built pages. Real ones, with Flexbox, Grid and Tailwind, and they looked good. But they did nothing. A button that goes nowhere. A form that forgets everything the moment you press submit. A price that never changes.
Today that starts to change.
HTML gives a webpage its structure. CSS controls its appearance. JavaScript gives the page behaviour. It allows a webpage to react, calculate, remember information and change things after the page has already loaded.
Think about an online shopping website. You click Add to Cart, the number beside the cart increases, the total price changes and the page may show a notification. HTML created the elements. CSS made them look good. JavaScript is what makes those elements respond to what you do.
1. Where JavaScript Actually Runs
Definition
A JavaScript engine is the program responsible for reading and executing JavaScript code. When you write JavaScript, something has to understand the instructions and perform them. That something is the JavaScript engine.
Real-Life Example
Imagine you give instructions to a person: "Take this amount of money, multiply it by three, then tell me the result." The person reads your instructions and performs them.
A JavaScript engine does something similar. You give it JavaScript instructions, and the engine reads and executes them.
Types of Places JavaScript Can Run
- Browser: JavaScript can run inside browsers such as Chrome, Firefox and Safari.
- Node.js: JavaScript can also run outside the browser using Node.js.
Browser JavaScript
Chrome and Edge use the V8 JavaScript engine. Firefox uses SpiderMonkey, while Safari uses JavaScriptCore.
When JavaScript runs in the browser, it can interact with the webpage. For example, it can change text, respond to clicks, validate forms and update elements.
Example
console.log() tells the engine to show a value. You will use it in almost every example this month, because it is how you see what your code is doing.
Node.js
Node.js allows JavaScript to run outside the browser. It is built around the V8 engine. This means you can use JavaScript to create servers, work with files, build command-line tools and create backend applications.
The terminal boxes in these lessons show what Node printed when the example was run. In the browser, the same console.log() output appears in the Console tab of the developer tools.
Overall Example
Imagine JavaScript as a language and the JavaScript engine as the person who understands that language. The language is the same, but you can place it in different environments.
Open your browser, press F12, select the Console tab and type the line below. Before pressing Enter, predict the answer.
Think of your browser as a kitchen and JavaScript as the chef. HTML gives the kitchen its structure, CSS decides how everything looks, while JavaScript is the chef actually doing things with the ingredients.
2. Variables: A Name for a Value
Definition
A variable is a named place used to store a value so that you can use that value later in your program.
Real-Life Example
Imagine you have a box labelled School Fees. Inside the box is ₦50,000. Instead of carrying the ₦50,000 around and writing the amount everywhere, you simply remember the name of the box: School Fees.
Variables work in a similar way. You give a value a name, then use that name whenever you need the value.
Types of Variable Declarations
const: used when the variable should not be reassigned.let: used when the value may need to be reassigned.var: an older way of declaring variables that you will mainly encounter in older JavaScript code.
Type 1: const
const creates a variable that cannot later be reassigned to another value.
Type 2: let
let is used when you expect the value to change.
Using const and let Together
Start with const by default. Use let only when you know the value needs to change.
What Happens When You Reassign const?
Because const protects a variable from being given a new value, JavaScript stops the program if you try.
A Common Confusion
const prevents the variable from being reassigned. It does not mean that every piece of data inside an object or array can never change.
In the example below, push() adds an item to the end of an array. You will meet it properly on Day 3.
The variable still points to the same array. You changed the contents of the array, not what the variable points to.
What About var?
You will see var in older JavaScript code. Modern JavaScript generally uses const and let.
For this course, recognise var when you see it, but build the habit of using const and let.
Create a variable containing your name and another containing your age. Print both values.
Your output will show your own name and age.
Think of a variable as a labelled container. The label tells you what is inside. const means the label cannot be moved to another container, while let allows you to point the label at a different value later.
3. The Types of Values You Are Actually Holding
Definition
A data type describes what kind of value JavaScript is working with. Knowing the type of a value is important because different types can behave differently.
Real-Life Example
Imagine a supermarket. You may have rice, money, a customer's name, a list of products, and a yes/no answer to whether a customer has paid.
They are all pieces of information, but they are not the same kind of information. JavaScript also needs to know what kind of value it is dealing with.
Main Types You Will Meet
| Type | What it holds | Example |
|---|---|---|
| String | Text | 'Chidinma' |
| Number | Numbers, whole or decimal | 2500 |
| Boolean | true or false | true |
| Undefined | A value has not been assigned | undefined |
| Null | Deliberately empty | null |
| Object | A collection of related values | { name: 'Ada' } |
| Array | An ordered list of values | ['rice', 'beans'] |
To ask JavaScript what type a value is, write typeof in front of it.
Type 1: String
A string is text. Real-life example: a person's name, a city, a course title or an email address.
Type 2: Number
A number represents numerical values. JavaScript uses one main number type for whole numbers and decimal numbers.
Type 3: Boolean
A boolean has only two possible values: true or false.
Real-life example: "Has the student paid?" The answer can be yes or no. In JavaScript, that can be represented with true or false.
Type 4: Undefined
undefined usually means a variable has been declared but has not yet been given a value.
Type 5: Null
null represents an intentionally empty value. You use it when you deliberately want to say that there is currently no value.
The second output surprises many beginners. JavaScript has a long-standing historical behaviour where typeof null returns "object".
Type 6: Object
An object allows you to group related information using named properties.
Real-life example: instead of keeping a student's name, age and course separately, you can group them together as one student object.
Type 7: Array
An array is an ordered collection of values. Real-life example: a shopping list containing rice, beans and bread.
JavaScript reports an array as object when you use typeof. If you specifically want to know whether a value is an array, use Array.isArray().
Overall Example
Imagine a student's information system. You might have their name as a string, their score as a number, whether they passed as a boolean, an array containing their courses, and an object containing all of their information.
Create variables for your name, age, whether you are a student, and a list of two technologies you want to learn. Then print them.
Think of data types as different kinds of containers. A string holds words, a number holds numerical values, a boolean answers a yes/no question, an array holds an ordered list, and an object groups related information together.
4. Operators: How JavaScript Works With Values
Definition
An operator is a symbol that tells JavaScript to perform an operation on one or more values.
Real-Life Example
When you calculate the total price of three items, you are performing an operation. When you ask whether someone's score is greater than 50, you are also performing an operation.
Main Operators We Will Use
| Operator | Meaning |
|---|---|
+ | Addition, or joining text |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder |
=== | Strict equality |
!== | Strict inequality |
Arithmetic Operators
The % operator gives you the remainder after division. For example, 10 divided by 3 gives 3 with 1 remaining, so the result is 1.
The Special Case: +
The + operator can perform arithmetic, but it can also join strings together. This is where beginners often get confused.
Why does '10' + 5 produce '105'? Because the first value is a string. JavaScript treats the + operation as joining text.
But '10' - 5 produces 5 because the minus operator is mathematical, so JavaScript converts the string into a number before performing the subtraction.
Values typed into HTML forms normally arrive as strings. So if a user enters 1500 and 3, you may actually receive "1500" and "3".
That is not what you wanted. You wanted 1500 × 3. Convert the values to numbers first.
Converting Text to Numbers
Number() converts a value into a number when the value can be converted successfully.
NaN means Not a Number. It indicates that JavaScript could not convert the supplied value into a valid number.
Number.isNaN().Create two values as strings, convert them to numbers, and multiply them.
5. Comparison: Understanding ===
Definition
A comparison operator allows JavaScript to check whether values are equal, different, greater, smaller or otherwise related.
Real-Life Example
Imagine a school checking whether a student's score is exactly 50. The computer needs to compare the student's score with 50 and produce either true or false.
Two Equality Operators
==: loose equality===: strict equality
The important difference is that strict equality checks both the value and the type.
Why Does 5 === '5' Return false?
The first value is a number. The second value is a string. Even though they visually contain the same digits, they are different types.
In modern JavaScript, use === when you want strict equality and !== when you want strict inequality.
Overall Example
Imagine a login system. The system may receive a user ID from a form as text, while another part of your application stores the ID as a number. Strict comparison makes the type difference visible instead of silently converting values.
Predict each result before running the code.
6. Template Literals: Stop Gluing Strings Together
Definition
A template literal is a JavaScript string written using backticks that allows you to insert variables and expressions directly into the string.
Real-Life Example
Imagine you want to send a customer a message: "Hi Chidinma, 3 bundles cost N4500."
The name, number of bundles and total price can all change depending on the customer. Template literals allow you to insert those changing values directly into the sentence.
Old Way: String Concatenation
Better Way: Template Literal
How ${} Works
Inside a template literal, ${...} tells "Evaluate what is inside these braces and place the result here."
You can insert variables as well as expressions, and a template literal can span several lines.
Why Template Literals Matter
Template literals become especially useful when your application needs to create dynamic text or HTML from data.
Instead of repeatedly breaking a sentence apart with +, you can write the sentence naturally and insert values where they belong.
Create a message containing your name, your course and your current learning day.
Think of a template literal as a sentence with empty spaces. The sentence is already written, and JavaScript fills the spaces with the current values.
7. Reading Your Own Output
Definition
The Console is a developer tool where you can see messages and results produced by JavaScript. One of the most common tools for displaying information there is console.log().
Real-Life Example
Imagine troubleshooting a car. You cannot simply stare at the car and guess what is wrong. You need information that tells you what is happening.
console.log() gives you that information while you are developing JavaScript.
Basic Example
Label Your Output
When you have many values, labels make the Console much easier to understand.
console.table()
When you have structured information, console.table() can make the information easier to inspect.
Create three variables representing a product name, price and quantity. Print them using console.log().
Do This Now
Now bring everything together. Do not just read these examples. Write the programs yourself, run them and predict the output before you run them.
1. Age Calculator
Given a birth year, calculate the person's age.
2. Transport Fare
Given a fare per trip and the number of trips per week, calculate the weekly cost.
3. Temperature Converter
Convert 37 degrees Celsius to Fahrenheit.
4. Score Summary
Given three test scores, calculate the total, average and whether the student passed.
Imagine that you are building a small Webbo3 course platform. A student called Musa buys three data bundles. Each bundle costs N1500. The system needs to remember Musa's name, remember how many bundles he bought, calculate the total cost, and display a message to him.
A variable can store Musa's name. A number can store the price. Another number can store the number of bundles. An operator can calculate the total. A template literal can create the final message. console.log() can show you the result.
Checkpoint
Q1. Why should const usually be your default?
Because const prevents the variable from being reassigned. This makes your intention clearer and prevents accidental reassignment. Use let when the value genuinely needs to change.
Q2. A form gives you "20" and "30". What does "20" + "30" produce?
It produces "2030" because both values are strings. The + operator joins the text instead of adding the numbers.
Q3. What is the difference between null and undefined?
undefined generally means that a variable has not been assigned a value. null is a value you deliberately assign to represent an empty value.
Q4. Why does typeof null return "object"?
It is a historical behaviour in JavaScript that has remained for compatibility reasons.
Tick Before You Move On
- ☐ I opened the Console and ran JavaScript inside it
- ☐ I understand what a variable is
- ☐ I know when to use const and when to use let
- ☐ I understand strings, numbers and booleans
- ☐ I understand undefined and null
- ☐ I know that arrays are objects according to typeof
- ☐ I understand why "10" + 5 produces "105"
- ☐ I know how to convert strings into numbers with Number()
- ☐ I understand the difference between == and ===
- ☐ I can use template literals
- ☐ I completed the practical exercises
Quick Recap
JavaScript gives webpages behaviour.
Variables allow you to store values using names.
Use const by default and let when you need to reassign a value.
JavaScript values have different types such as strings, numbers, booleans, undefined, null, objects and arrays.
Operators allow you to calculate and compare values.
Be careful when mixing strings and numbers because JavaScript can convert values automatically.
Use === when you want strict equality.
Template literals make dynamic strings easier to read.
Finally, use console.log() to inspect what your JavaScript is actually doing.
Final Scenario
Imagine you wake up tomorrow and you are asked to build a simple checkout system.
A customer has a name. The customer selects a product. The product has a price. The customer selects a quantity. Your program needs to calculate the total and display a friendly message.
You already have every basic idea needed to start:
- Variables to remember the information.
- Numbers to represent prices and quantities.
- Operators to calculate totals.
- Template literals to create the message.
- Console tools to inspect the result.
That is the mental model you should carry forward. You are no longer simply writing random JavaScript statements. You are storing information, understanding what type of information it is, performing operations on it and turning the result into something useful.
Tomorrow, Day 2: Decisions and functions. You will turn these values into logic that can make decisions, and package that logic into reusable blocks of code.