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)