code snippets

If-Else Alternatives

Why use that old-school, verbose code when these shortcuts work just as well? All the cool kids are using code like this....


1const wabbitSeason=true;
2
3return (
4    <>
5        //logical AND operator
6        { wabbitSeason && <p>Say your pwayers wabbit....</p>}
7        
8        //ternary operator
9        { wabbitSeason ? "Run wabbit!" : "It's ok, you're safe...." }
10    </>
11)

Summary: The logical AND operator && is a nice shortcut for "if this is true then do that".

The ternary operator gives you the full "if this is true do that, else do this thing"