DEV Community

Discussion on: Writing If Statements with the Ternary Operator

Collapse
 
sebnitu profile image
Sebastian Nitu • Edited

Great post! I've recently started using ternary operators as well. One "gotcha" I've found when writing them is to not use them in a way that make the line-length too long. Typically I try and keep them under 80 characters, so you can also write your example like this if that was a goal:

let userIdValid = (userId.value.length === 10) ? 
  "the user ID is valid" : 
  "the user ID is not valid";

Thanks for the post!

Collapse
 
valentinaperic profile image
Valentina Peric

Thank you Sebastian - I am glad you enjoyed it! Your refactor is a great reminder that you can use multiple lines for ternaries! Thank you for the addition 😊