An open API service indexing awesome lists of open source software.

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.

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/)