Friday, January 6, 2023

JavaScript Functions for Salesforce

 'use strict'; // Avoid using variables without the declartaion

val = 'test';
console.log(val); // You see error 

---
// Functions 
function fn() {
	console.log('Inside fn');
}

// Calling function
fn();

console.log(calculateBill(500,5)); // This will work as soon as it is declrated atleast later
// Function Declartaion
function calculateBill(billAmount, numOfGuys) {
	let billStmt = `Each guy bill is ${billAmount}. Total bill for ${numOfGuys} guys is ${billAmount * numOfGuys}`;
	return billStmt;
}

console.log(calcBill(799,5));  // This won't work due to hoisting
// Function Expression - This is better to use 
const calcBill = function(billAmount, numOfGuys) {
	return billAmount * numOfGuys;
}

// Function Expression
const calcAge1 = function(birthYear) {
	return 2021 - birthYear;
}

// ES6 - Arrow Function
const calcAge2 = birthYear => 2021 - birthYear;

console.log(calcAge1, calcAge2);

const isEligible4License = birthYear => {
	const age = 2021 - birthYear;
	const isTrue = (age >=18 ? true : false);
	return isTrue
}
console.log(isEligible4License(30));

const calcBill2 = (billAmount, numOfGuys) => billAmount * numOfGuys;
console.log(calcBill2(799,5));

// Calling a function from the another function
const calcBill3 = (billAmount, numOfGuys) => calcBill(billAmount, numOfGuys);
console.log(calcBill3(799,5));

No comments:

Post a Comment