Azhar Ahmad
Azhar Ahmad

Reputation: 183

Accessing Javascript variable

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, } }; 

Upvotes: 1

Views: 206

Answers (2)

Curtis
Curtis

Reputation: 103368

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);

Upvotes: 3

Wesley
Wesley

Reputation: 21

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
}

Upvotes: 0

Related Questions