Beginner learning javascript

问题: I am beginner in javaScript, I am trying to practice if and else statements. I have defined a function and called the function. Everything is working but i am not able to u...

问题:

I am beginner in javaScript, I am trying to practice if and else statements. I have defined a function and called the function. Everything is working but i am not able to understand why I am getting "undefined" in console.log with the executed function.

console.log("Starting");

function data(school) {
  if (school == 10) {
    console.log("its old");
  } else if (school <= 9) {
    console.log("its new");
  } else {
    console.log("no-data-available");
  }
}
var result = console.log(data(10));

console.log("Closing");


回答1:

You don't need to call console.log() on data(), since it calls console.log() on its own (and returns undefined, which you're seeing).

However, if you change data() to return the values, not log them, then you do need to call console.log().

In either case, when you're assigning var result = console.log(...), result will end up being undefined.

function data(school) {
  if (school == 10) {
    return "its old";
  } else if (school <= 9) {
    return "its new";
  } else {
    return "no-data-available";
  }
}
var result = data(10);
console.log(result);

will probably do what you're looking for.


回答2:

This is because you are not returning anything from the function explicitly.

Function Description

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.

If the value is omitted, undefined is returned instead.


回答3:

The error is occurring because your function data() doesn't return any value. You need to return some value if you don't want to print undefined.

function data(school) {
  if (school == 10) {
    console.log("its old");
    return 'old data';
  } else if (school <= 9) {
    console.log("its new");
    return 'new data';
  } else {
  console.log("no-data-available");
    return 'data unavailable';
  }
}

回答4:

Solve this problem

console.log("Starting");

function data(school) {
  if (school == 10) {
    console.log("its old");
  } else if (school <= 9) {
    console.log("its new");
  } else {
    console.log("no-data-available");
  }
  return school;
}
var result = console.log(data(10));

console.log("Closing");


回答5:

Because your function does not have a return statement, in such cases function returns undefined value. So last line

console.log(data(10));

prints undefined.

You need to return the values from data function, if you want to print them outside.

console.log("Starting");

function data(school) {
  if (school == 10) {
    return "its old";
  } else if (school <= 9) {
    return "its new";
  } else {
    return "no-data-available";
  }
}
var result = console.log(data(10));

console.log("Closing");


回答6:

there is no need of appending the function result to the variable , a function can be directly called.

function data(school) {

  if (school == 10) {
    console.log("its old")

  } else if (school <= 9) {
    console.log("its new")

  } else {
    console.log("no-data-available")

  }
}
data(10);


console.log("Closing");


回答7:

When JavaScript reaches a return statement, the function will stop executing.

If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.

Functions often compute a return value. The return value is "returned" back to the "caller":

console.log("Starting");

function data(school) {
  if (school == 10) {
    return "its old";
  } else if (school <= 9) {
    return "its new";
  } else {
    return "no-data-available";
  }
}
var result = console.log(data(10));

console.log("Closing");


回答8:

you are not returning any values. Since this question is answered 15 times I will give you something different. First off, in JavaScript it is often preferred to use three = because then you also test if the type is the same. E.g 1 == "1" // true but 1 === "1" // false.

However, if you can say that anything above 10 is old, then a ternary expression could be used. I would suggest this (using arrow function)

const checkSchoolAge = schoolAge => schoolAge >= 10 ? 'It's old!' : 'It's new!';

console.log(checkSchoolAge(10)); // It's old!
  • 发表于 2019-07-05 10:51
  • 阅读 ( 97 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除