DONATE  
MENU
JSON
AJAX
PHP
JAVASCRIPT
MYSQL
HTML
CSS

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Conditional Testing » If Statement
Share Tweet WhatsApp

If Statement

The if statement is a conditional tester used in ensuring the execution of a code block only when a condition or expression is true. It is similar to our real life usage of the word "if". For example, "if you are done, turn off the system". The statement is true when the individual in question is "done". Same way do you use the "if" statement in executing a statement(s) only when a condition is true. For example, if "8 is equal to 5", then do this or that.
var num = 5;
if (num == 10){
alert("Number accepted.");
}
In the above, we used the "if" conditional tester to test if the value of the variable (num) is equal to 10. If it is true (i.e the variable value is equal to 10), then, the statement contained in the code block ({}) will be executed.
<script>
var password = prompt("Enter a password:");
if (password == 123){
alert("LOGIN SUCCESSFUL");
}
</script>

Copy PRACTICE IT

If Statements With Logical Operators Conditions being tested in an "if" statement can be put together using logical operators and together, form a larger expression to be tested. You can learn more on logical operators here.
 if (4 > 2 && 7 < 12){ /*This is true if 
4 is greater than 2 and 7 is less then 12
*/
}
EXAMPLE
<script>
var username = "SB";
var password = "myPass";
if (username == "SB" && password == "myPass"){
alert("Access Granted.");
}
</script>

Copy PRACTICE IT

= And == Most beginners do make mistakes of using the assignment operator(=) in place of the comparison operator(==). For example, a beginner may want to test if the value of a variable is same as that of a value using the assignment operator(=) because that is what we have been familiar with in our every day live mathematics.
var x = 5;
if (x = 2){
alert(1);
}
Now, the condition above will always be true because it is same as saying that "if 2 has been assigned to variable 'x', alert 1". The assignment will always be done. Therefore, take note of the difference between the assignment operator(=) and the comparison operator(==). The assignment operator(=) is used in assigning a value to a variable while the comparison operator(==) is used in comparing two values in order to determine if they possess same values. SUMMARY
  1. The if statement is used in testing a statement or expression.
  2. if the state of the expression returns true, then a code block is executed.
  3. The expression to be tested in an if statement can be made sophisticated with the use of various logical operators.
Take a short quiz on If Statement.
Ask a question or tell us if If Statement is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode