If else statement Javascript

Hello, first time poster here. I am struggling with javascript as I am a new user. I did a search on google and then here once I found this forum, but could not find a good answer to my question. I am trying to write a If else statement for a report. The data is stored as TRUE or FALSE in the table. I am wanting to have an X print on the report if the data is TRUE and a null on the report if the data is FALSE. Seems like a simple thing, but everything I try, the report either brings back a null, X or the section with a code does not return any data for anything. Below is the current code I have tried, but this particular code returns a null in each column that the test is run.

if (dataSetRow[“column”] === true) {
dataSetRow[“column”] = “X”
} else {
dataSetRow[“column”] = " " }

I would appreciate any help as I am pretty frustrated in trying so many variations of this code to make it work. I hope I have provided enough information. Thank you so much for any help.

It’s hard to know what might be going on without seeing the surrounding code.

Sorry, maybe I did not explain this properly. The query that the report uses brings back the columns needed for the report. I just pull the columns onto the report from the query. Basically kind of like the way Crystal Reports or Business Objects work. However, I can create an expression on the column using javascript. In other words, if I just pulled the column onto the report, it would display true when executed. I just need to write an expression in javascript that would display an X instead of true or display a null instead of false, hence an If else statement is what is needed.

I know the best solution would be to write this code in the query. However, because the query is created in another application, it does not allow me to change the query code.

Well, null isn’t the same thing as " ".

The reason I said that we can’t help much without the surrounding code is that an if/else is pretty simple, but relies on having a good understanding of the data that you’re looking at.

The problem is that you are checking if the value assigned to dataSetRow[“column”] is strictly equal to true. What you actually need is to check if dataSetRow[“column”] is truthy. See the code snippet below.

// First ensure the dataSetRow object exists, then check if the 'column' property has a truthy value.
if (!!dataSetRow && dataSetRow[“column”]) {
  dataSetRow[“column”] = “X”;
} else {
  // You should set the value to null or "" (no space).
  dataSetRow = !!dataSetRow ? dataSetRow || {};
  dataSetRow[“column”] = null;
}

You can also use type coercion to check if the value is true. This is another option, but if I were reviewing your code on the job I would prefer option 1 above.

// note == instead of ===

if (dataSetRow[“column”] == true)

See the MDN docs for more information on thruthy values.

If you’re confused as to why === is the incorrect operator in this case, check out the equality comparison docs on MDN.

If you’re wondering how the !! works in the code below, check out this Medium article.

Thank you so much j10wy. With a little more tweaking, I finally got it to work the way I need.

I appreciate your help so much.