Javascript Extension best practices - for dot or brackets notation?

uniqueuser
New Member
uniqueuser

Hi there,
I am wondering if there is a difference on using either one of them to reference a UDO variable in javascript extensions:

Tealiums documentation suggests to use the brackets notation like b['var_name'] fo example here:

https://docs.tealium.com/platforms/javascript/the-b-object/

While in the javascript extension itself, the editor suggests to use the dot notation:

hint.png

Personally I currently stick to the brackets notation as from the documentation but i would prefere to use the dot notation for better readability in the future.

Cheers.

2 REPLIES 2

Javascript Extension best practices - for dot or brackets notation?

dsavvy
Frequent Visitor
dsavvy Frequent Visitor
I try to us dot notation where i can because i find it easier to read, and can't be overwritten with another string variable, where in brackets the variable name can inherit other values if they haven't been organised properly.

Have a read of this article, it explains it better than i can

https://codeburst.io/javascript-quickie-dot-notation-vs-bracket-notation-333641c0f781

I think it is just down to the situation and personal preference.
Damian Savvides

Javascript Extension best practices - for dot or brackets notation?

Tealium Employee

Hi,

in TiQ, I'll use dot notation when I can (so I'd write your example  b.var_name).  But many automatically generated names in TiQ include a dot, which forces you to use bracket notation:  b["cp.cookie_name"].

That's more a technical consideration than a "best practice" I guess. 

But I do agree that dot notation is easier to read, and doesn't risk dumb errors like omitting your quotes and accidentally referencing a variable instead of using a string literal.

Hope that's useful?

Best
Caleb