Simplifying JavaScript Using Conditionals

in , August 28th, 2024

This article will describe a few simple ways to condense and simplify your JavaScript code.

Did you know that SuiteScript (NetSuite programing language) is built off of JavaScript?

Simple “AND”, “OR” check

  • You can use “&&” to conditionally run a line of code:
let x = 5;
let y = 0;
y > 0 && x = x + 1; 

In this example, the x = x + 1 part will not execute since y > 0 is false. You can use this method to remove “if” statements and shrink your code base.

  • You can use “||” to conditionally run a line of code. This is similar to the above but is used much less often.

“In-line if/else” statement

Use this to write condensed and convenient if/else statements

let x = 5;
let y = 0;
z = (x + y > 3) ? 6 : 7;

The syntax is as follows: “conditional” ? code to execute if conditional is true : code to execute if conditional is false

This statement is a really handy tool to cut down on your code base and can be easy to read once you get used to it.

Author: Sam Gagliardi


Got stuck on a step in this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!

 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart