There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hello there! I am getting a little confused as to when semicolon ; should be used after curly braces {}. I see most times it is not used, but sometimes it is. Could you please clarify this? Thanks!
You’d look at what the statement in question looks like, either it includes a semi-colon, or not.
Redundant semi-colons results in empty statements which is why you won’t notice having too many, and when they’re missing javascript adds them anyway which is why you also won’t notice if there are too few.
Curly braces have different meanings depending on what they’re part of. They’re not their own “thing”.
There seem to be multiple formats for writing the code in this lesson, which causes confusion. Page 2 of 11 dictates that calling a function requires console.log(identifier());. Page 3 of 11 states only identifier(); is needed. Requesting the solution is the first time identifier(); (without console.log) is introduced as a way to call the function.
To be more clear, this is on page 2:
function greetWorld() {
console.log(‘Hello, World!’);
}
console.log(greetWorld()); // Output: Hello, World!
Page 3 requests a function to be called 3 times. Here, “console.log(identifier());” is wrong. This is the method the program now wants:
functionName();
In what case do you use console.log(greetWorld();? What is the appropriate use for this command? Could you provide a practical example of how it is different from greetWorld();? The language that you used is not making sense to me.
I’m also struggling to wrap my head around this a bit - the undefined part…
I understand calling the function, as per the next exercise, like so:
function getReminder() {
console.log('Water the plants.');
}
getReminder();
Logged:
Water the plants.
The function is declared, and then the “instructions” in the body are called using the identifier.
…and I understand returning a value by printing to console, as @mtf described above:
function getReminder() {
return "Water the plants.";
}
console.log(getReminder());
Logged:
Water the plants.
We declare the function and then print the result of the instructions “return a value”.
…but the below exercise, where you do the following does not make sense.
function getReminder() {
console.log('Water the plants.');
}
console.log(getReminder());
Logged:
Water the plants.
undefined
So the function is declared first (which on its own would not log anything), then console log which includes the function identifier… is this the same as calling the function and why we get “Water the plants” printed? Why is undefined printed? Is the second console.log seen as empty even though it contains the identifier?
you never need it, semicolons are optional, but if you’re going to use them, use them consistently. Semicolons are used to mark the end of the line to help others read your code. since we use multiple lines to display one line of code, it makes it easier to read. so your example:
const example = () => { if (...) { ... } };
would have the semicolon at the end of the line, but if we wrote the line out like this:
const example = () => {
if (...) {
}
};
the two are the exact same just divided up on different lines to help us read it but the semicolon should be at the end of the line of code.
You also could end the if statement with a semicolen before the curly braces “{}”:
const example = () => {
if (...);
};
some times, theres a line of code, winin a line of code. like with a do…while statement:
do {
if (...){
};
if (...) {
};
var++;
if (...) {
};
while (...) {
};
we use the semicolons to divide the different codes we want to run within the do statement
There are no indications in this first lesson on functions that I needed to provide a function call in order to have anything log to the console. I had to do a google search to find that out.
Instead of being clever and talking about hoisting, you might want to work on clearer instructions.
JavaScript Hosting.
Hello, I’m hoping you can answer my question about JavaScript Hosting.
When would you use it? Is there any situation when it useful to declare a variable before its used, or is it just a quirk of JavaScript to be aware of? I’ve had a nosy round the Internet, plenty of articles about how hosting works but nothing on when/if you would potentially use it. Cheers