Skip to content

Javascript for Salesforce Developers

Hello trailblazers,

I found that many of the salesforce developers come across difficulties and confusions while writing javascript code. So, I have tried to cover up some best practices and clear some basic things to avoid mistakes and enhance code performance.

Note – JavaScript is case sensitive.

Declaring Variables

Variable declaration is done using one of three operators: var, let, and const. In general, use let and const, const is immutable. let is recommended over var because-

  • var – has function-level scoping 
  • let – has block-level scoping 

null, undefined and NaN

null – The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values.

typeof null          // "object" (not "null" for legacy reasons)

undefined  – It is a variable in the global scope. The initial value of undefined is the primitive value undefined. A variable that has not been assigned a value is of type undefined.

typeof undefined     // "undefined"

NaN – the result of failed mathematical operations

=== and == operators

=== is only true if the operands are of the same type and the contents match.

== converts the operands to the same type before making the comparison.

Objects 

It is used to store various keyed collections and more complex entities.

let object = {}

Initializing objects using variables

let firstName = 'John', lastName = 'Doe'; 
let user = { firstName : firstName, lastName : lastName }

With ES6, the above code can be reduced as long as the variables and object property names are the same.

let firstName = 'John', lastName = 'Doe';
let user = { firstName, lastName };

Commonly used properties and methods of objects:

  • Properties – Object.length
  • Methods – Object.assign(), Object.create(), Object.keys(), Object.values()

Cloning an object:

 1. Using Spread – It makes a shallow copy. A shallow copy will duplicate the top-level properties, but the nested object is shared between the original(source) and the copy(target).

const obj = {1 : 'red' , 2 : 'black'};
const copy = {...obj};

2.Using Object.assign – It also makes a shallow copy. 

const obj = { a: 1 };
const copy = Object.assign({}, obj);

3. Using JSON – It creates a deep copy of the object. The copy and the original object will not share anything.

let obj = { a: 1, b: { c: 2} }
let copy = JSON.parse(JSON.stringify(obj));

Arrow functions

ES6 introduced a shorter way to define functions called arrow functions.  Taking an example of the normal JavaScript function.

let result = function (i,j) { 
    return i+j; 
}

Using the fat arrow symbol ( => ), you can now create the same function using code like this:

let result = (i,j) => i+j; 

Promises

A promise is just that: a promise to return something at a later time. Either the thing you wanted is returned, or an error. Promises can simplify code that handles the success or failure of asynchronous calls, or code that chains together multiple asynchronous calls.

firstPromise()
    .then(result => {
	// handle result
    })
   .catch(error => {
	 // handle error
    });

References –

https://trailhead.salesforce.com/en/content/learn/trails/learn-to-work-with-javascript

https://scotch.io/bar-talk/copying-objects-in-javascript

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_promises.htm

Published inCoding Mania

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *