Web Development /JavaScript Programming / Lesson
Web Development

JavaScript Operators, Expressions and Decision Making

Learn how JavaScript performs calculations, compares values, combines conditions and makes decisions using arithmetic, assignment, comparison, logical and conditional operators.

🎯

Learning Objectives

["Understand what JavaScript operators are and how they are used.","Use arithmetic operators to perform calculations.","Use assignment operators to update variable values.","Understand equality and comparison operators.","Use strict equality and strict inequality correctly.","Use logical AND, OR and NOT operators.","Understand operator precedence and grouping.","Use the ternary operator for simple conditional expressions.","Understand truthy and falsy values in decision-making.","Combine operators to build practical JavaScript conditions."]

📖

Lesson Content

Introduction

JavaScript programs frequently need to calculate values, compare information and make decisions. A student portal, for example, may need to determine whether a student has completed enough lessons before granting access to a certificate.

JavaScript uses operators to perform these operations. Operators allow developers to calculate numbers, assign values, compare data and combine multiple conditions.

In this lesson, you will learn the major JavaScript operators and how they can be combined to create useful program logic.

What Is an Operator?

An operator is a symbol or keyword that tells JavaScript to perform an operation on one or more values.

10 + 5

In this example, + is the addition operator.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorNameExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5
/Division10 / 5
%Remainder10 % 3
**Exponentiation2 ** 3

Addition

const firstNumber = 10;

const secondNumber = 20;

const total = firstNumber + secondNumber;

console.log(total);

The result is 30.

const firstName = "John";

const lastName = "Doe";

const fullName = firstName + " " + lastName;

console.log(fullName);

Subtraction

const price = 5000;

const discount = 1000;

const finalPrice = price - discount;

console.log(finalPrice);

Multiplication

const price = 2500;

const quantity = 4;

const total = price * quantity;

console.log(total);

Division

const totalMarks = 400;

const subjects = 4;

const average = totalMarks / subjects;

console.log(average);

Remainder Operator

console.log(10 % 3);
const number = 10;

console.log(number % 2 === 0);

Exponentiation

const result = 2 ** 3;

console.log(result);

Assignment Operators

let score = 50;

score += 10;

console.log(score);
let score = 100;

score -= 20;

score *= 2;

score /= 4;

Increment and Decrement

let lessons = 5;

lessons++;

console.log(lessons);
let lessons = 5;

lessons--;

console.log(lessons);

Comparison Operators

OperatorMeaning
===Strictly equal
!==Strictly not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Strict Equality

console.log(10 === 10);

console.log(10 === "10");

Strict Inequality

console.log(10 !== 5);

console.log(10 !== "10");

Greater Than and Less Than

const age = 20;

console.log(age > 18);

console.log(age < 18);

Greater Than or Equal To

const score = 70;

console.log(score >= 70);

Logical Operators

  • && — AND
  • || — OR
  • ! — NOT

Logical AND

const age = 20;

const hasAccount = true;

console.log(
	age >= 18 && hasAccount
);

Logical OR

const isAdmin = false;

const isInstructor = true;

console.log(
	isAdmin || isInstructor
);

Logical NOT

const isLoggedIn = false;

console.log(!isLoggedIn);

Combining Conditions

const age = 21;

const hasAccount = true;

const isVerified = true;

const canAccessCourse =
	age >= 18 &&
	hasAccount &&
	isVerified;

console.log(canAccessCourse);

Operator Precedence

const result = 10 + 5 * 2;

console.log(result);
const result = (10 + 5) * 2;

console.log(result);

Truthy and Falsy Values

  • false
  • 0
  • ""
  • null
  • undefined
  • NaN
const username = "David";

console.log(
	Boolean(username)
);

The Ternary Operator

condition ? valueIfTrue : valueIfFalse;
const age = 20;

const status =
	age >= 18
		? "Adult"
		: "Minor";

console.log(status);

Practical Student Example

const completedLessons = 16;

const minimumLessons = 16;

