2

Here is my code

function consec(string) {
  for (let letter of string) {
    console.log(letter);
  }
}

console.log(consec("zoo"));

the result is

"z"
"o"
"o"
undefined

This is the link of my code. Please feel free to correct. (It's my first to ask question through SO, I appreciate any advices :))

2
  • 2
    Last undefined is the return value of consec() function. Since you are not returing anything from consec(), it is displaying undefined. Commented Feb 9, 2018 at 7:31
  • undefinedhere is the return value from the function. The characters are logged in the function, and "undefined" is logged from the .log which calls consec because the function doesn't return anything. Commented Feb 9, 2018 at 7:31

1 Answer 1

6

Remove the console.log() from console.log(consec("zoo")); then you will not get that undefined printed in console as the last undefined is from the return value of consec("zoo"); which is undefined.

function consec(string) {
  for (let letter of string) {
    console.log(letter);
  }
}

consec("zoo");

Let's say if you have return value in function then you get that in console like this:

function consec(string) {
  for (let letter of string) {
    console.log(letter);
  }
  return("Finished");
}

console.log(consec("zoo"));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.