Programming Fundamentals: Values, Variables, and Operators
By the end of this lesson, you'll be able to:
- Explain what a statement and a value are in plain English
- Recognize the three basic value types: strings, numbers, and booleans
- Declare a variable with let or const and read/change its value
- Predict the result of a comparison and of the ! (not) operator
- Read a simple if/else statement and a simple function
Prerequisites: Module 2: "Exercise: Create, Deploy, and Test an LWC in a Real Org"
What Is Code, and What Is a Statement?
Code is a precise, step-by-step set of instructions that a computer follows, in order, exactly as written — nothing is left to guesswork. A statement is one complete instruction. In JavaScript, a statement is usually one line, and usually ends with a semicolon (;):
let count = 0;
That single line is one statement: "create something called count, and give it the value 0." The rest of this lesson is really just learning to read (and then write) statements like this one, piece by piece.
Values and Their Types
A value is a single piece of data — one specific fact the program is working with. Every value in JavaScript has a type. This lesson covers the three simplest and most common ones:
'Acme Corp' // a string — text, always wrapped in quotes
42 // a number — no quotes
true // a boolean — only ever true or false
- A string is text — a name, an address, a status label. Always written inside quotes (
'single'or"double", either is fine, just be consistent). - A number is, unsurprisingly, a number — no quotes, and no commas (write
1000, not1,000). - A boolean has exactly two possible values:
trueorfalse. Nothing in between. Booleans answer yes/no questions: "is this welcome message showing?" "is this form valid?"
Variables: Storing a Value with a Name
A variable is a named container for a value — instead of a value floating around unlabeled, a variable gives it a name you can refer to later.
let greeting = 'Hello';
const maxAttempts = 3;
= here is assignment — "store the value on the right into the variable on the left." It is not a mathematical equals sign; read it as "gets the value" rather than "equals."
let declares a variable that can be reassigned later:
let score = 0;
score = 10; // allowed — score now holds 10 instead of 0
const declares a variable that cannot be reassigned after its first value:
const companyName = 'Acme Corp';
companyName = 'Globomantics'; // NOT allowed — this causes an error
A reasonable default: use const unless you already know the value will need to change later, in which case use let.
Comparisons and the ! (Not) Operator
A comparison checks a relationship between two values and produces a boolean result (true or false):
5 > 3 // true — 5 is greater than 3
5 === 3 // false — 5 is not equal to 3
'a' === 'a' // true — the two strings are identical
(=== checks "is this exactly equal to that" — it's the comparison you'll use almost everywhere in this course. > and < compare size, the way you'd expect from ordinary arithmetic.)
The ! operator means "the opposite." Applied to a boolean, it flips it:
!false // becomes true
!true // becomes false
Walk through that slowly: false means "not showing." !false asks "the opposite of not-showing" — which is true, "showing." Similarly, !true — the opposite of "showing" — is false.
This single idea is what makes a toggle possible:
let showWelcome = false;
showWelcome = !showWelcome; // showWelcome is now true
showWelcome = !showWelcome; // showWelcome is now false again
Each time this runs, showWelcome flips to the opposite of whatever it currently is — exactly the mechanism behind an on/off switch, or a "show more / show less" button.
if and else: Making Decisions
An if statement runs a block of code only when a condition is true:
if (score > 100) {
console.log('High score!');
}
Add an else to run different code specifically when the condition is false:
if (score > 100) {
console.log('High score!');
} else {
console.log('Keep trying.');
}
Read this as: "if the condition in parentheses is true, run the first { } block; otherwise, run the { } block after else." Only one of the two blocks ever runs for a given if/else pair.
Functions: Reusable Instructions
A function is a named, reusable block of instructions — write the steps once, then run them again as many times as needed, with different inputs, without retyping them.
function greet(name) {
return 'Hello, ' + name;
}
Breaking this apart:
function— the keyword that starts a function definition.greet— the function's name, chosen by the developer, used to call it later.(name)— a parameter: a placeholder for a value the function needs each time it runs. The function has no idea whatnamewill actually be until it's called.{ ... }— the function's body: the instructions that run.return— sends a value back out of the function to whatever called it. A function withoutreturnproduces no usable result.
Calling the function actually runs it, supplying a real value for each parameter:
greet('Ada'); // returns 'Hello, Ada'
greet('Sipho'); // returns 'Hello, Sipho'
Defining a function does not run it — nothing happens until it's called, just like writing a recipe doesn't cook the meal.
Connecting This to the Hello Salesforce Project
Module 1's Hello Salesforce project (Lesson 5) included this line, presented then as something to copy without full explanation:
this.showWelcome = !this.showWelcome;
Every piece of that is now something you understand: showWelcome is a variable holding a boolean (true or false) — whether the welcome message is currently showing. !this.showWelcome computes the opposite of its current value. = assigns that opposite value back into showWelcome. Put together: "flip showWelcome to whatever it currently isn't" — the exact toggle mechanism explained above, now fully demystified. (The this. part — what it refers to and why it's there — is explained in full in this module's "Functions, Arrow Functions, and Classes" lesson.)
Exercise
For each value, name its type (string, number, or boolean): "Acme Corp", 42, true, "42".
Show hint
Look carefully — one of these looks like a number but is actually text, because of the quotes.
Exercise
Challenge: predict the final value of isOpen after these three lines run, explaining each step as a comment.
Show hint
Walk through the ! operator one line at a time, the same way this lesson's toggle example did.
Programming Fundamentals: Values, Variables, and Operators Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson assumes zero programming background — every later lesson in this course builds directly on the handful of ideas taught here: values, variables, comparisons, decisions, and functions.