ECMAScript for salesforce

Quick revision to Javascript

Controllers: Visualforce page, Apex or Lightning and Aura

ECMAScript 6 is converted to 5.1, by a transpiler(Babel, Traceur) to support those browsers that supports ECMA 5.1

Lightning uses JS in strict mode and let’s stick to ECMAScript 5.1


Once you are on official page of salesforce, head to lightning components and enable debug mode. Setup mydomain and register and login. Add chrome extension as “Salesforce lightning inspector”. Personal -> adv settings -> Enable development mode. Head to Dan Apleman ->developer console and start coding in File->New

JS vs Apex

Apex is case insensitive, JS is case sensitive!

JS has only one catch but Apex has multiple catch blocks

JS supports switch unlike Apex

Variables: int i; system.debug(i);

In JS, null exists and undefined word denotes undefined i.e. null different from undefined

String +int =string — JS(vars are untyped) but error in Apex(|||lr to C++)

Use parse (parseInt(“23.5”)) for conversions in JS

“5” == 5 — True in JS, so use ===


APEX Objects

public class ClassName{public String firstname;
public String fullname{ ... } //function
var j = new Object();
j.firstname = “ewuri”;
j.studid = 1;

etc… can be assigned… dynamically without creating the class and including the member in the class. Can also use:

JS Objects

"firstname" : {value: "ewuri", write:true},
"studid" :1;


var j = "{"kdf": "suerew", stuid = 1}"
var k= JSON.parse(j) //to convert string to JSON

“k” is now in JSON format

Default object is window



returnType nameOfFunc (params){...


No return type

function nameOfFunc(params) { ... }
j.variable = function(){ ... } //fullname member is directly appended to the object j
j.fullname; //gives function(){ ... } i.e. property
j.fullname(); // gives value i.e. "weruiu"

In JS, any number of params are allowed i.e. no overloading

In JS, constructors are always capitalized

Use new operator to define new objects

ARRAYS: Functions can also be the member of array as they are kinda objects…

arr = [function dics(word){ console.log("this is the meaning of word");}];
arr[0](it) //output: this is the meaning of it

AJAX — Asynchronous Javascript and XML