Best Website-BuildersBest Website-Builders
    What's Hot

    Tyson Fury v Oleksandr Usyk: Talks for undisputed heavyweight fight on verge of collapse

    March 22, 2023

    Central Banks Are Waging a ‘Class War’ in Inflation Fight: Economist

    March 22, 2023

    Six Nations 2023: Freddie Steward's red card against Ireland rescinded

    March 22, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Staff Sergeant Lixandy Estrella Cespedes > Gunsan Air Force Base > Gunsan Air Force Base

      March 22, 2023

      Social Security launches investigation into disappearance of zombie drug

      March 22, 2023

      The Markland area will be closed for about six months

      March 21, 2023

      The battle of ironclads marked the beginning of modern ships

      March 21, 2023

      CDJapan : Pale Moon 32.1.0

      March 21, 2023
    • Joomla

      Reseller Hosting Business: Important Q&A

      March 21, 2023

      Web Hosting: 8 Elements Every Entrepreneur Should Look For

      March 20, 2023

      VS Code Extension for In-Browser Development, WapuuGotchi Gamification Plugin & More – WP Tavern

      March 20, 2023

      How Superior Web Hosting Support Can Drive Business Success

      March 17, 2023

      PANDACU Studio Website Development Cooperation First Page Sage SEO Dsign Chicago adstargets Cardinal Digital Agency

      March 16, 2023
    • PHP

      Maren Morris introduces son to drag queen and unapologetically shares support for LGBTQ community at Tennessee benefits show

      March 21, 2023

      Gwyneth Paltrow ski crash lawsuit trial begins

      March 21, 2023

      “Anything exciting today?”

      March 21, 2023

      Possible New York Trump indictment attracts demonstrators

      March 21, 2023

      Here are the best cheap wireless earbuds under $25 on Amazon

      March 21, 2023
    • UX

      Custom driver apps: once a bonus, now a necessity

      March 22, 2023

      Adobe Explodes Its AI

      March 22, 2023

      ‘Laws are working as intended’: Pay transparency brings new awkwardness to the workplace

      March 21, 2023

      Microsoft says PlayStation should make an exclusive to compete with Call of Duty

      March 21, 2023

      How a holistic UX approach adds value to the design process

      March 21, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      Jedi Survivor tackles one of Star Wars’ biggest challenges

      March 22, 2023

      Quordle today – Tips and Answers for Wednesday, March 22nd (Game #422)

      March 22, 2023

      T-Mobile US Acquires Mint Mobile

      March 22, 2023

      Affordable Jabra Elite 4 can cause top wireless earbuds to wiggle in boots

      March 22, 2023

      CPU aside, today is the day the GPU takes over your kingdom

      March 22, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » Different ways to write conditional statements in JavaScript
    JavaScript

    Different ways to write conditional statements in JavaScript

    websitebuildersnowBy websitebuildersnowAugust 12, 2022No Comments4 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Conditional statements let you execute blocks of code based on certain conditions.


    The JavaScript language offers various ways of using conditional statements. Many of them are common to other programming languages ​​as well. However, you should know the benefits of each and how they work in JavaScript.


    1. if-else and else-if statements

    Ann if-else The statements execute one block if the condition is true and the other block if the condition is false. else-if executes a block that matches one of several conditions, or executes a default block if none of the conditions match.

    A true value is the value that JavaScript considers truth when encountered in a boolean context.False values ​​are values ​​that JavaScript considers error when encountered in a boolean context.

    JavaScript considers all values ​​true unless a few values ​​are false.false value is error, 0, -0, 0n, “”, null, undefinedand NaN.

    has the following syntax: if-else statement:

     if (condition) {
        
    } else {
        
    }

    In some cases, it may be necessary to check multiple related conditions. In these scenarios, else-if Evaluate extra conditions.

    for example:

     if (condition) {
        
        
    } else if (condition_2) {
        
        
    } else if (condition_n) {
        
        
    } else {
        
    }

    use else-if Statements allow you to evaluate any number of conditions. However, this method quickly becomes ugly and difficult to maintain as the number of conditions increases.

    JavaScript is switch statement.

    2. switch statement

    of switch The statement evaluates the expression once and attempts to match it with one or more possible values. Each possible match is case keyword.

    If the switch statement finds a match, it executes all subsequent statements. break down statement.

    The syntax for the switch statement is:

     switch (expression) {
        case 'first-case':
            
            break;
     
        case 'case_2':
            
            break;
     
        default:
            
    }

    of break down The statement is switch to specify where to stop executing code. If you miss a break statement, code execution continues, executing all other code blocks after the first match. This is a rare occurrence.

    3. Ternary operator

    In JavaScript, you can also use the ternary operator to shorten conditional statements.

    The ternary operator takes three operands:

    1. A question mark (?).
    2. After the question mark, a colon (:). This will be executed if the condition is true.
    3. An expression after a colon that is executed if the condition is false.

    for example:

     condition ? console.log('Condition is truthy') : console.log('Condition is falsy');

    The above statement effectively means “if ‘condition’ is true, then log the first message, else log the second message”.

    4. Short circuit

    Short-circuiting is a technique that involves using logical operators again (||) and and (&&) evaluates expressions from left to right.

    Operations involving the OR operator short-circuit by returning the first true value encountered. If all values ​​of the expression are false, short-circuit and return the last false value.

    Operations with the AND operator short-circuit by returning the first bogus statement found. Short-circuits and returns the last true value if all statements in the expression are true.

    An example of a conditional statement using the OR operator is shown below.

     app.listen(process.env.PORT || 3000)

    This shorthand approach to creating conditional statements is common in Express applications. It is written as: port Environment variables exist. Please use it. Otherwise, use port 3000.

    An example of a conditional statement using the AND operator is shown below.

     foo && console.log('foo is defined')

    The code block above is an “if Phew Calls the console.log() function, if defined.

    This technique is the shortest way to write a conditional statement, but it can make your code less readable. Avoid overuse, especially if you are working as part of a large team.

    Importance of conditionals

    Conditional statements are what allow the program to make decisions. Without them the code runs straight from start to finish. They are also part of the loop. Without them the loop would run infinitely and crash the application.



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleDatawisp Raises $3.6M in CoinFund-Led Round to Build No-Code Data Platform
    Next Article How to host a website for free from your PC or laptop
    websitebuildersnow
    • Website

    Related Posts

    Move away from using (most) WebR R functions in WebR-powered apps/sites and call a single JavaScript function

    March 21, 2023

    “Smaller, Simpler, Faster” — Visual Studio Magazine

    March 21, 2023

    Burp Suite Enterprise Edition Power Tools: Unleash the Power of Command Line, Python and More | Blog

    March 21, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Tyson Fury v Oleksandr Usyk: Talks for undisputed heavyweight fight on verge of collapse

    March 22, 2023

    Central Banks Are Waging a ‘Class War’ in Inflation Fight: Economist

    March 22, 2023

    Six Nations 2023: Freddie Steward's red card against Ireland rescinded

    March 22, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.