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.jslet text = `o hai mark`;//allows multi line stringslet text2 =`o haimark`;//the best part of template literals is string interpolation. It's a way to interpolate variables/expressions into the stringlet 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.
jslet 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
jslet 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, etclet 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 0random.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.
jslet numba = 420;// orlet 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
jsNumber.isNaN(42); //falseNumber.isNaN(true); //falseNumber.isNaN(0/0); //trueNumber.isInteger(88); //trueNumber.isInteger(6 * 9); //trueNumber.isInteger(0.2); //falseNumber.isInteger('369'); //falseNumber.isInteger(0/0); //trueparseFloat('33'); //33parseFloat('33.00'); // 33parseFloat('11.22'); // 11.22// parseIntparseInt('11'); // 11parseFloat('5 6 7'); // 5parseFloat('21 years'); // 21parseFloat('Hi I"m 21'); // NaNlet numba = 11;let numbaString = numba.toString();// '11'let test = 'yo rock';test.valueOf() // yo rock