The following function takes any and all uppercase letters in a string and turns them into a lowercase letter. Check the console to see what it does.
function makeLower(aString) {
return aString.toLowerCase();
}
console.log(makeLower("ThIs HaS a LoT oF UpPeRcAsE lEtTeRs"));
The same function, but as a function expression. This turns the function into a value and treats a variable as a function. Useful for using the same function for multiple different instances. The code here uses "bString" because "aString" is already in use.
const makeLower2 = function(bString){
return bString.toLowerCase();
}
console.log(makeLower2("THIS IS BEING TYPED WITH CAPS LOCK TURNED ON"));
This removes the need to define something as a function by using an arrow to automatically indicate that it is a function. This makes the code much more condensed.
const makeLower3 = (cString) => {
return cString.toLowerCase();
}
console.log(makeLower3("MAN I SURE DO LOVE TYPING WITH ALL CAPS ON I REALLY LIKE IT WHEN ALL MY LETTERS LOOK LIKE BLOCKS WHEN I SQUINT"));
The best part about this is that you can remove certain parts of the code that define parts of the function due to the way the arrow function is made. This example code will still work even without the curly braces and the return command, the arrow takes care of it by itself.
const makeLower4 = dString => dString.toLowerCase();
console.log(makeLower4("AW YEAH LETTERS LETS GOO LOOK AT ALL OF THESE CAPITAL LETTERS"));
Here are some more examples of how arrow functions work with different paremeters.
const doubleThing = (thingone, thingtwo) => `${thingone} ${thingtwo}`;
console.log(doubleThing('chicken', 'sandwich'));
const magic = () => {
const first = "Abra";
const second = "Kadabra";
const third = "Alakazam";
return `${first} ${second} ${third}`;
}
console.log(magic('first', 'second', 'third'));