Home > Web Front-end > JS Tutorial > Concepts of true, false, truthy and falsy in JavaScript

Concepts of true, false, truthy and falsy in JavaScript

Mary-Kate Olsen
Release: 2024-12-13 19:58:17
Original
840 people have browsed it

JavaScript-এ true, false, Truthy এবং Falsy এর ধারণা

true and false are boolean data types in JavaScript, used to perform various logical operations and condition checks. Below is a detailed discussion of the usage of true and false, and the truthy and falsy values ​​in JavaScript.

1. True and False are used when:

True:

The value true indicates that the result of a condition or logical test is "true".

False:

The value false indicates that any condition or logical test results in "false".

Example:

let isAdult = true; // একজন প্রাপ্তবয়স্ক হলে 'true' হবে
let isStudent = false; // শিক্ষার্থী না হলে 'false' হবে

if (isAdult) {
    console.log("You are an adult.");
} else {
    console.log("You are not an adult.");
}
Copy after login
Copy after login

2. Truthy and Falsy value keys:

Some values ​​in JavaScript behave as true and false, even though they are not directly true or false. It has two categories:

Falsy values:

Falsy values ​​are values ​​that evaluate to "false" in a logical test. The following values ​​are considered falsy in JavaScript:

  • false
  • 0 (zero)
  • -0 (negative zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (Not a Number)

Example:

if (0) {
    console.log("This will not run because 0 is falsy.");
} else {
    console.log("Falsy value."); // এই লাইনটি রান করবে
}
Copy after login
Copy after login

Truthy values:

Truthy values ​​are values ​​that evaluate to "true" in a logical test. Below are examples of some common truthy values:

  • true
  • Any non-zero number (eg: 1, -1, 100)
  • Any non-empty string (eg: "hello", "false", "0")
  • object (eg: {}, [])
  • function

Example:

let isAdult = true; // একজন প্রাপ্তবয়স্ক হলে 'true' হবে
let isStudent = false; // শিক্ষার্থী না হলে 'false' হবে

if (isAdult) {
    console.log("You are an adult.");
} else {
    console.log("You are not an adult.");
}
Copy after login
Copy after login

3. Truthy and Falsy checking rule:

When you use a variable in an if or logical operation, JavaScript automatically evaluates it as truthy or falsy.

Truthy example:

if (0) {
    console.log("This will not run because 0 is falsy.");
} else {
    console.log("Falsy value."); // এই লাইনটি রান করবে
}
Copy after login
Copy after login

Falsy example:

if ("hello") {
    console.log("This will run because 'hello' is truthy."); // এই লাইনটি রান করবে
} else {
    console.log("Falsy value.");
}
Copy after login

4. for short:

  • Truthy: Any value that evaluates to true when the condition is checked (eg: non-zero number, non-empty string, object).
  • Falsy: Any value that is considered false in the condition check (eg: false, 0, "", null, undefined, NaN).

True and false conditions in JavaScript are important for making logical decisions. Understanding Truthy and Falsy will give you more skill in creating terms.

The above is the detailed content of Concepts of true, false, truthy and falsy in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template