salesforce javascript developer 1 - string literals, numbers - my notes

listening to chopin on October 15th, 2021

On my last post about the salesforce javascript cert, I lightly touched upon strings. Let's get into template literalsss. Here are a few straight forward examples on how this works with javascript.

Note: `toFixed()` is a method that formats a number using fixed-point notation.
js
let text = `o hai mark`;
//allows multi line strings
let text2 =
`o hai
mark`;
//the best part of template literals is string interpolation. It's a way to interpolate variables/expressions into the string
let text3 = `what'd she say? ${text}`;
let quantity = 11;
let price = 2.20;
let total = `total is ${(quantity * price).toFixed(2)}`
// total is 24.20

You can create a string as a primitive, e.g let str = "a string" or as an object with the String() constructor. The output will be slightly different if you are using the string() constructor. tbh I have never set a string with a constructor but this is probably one of those gotcha test questions, so you SHOULD know about this. I'm also sneaking in length property in here. ezpz, this just finds the length of the string. Very nice.

js
let str = "lol"
let str2 = new String('lmao');
//console
//String {"lmao"}
let fruit = 'persimmon';
console.log(fruit.length);
// 9

Strings have different types of methods, I'm listing some common ones below with examples:

toLowerCase() - returns string with char converted to lowercase

toUpperCase() - returns string with char converted to uppercase

concat() - concatenates strings together and returns a new one

includes() - returns true/false

indexOf() - returns index of a value if it is there, returns -1 if not found

replace() - searches for string and replaces it with another, returns new string

substring() - searches for string and replaces it with another, returns new string

js
let person = 'Jennay';
let greeting = 'hola';
person.toLowerCase(); // returns 'jennay'
person.toUpperCase(); // returns 'JENNAY'
greeting.concat(person); // returns 'holaJennay'
// if you are concat-ing something like ^, a greeting, I think a template literal is a bit nicer maybe? You can control the spacing and add html, etc
let concating = `${greeting} ${person}`;
let random = `help I am stuck in a well`;
random.includes('well');
// returns true because "well" exists in random.
random.includes('WELL');
// returns FALSE because it is case-sensitive :)
random.indexOf('am');
// returns 7. don't forget js arrays start at 0
random.indexOf('AM');
// returns -1.
random.replace('well', 'fridge');
// `help I am stuck in a fridge` :)
random.substring(0,2);
//returns 'hel'

Just like strings, a number can be created as a primitive or w/ the Number() constructor.

js
let numba = 420;
// or
let anotherOne = new Number(420);

Some common number methods:

isNan() - NaN(Not a Number) checks if the value is not a number. returns true if it is NOT a number.

isInteger() - checks if value is a number AND is an integer

parseFloat() and parseInt() - parses string and returns a number (floating point or int)

toFixed() - converts num to string, rounds number to keep 2 decimals

toString() - converts num to string

valueOf() - returns the value of the string

js
Number.isNaN(42); //false
Number.isNaN(true); //false
Number.isNaN(0/0); //true
Number.isInteger(88); //true
Number.isInteger(6 * 9); //true
Number.isInteger(0.2); //false
Number.isInteger('369'); //false
Number.isInteger(0/0); //true
parseFloat('33'); //33
parseFloat('33.00'); // 33
parseFloat('11.22'); // 11.22
// parseInt
parseInt('11'); // 11
parseFloat('5 6 7'); // 5
parseFloat('21 years'); // 21
parseFloat('Hi I"m 21'); // NaN
let numba = 11;
let numbaString = numba.toString();
// '11'
let test = 'yo rock';
test.valueOf() // yo rock