How To Use If In Javascript

Tanggal : 14 Feb 2023, Category : JavaScript, Create By : admin

In JavaScript, you can use the "if" statement to execute code based on a condition. The basic syntax is:

if (condition) {
// code to execute if the condition is true
}
 

Here's an example:

let age = 25;

if (age >= 18) {
console.log("You are an adult.");
}
 

In this example, the code inside the curly braces will only be executed if the condition inside the parentheses (age is greater than or equal to 18) is true. If the condition is false, the code inside the curly braces will be skipped.

You can also use "else" to specify code to execute if the condition is false, like this:

let age = 15;

if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult.");
}
 

In this example, since age is less than 18, the code inside the "else" block will be executed, and "You are not an adult." will be logged to the console.

 


Keyword:
  • How To Use If In Javascript

Artikel Lain

Mengenal Substr Php

Date : 27 Dec 2018, Category : Tutorial, Create By : admin

Fungsi substr() adalah fungsi PHP untuk mengambil atau memotong text atau string, substr mengambil sebagian karakter dari sebuah string. Fungsi substr ini cukup sering digunakan dalam pembuatan aplikasi berbasis php. Sebagai contoh sederhananya, .....

What Is Css Language And Examples Of Its Use

Date : 13 Feb 2023, Category : CSS, Create By : admin

CSS (Cascading Style Sheets) is a style sheet language used for describing the look and formatting of a document written in HTML or XML. It is used to control the .....

Css Colors And How To Use Them

Date : 15 Feb 2023, Category : CSS, Create By : admin

CSS colors are used to add color and style to HTML elements on a web page. Here's how to use them: You can specify a color using a named color, such .....