Ad

function greetings(name){
return My name is: ${name}
}
//For greetings(aName); the output would be "My name is aName"

const greetings = (name) => My name is: ${name};
//For greetings(anotherName) the output would be "My name is anotherName"

const greetings = (n) => My name is: ${n};
//For greetings(thisName); the output would be "My name is thisName"

Code
Diff
  • const greetings = (name) => `My name is: ${name}`;
    • var myName = 'Elijah'
    • console.log('My name is:'(var myName = 'Elijah'))
    • const greetings = (name) => `My name is: ${name}`;

To have the function display whatever name that you want you can write it in the following ways.

function greetings(name){
  return `My name is: ${name}`
}
//For greetings(aName); the output would be "My name is aName"


const greetings = (name) => `My name is: ${name}`;
//For greetings(anotherName) the output would be "My name is anotherName"


const greetings = (n) => `My name is: ${n}`;
//For greetings(thisName); the output would be "My name is thisName"

This would make the previous code more flexible if you so desire.

Code
Diff
  • function greetings(){
      return 'My name is: seraph776'
    }
    
    • const greetings = () => "My name is: seraph776"
    • function greetings(){
    • return 'My name is: seraph776'
    • }

I inserted all the elements inside an arrow funtion which when it's run prompts the for a number. That number is then saved as the main variable of the function which test for even or odds using the modulo of 2, and returning if its odd or even.

const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";

// Explainations for const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";

// This is equivalent to creating a function with an "if statement", starting by declaring the function

Format:
const functionName = (elements) => [conditional statement] ? if true return this : else this

  • "=>": Turns the variable into a function, it initiates it.

  • "?" : The question mark initiat the "if statement" with the condition and the returning values
    on each side of it.

  • ":" : The colon determines what to do if the condition is met or not '[met]:[unmet]'

Code
Diff
  • const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";
    • const number = prompt("Enter a number: ");
    • if(number % 2 == 0){
    • console.log("The number is even.");
    • }else{
    • console.log("The number is odd.");
    • }
    • const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";