Data types in Salesforce Lightning and JavaScript

Datatypes in Lightning and Javascript

JavaScript data types are “dynamic typed” data types meaning are there are data types but a variables does not bound of them.

There are majorly three data types

  • Number
  • String
  • Boolean

The number data type represents both integer (ex. 123,84 , 950 etc ) and floating point numbers(12.3 , 0.84,9.5).

There are mathematical operations with number are allowed in JavaScript for numbers but there are few points to be considered while using number
1. division by zero is returns infinity
2. multiplication/division between number and number as string will not return the expected result it will return us “NaN

The string data type in JavaScript must be enclosed under quotes. single quote and double quote are simple quote.
1. Backtick(`) quote are important for getting expression under `${1+3}` will return 4
2. Simple quotes “” and ” will return result in string.

for example : 
"This is addition result = 1 + 2" -> "This is addition  result = 1 + 2" 
"This is addition  result = ${1 + 2}" -> "This is addition  result = 1 + 2"  
 'This is addition  result = ${1 + 2}' -> "This is addition  result = 1 + 2"    
` This is addition  result = ${1 + 2} `  -> "This is addition  result = 3" 
let name = 'James Bond';
 ` My Name is ${ name } `  -> "  My Name is James Bond "  

The boolean data type in JavaScript has only values true and false.
let nameFieldChecked = true; // true
let ageFieldChecked = false; // false
let isGreater = 4 > 1; // true

The null datatype does not belong to any of the data type described above.
It forms a separate type of its own which contains only the null value:

The undefined also does not belong to any of the above data type.
If an variable does not assign any value then it is considered as undefined.
If a variable is declared, but not assigned, then its value is undefined.
let x; alert(x); // this will result as undefined

The Object and Symbols
The object type is special.
All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities. We’ll deal with them later in the chapter Objects after we learn more about primitives.
The symbol type is used to create unique identifiers for objects. We mention it here for completeness, but we’ll study it after objects.

The typeof operator
The typeof operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.

  1. As an operator: typeof x.
  2. As a function: typeof(x).

Leave a comment