https://github.com/codeadamca/javascript-if-statements
A basic example of using JavaScript if statements on a webpage.
https://github.com/codeadamca/javascript-if-statements
if-statements javascript learning-code
Last synced: 3 months ago
JSON representation
A basic example of using JavaScript if statements on a webpage.
- Host: GitHub
- URL: https://github.com/codeadamca/javascript-if-statements
- Owner: codeadamca
- Created: 2021-01-23T20:46:30.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-26T21:28:56.000Z (12 months ago)
- Last Synced: 2025-10-05T19:59:33.543Z (3 months ago)
- Topics: if-statements, javascript, learning-code
- Language: HTML
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A Basic Introduction to JavaScript and If Statements
JavaScript `if` statements allow code to make decisions. A typical `if` statement has the following structure:
```javascript
if(variable == "value")
{
// Code to execute when condition is true
}
```
An `if` statement causes the block of code between the open `{` and close `}` to only be executed if the condition between the open `(` and close `)` is true. When writing a condition, the following operators can be used:
OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
## Validation
JavaScript `if` statements are commonly used when validating a form. Create a new `HTML` document and add the following code:
```html
JavaScript If Statements
JavaScript If Statements
Enter some text:
Check
```
When the button is pressed, we will display a message indicating if the textbox has a value or is blank. We will achieve this by using an `if` statement followed by an `else` statement. The block of code associated to the `else` is executed if the condition of the `if` statement is false:
```javascript
if(variable == "value")
{
// Code to execute when condition is true
}
else
{
// Code to execute when condition is false
}
```
Add a `script` tag below the button in the new doucument. Add the following JavaScript to check the textbox and display a message:
```javascript
var button = document.getElementById("check");
button.addEventListener("click", function(){
var textbox = document.getElementById("text");
if(textbox.value == "")
{
alert("The textbox is empty!");
}
else
{
alert("The textbox has a value");
}
});
```
## Try It Out
Create a new HTML document, add the standard HTML tags, and a title:
```html
JavaScript Greeting
JavaScript Greeting
```
After the title add a `script` tag and the following JavaScript:
```javascript
var now = new Date();
var day = now.getDay();
var hour = now.getHours();
document.write("
The current day is " + day + ".
");
document.write("The current hour is " + hour + ".
");
```
This code creates a date object and uses the `getDay()` and `getHours()` to extract the day of the week (0 for Sunday and 6 for Saturday) and the hour of the day (in a 24 hours format).
After the previous JavaScript add an `if` statement to display a greeting based on the time of day and day of week:
ConditionMessage
MondayDoes someone have a case of the Mondays!
Friday afternoonTGIF!
WeekendHave a great weekend!
Other morningsGood morning!
Other afternoonsGood afternoon!
> Full tutorial URL:
> https://codeadam.ca/learning/javascript-if-statements.html
***
## Repo Resources
* [Visual Studio Code](https://code.visualstudio.com/)
* [W3Schools - JavaScript](https://www.w3schools.com/js/)