
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is if statement in JavaScript?
The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.
Syntax
The syntax for a basic if statement is as follows −
if(expression){ Statement(s)to be executed if expression is true }
Here a JavaScript expression is evaluated. If the resulting value is true, the given statement(s) are executed. If the expression is false, then no statement would be not executed. Most of the times, you will use comparison operators while making decisions.
Example
You can try to run the following to learn how to work with if statement in JavaScript −
<html> <body> <script> var age = 25; if( age > 18 ){ document.write("Eligible to vote!"); } </script> <p>Set the variable to different value and then try...</p> </body> </html>
- Related Articles
- What is if...else if... statement in JavaScript?
- What is the if...else statement in JavaScript?
- What is break statement in JavaScript?
- What is continue statement in JavaScript?
- What is Switch...case statement in JavaScript?
- What is while loop statement in JavaScript?
- What is for loop statement in JavaScript?
- What is a block statement in JavaScript?
- What is for...in loop statement in JavaScript?
- What is do...while loop statement in JavaScript?
- What is for each...in a statement in JavaScript?
- What is the role of throw statement in JavaScript?
- What is correct syntax of Python if statement?
- How to write an inline IF statement in JavaScript?
- What is basic syntax of Python if...else statement?

Advertisements