JavaScript if Statement
You are Here:
JavaScript if Statement
The if
statement executes a block of code if a specified condition is true.
Without Using Curly Brackets
Note: It is not necessary to use open and close curly brackets if you want to execute just one statement when the condition is true.
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Try to change "num = 50" to "num = 150"</p>
<p id="point"></p>
<p id="point1"></p>
<script>
var x = document.getElementById("point");
var y = document.getElementById("point1");
var num = 50;
if(num < 100)
x.innerText = "num is less than 100";
y.innerText = "I will always execute";
</script>
</body>
</html>
Syntax
if (condition)
// one line code
Using Curly Brackets
Note: It is necessary to use open and close curly brackets if you want to execute more than one statement when the condition is true.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Try to change "num = 50" to "num = 150"</p>
<p id="point"></p>
<p id="point1"></p>
<script>
var x = document.getElementById("point");
var y = document.getElementById("point1");
var num = 50;
if(num < 100){
var txt = "num is less than 100";
x.innerText = txt;
}
y.innerText = "I will always execute";
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.