const passedAssessment = true;

const eligible =
	completedLessons >= minimumLessons &&
	passedAssessment;

console.log(eligible);

Another Practical Example

const username = "student01";

const passwordEntered = true;

const accountActive = true;

const canLogin =
	username !== "" &&
	passwordEntered &&
	accountActive;

console.log(canLogin);

Expressions vs Statements

10 + 5
let total = 10 + 5;

Short-Circuit Evaluation

const isLoggedIn = false;

isLoggedIn && console.log(
	"Welcome back!"
);

Building a Simple Eligibility Checker

const age = 20;

const completedLessons = 18;

const requiredLessons = 16;

const hasAccount = true;


const eligible =
	age >= 18 &&
	completedLessons >= requiredLessons &&
	hasAccount;


console.log(
	"Eligible:",
	eligible
);

Practice Exercise

Create a JavaScript program that stores a student's score, required passing score and assignment submission status. Determine whether the student meets both requirements.

Mini Challenge

console.log(10 + 5 * 2);

console.log(10 === "10");

console.log(20 > 10 && 5 < 3);

console.log(true || false);

console.log(!true);

console.log(8 % 2 === 0);

Best Practices

  • Use === and !== when you need strict comparisons.
  • Use parentheses when they make a complex expression easier to understand.
  • Break complicated Boolean expressions into clearly named variables when appropriate.
  • Choose meaningful variable names.
  • Do not rely on confusing automatic type conversions.
  • Keep conditions simple and readable.

Conclusion

Operators are fundamental building blocks of JavaScript programming. They allow developers to calculate values, assign data, compare information and combine conditions.

In this lesson, you learned arithmetic operators, assignment operators, comparison operators, logical operators, truthy and falsy values, operator precedence and the ternary operator.

These concepts prepare you for the next major programming skill: using conditional statements to make JavaScript programs respond differently depending on the situation.

💻

Code Example

const studentName = "Dwallnet Student";

const score = 78;

const passingScore = 50;

const completedLessons = 12;

const requiredLessons = 10;

const assignmentSubmitted = true;


/*
|--------------------------------------------------------------------------
| Calculate Results
|--------------------------------------------------------------------------
*/

const passedScore =
	score >= passingScore;

const completedRequiredLessons =
	completedLessons >= requiredLessons;


/*
|--------------------------------------------------------------------------
| Combine Conditions
|--------------------------------------------------------------------------
*/

const eligible =
	passedScore &&
	completedRequiredLessons &&
	assignmentSubmitted;


/*
|--------------------------------------------------------------------------
| Output
|--------------------------------------------------------------------------
*/

console.log(
	"Student:",
	studentName
);

console.log(
	"Passed Score:",
	passedScore
);

console.log(
	"Required Lessons Completed:",
	completedRequiredLessons
);

console.log(
	"Assignment Submitted:",
	assignmentSubmitted
);

console.log(
	"Eligible:",
	eligible
);


/*
|--------------------------------------------------------------------------
| Ternary Example
|--------------------------------------------------------------------------
*/

const status =
	eligible
		? "Eligible for completion"
		: "Requirements not completed";

console.log(status);
⭐

Key Takeaways

["Operators allow JavaScript to perform calculations and work with values.","Arithmetic operators include addition, subtraction, multiplication, division, remainder and exponentiation.","Assignment operators are used to assign and update variable values.","Comparison operators return Boolean results.","Strict equality uses === and checks both value and data type.","Logical AND requires the relevant conditions to evaluate as true.","Logical OR evaluates to true when at least one relevant condition is true.","The NOT operator reverses a Boolean value.","Parentheses can be used to control and clarify operator precedence.","Truthy and falsy values affect how JavaScript evaluates conditions.","The ternary operator provides a concise way to handle simple two-way decisions.","Clear and readable expressions make JavaScript programs easier to maintain."]

Enjoying this lesson?

Sign in to your Dwallnet Academy student account to track your learning progress and mark lessons as complete.

Sign In to Track Progress