simple (subjective statement) exercises in JavaScript theory: syntax, object-oriented programming (OOP) etc.
- a function that prints the classic "Hello World" in the console, with no arguments passed to it
function printHW(){console.log("Hello World!");}
printHW();
- a function that prints the classic "Hello World" in the console, with a string argument passed to the function
function printHW(str){console.log(str);}
printHW("Hello World!");
- above function, but written as a value
let printHW = function(str){console.log(str);}
printHW("Hello World!");
- nested function
function printH(){
function printW(){return "World";}
return `Hello ${printW()}!`
}
console.log(printH());
- passing arguments to nested functions
function printFinal(word){
function getLastWord(word){return `Beautiful ${word}`;}
return `Hello ${getLastWord(word)}!`
}
console.log(printFinal("World"));
- arrow function
let sqrOfNum = (num) => {return num * num;}
- a simple class
class Animal {
constructor(weight, dominantColor) {
this.weight = weight;
this.dominantColor = dominantColor;
}
}
let Fido = new Animal(5200, "brown");
console.log(`weight: ${Fido.weight}, color: ${Fido.dominantColor}`);