Making Code a Bit Easier

Zachary Lindop
2 min readAug 27, 2021

--

We all love code, no matter how cumbersome and intimidating it often is, it lets us communicate our wants, needs and dreams to the technological world. It draws us, in a fun way, but making your way thru it is sometimes easier said than done. From the detailed DOM inquires of vanilla Javascript, to the spread out universe of components and props that make up React, it’s a jungle out there for sure. In the beginning of my coding journey, I preferred to do everything the basic way, it’s easy and is fundamental to building your skills. But as I learned more and more concepts, and my requirements kept getting tougher I quickly realized that I needed to pivot towards efficiency. Short cuts had to be carved out. One of my favorite tools that I discovered was the Ternary Operator. This tool is based off of a fundamental Javascript concept: The if, else statement runs like this:

IF ( condition) { expression if true} ELSE {expression if false}

Doesn’t look like much, but depending on the size of you code, that formula can get really bulky, and carry down many lines. I recently was doing an assignment that was manipulating data from an array using props in React, and almost every single attribute I had to render was called on easily by a prop. Except the last one, which I need to return based on conditional reasoning. I was a bit frustrated to get so far only to be stopped by a simple true/false question. After sighing and trying to figure it out for awhile, I eventually pondered how to work an if/else statement into this code…it seemed a bit difficult based on the code. But after taking a breather and simply stepping back for a second, I realized that the Ternary Operator could easily be worked into the curly brackets…ok, maybe not easily, but it worked much easier than Else/If, and it looked a lot nicer too! The Ternary Operator runs like this:

condition ? expressionIfTrue : expressionIfFalse

Simple form, and you do need to use your noodle to figure out the exact things to pass in there, but it super effective once mastered. Having picked up handy little tools like this makes my coding journey thru soft-ware engineer bootcamp a little less stressful. I highly recommend learning this tool for all your conditional rendering needs!

--

--