CC-00: Functions and Console Logs
with Carmen Salas • 2024/10/01
Code Challenge
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
What is the difference between
console.log
and thereturn
statement? In your response, be sure to answer the following:What is the purpose of
console.log
?How is the
return
related to functions in JavaScript?
ans:
console.log
's main purpose is to essentially see an output into the console. In other programming languages, this is whatprint
would be.Every function has a corresponding
return
. It is implied to returnnull
in occassions where it is not specified.return
by itself acts to terminate a function, and optionally hold a value along with it.
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"
.
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
.
What is the difference between
let
andconst
? 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