CC-00: Functions and Console Logs

with Carmen Salas • 2024/10/01

github


Code Challenge

  1. In this code snippet, what is the argument and what is the parameter?

    const sayHi = (name) => {
      return "Hi" + name
    };
    
    sayHi("fellows");
    

ans: "fellows" is the parameter name is the argument


  1. What is the difference between console.log and the return statement? In your response, be sure to answer the following:

    1. What is the purpose of console.log?

    2. How is the return related to functions in JavaScript?

ans:

  1. console.log's main purpose is to essentially see an output into the console. In other programming languages, this is what print would be.

  2. Every function has a corresponding return. It is implied to return null in occassions where it is not specified. return by itself acts to terminate a function, and optionally hold a value along with it.


  1. What is printed to the console, when the function sayHi is invoked?

    const sayHi = (name) => {
      console.log(name)
      return "Hi" + name;
    };
    
    sayHi("fellows");
    

ans: fellows is printed when the sayHi function is invoked. The return value however, is "Hifellows".


  1. Run this code in a file, what does the variable hello evaluate to? Why does it evaluate to this?

    const hiFunc = () => {
      console.log("hi");
    };
    
    const hello = hiFunc();
    

ans: In the code above, hello evaluates into "hi", as a console.log output. However hello as a variable remains undefined.


  1. What is the difference between let and const? What is the best practice when choosing which keyword to declare a variable with?

ans: let and const are similar in the sense that they are both keywords used to declare variables. The key difference is that let allows variable re-definition/assignment while const does not.

Last updated