Interview Questions – Lightning Framework – Part 5 – Attributes & Expressions

Interview Questions – Lightning Framework – Part 5 – Attributes & Expressions
Checkout earlier posts in interview question series to read previous questions.

36. What are attributes?

Attributes are same as variables in a Class. They are typed fields that are set on a specific instance of a component, and can be referenced from within the component’s markup using an expression syntax. Attributes enable you to make components more dynamic.

You can define attributes using <aura:attribute> tag, every attribute has a name and a type.

<aura:attribute name="whom" type="String" default="world"/>

Attributes may be marked as required by specifying required=”true”, and may also specify a default value.

37. What attributes does <aura:attribute> tag support?

<aura:attribute> tag supports below attributes:

  • access – Define access of attribute. Can be public, private and global.
  • name – Define name of attribute.
  • type – Type of attribute
  • default – Default value of attribute
  • required  – Makes your attribute required.
  • description – Description of the attribute

38. What are all supported attribute types in aura component?

Aura components supports a number of data types for attributes.

  • Boolean
  • String
  • Integer
  • Decimal
  • Long
  • Double
  • Date
  • Datetime
  • Set
  • List
  • Map
  • Object
  • Function
  • Standard & Custom Object Type
  • Apex Class Type

39. What are expressions?

Expression allows accessing attribute value in component markup. Use expressions for dynamic output or passing values into components by assigning them to attributes.

An expression is any set of literal values, variables, sub-expressions, or operators that can be resolved to a single value. Method calls are not allowed in expressions.

Expression syntax:

  • { !expression } – Called as “bound expression”. Fetch new value of attribute automatically as soon as attribute value is modified by component.
  • { #expression } – Called as “unbound expression” which means it will not refresh its value automatically when the attribute value is modified by the component.

40. What are different types of expressions?

Expressions are of two types:

Bound Expression – This expression creates 2 way data binding. This expression ensures you get the latest value of the attribute all the time. This is a costly solution as the frame has to create 2 listeners to read and write the value all the time.

{!expression} is the way to create bound expression.

Unbound Expression This expression does not create any data binding. This is much cheaper solution if you want to set the data of the attributes only once. There are no listener attached to this variable hence it will never detect the changes happened.

{#expression} is the way to create unbound expression.

41. When we should use unbound expression?

Unbound expressions should only be used when you have an attribute value in advance since it only supports one time data binding.

  • Generally it is being used within loops/iteration when the value is already handy in advance.
  • Child component being called from parent component.

Using of unbound expression should be avoided in case of null attribute values.

Also watch: Advance Lightning Series – Episode 3 – Data Binding

42. What is ternary operator and how it is used?

Ternary operator is single line if-else statement.

<condition> ? True statement execution : false statement execution.

This expression uses the ternary operator to conditionally output one of two values dependent on a condition. Generally being used to conditionally render component.

43. What are value providers in Lightning Framework?

Value providers are a way to access data. Value providers encapsulate related values together, similar to how an object encapsulates properties and methods.

The value providers for a component are v (view) and c (controller).

  • v- Used to get attribute value of component markup. Like, {!v.attributeName} or {#v.attributeName}
  • c- Used to  access controller method from component markup. Like, {!c.controllerMethodName}

Read Also:

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has One Comment

Leave a Reply

Close Menu