Controlling the Execution Flow With Conditional Statements in JavaScript
JavaScript·5 min read·Jan 1, 2025
In programming, control statements are code structures used to manage the flow and logic of a script by conditionally executing (or not) a set of instructions based on predicates called conditions.
Comparison operators
A condition is an expression that when evaluated produces a boolean result, either true or false.
These conditions are created using comparison operators that allow to logically compare values between them.
Checking the equality of values
In JavaScript, the equality or inequality of two values can be verified using the loose equality, strict equality, inequality, and strict inequality operators.
Loose equality
When using the loose equality operator ==, JavaScript performs a type coercion, which is an implicit type conversion of values from one data type to another (e.g., string to number), and checks whether its operands values are equal.
By doing this, JavaScript attempts to make sense of the comparison by converting one or both values to a common type before executing it.
In this context, JavaScript may treat different data types as equal if their values are equivalent after conversion.
For example, all of the following comparisons will output true:
console.log(18 == '18');console.log(false == 0);console.log(true == 1);console.log(null == undefined);console.log(0 == '');Strict equality
When using the strict equality operator ===, JavaScript doesn't perform type coercion and checks whether its operands types and values are equal.
Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle