DEV Community

arfa
arfa

Posted on

Javascript conditional operator

Let's implement a simple function that takes two parameters " width" and " height" of a picture, and suppose to return true if our picture is a landscape and return false if it isn't.

we can handle it with this simple code

if(width > height) return true
return false
Enter fullscreen mode Exit fullscreen mode

but we can write it in a better way if we use a conditional operator

return width > height ? true : false
Enter fullscreen mode Exit fullscreen mode

we can improve it even further more like this

return width > height
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay