1

This must be a a very simple question, but may be I'm doing it wrong.

I wanted to set default aura:attribute value to null on lightning component as below.

e.g.

<aura:attribute name="selectedObj" type="Object" default="null"/>

but this gives me selectedValue as a String, and not comparing with null in lightning javascript controller/helper code.

let selectedObj = component.get('v.selectedObj');

console.log('selectedObj: '+(selectedObj===null) +' string comparison: '+(selectedObj==='null'));
component.set('v.selectedObj', null);
console.log('selectedObj: '+(component.get('v.selectedObj')==null)+' string comparison: '+(component.get('v.selectedObj')=='null'));

Result:

selectedObj: false string comparison: true 
selectedObj: true string comparison: false

Therefore I had to set null value explicitly in javascript controller at the initialization. e.g.

component.set('v.selectedObj', null);

Is this the correct approach to set null in javascript? are there any better alternatives to set default null value for aura:attribute?

Cheers!

1

Don't define a default attribute. This causes the attribute to default to null.

Eg

<aura:attribute name="selectedObj" type="Object" />
| improve this answer | |
  • 1
    too easy, thanks! – Pasan Eeriyagama Oct 15 '19 at 2:26
  • Yeah, I was wondering if it was bad to have such a short answer... But I couldn't think of anything else useful to add! – Caspar Harmer Oct 15 '19 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.