Accessing Javascript variable

213 views Asked by At

In the following code I am accessing the cookieCode in the var MasterTmsUdo but I am unable to access this variable. Can anyone tell me how can I access this variable inside var MasterTmsUdo?

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode, } }; 
2

There are 2 answers

6
Curtis On

It's a property of the CJ object, therefore you can access it via:

MasterTmsUdo.CJ.FIRECJ

Therefore you could have:

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode } };
console.log(MasterTmsUdo.CJ.FIRECJ);
0
Wesley On

Access the variable by using the following code:

var FireCJ = MasterTmsUdo['CJ']['FIRECJ'];

OR

var FireCJ = MasterTmsUdo.CJ.FIRECJ;

You can then alert or console.log the code and it will return the string "True". FireCJ can also be used in other code to make some IF statements for example
Small note: You can either use a boolean type (true or false) instead of a string type containing 'True'.

IF statement with boolean:

if(FireCJ) {
    // TRUE
} 

IF statement with your code:

if(FireCJ == 'True') {
    // TRUE
}