Home  >  Q&A  >  body text

return返回值跟document.write输出区别是什么?不能都直接打印出来么?

return跟被注释掉的输出函数

function  app2(x,y)
  { var sum,x,y;
    sum = x * y;
    return sum;
//document.write(sum = x * y + "</br>");
}
看透不说透看透不说透2862 days ago1243

reply all(3)I'll reply

  • 数据分析师

    数据分析师2017-09-30 23:06:48

    What is the difference between return value and document.write output? Can't they all be printed directly? -PHP Chinese website Q&A-What is the difference between return value and document.write output? Can't they all be printed directly? -PHP Chinese website Q&A

    Please watch and learn.

    reply
    0
  • 阿神

    阿神2016-12-20 13:31:13

     return返回的这个值只是把结果返回,并未打印出来,返回的结果可以存在其他参数里面,方便以后的使用,而return如果要打印,还是得调用document.write,如document.write(app(2,3))打印2*3的值 

    reply
    1
  • 迷茫

    迷茫2016-12-20 13:30:36

    你可以这样用,var result=app2(2,3),将返回的结果存在result里面,再输出打印document.write(result),这个函数app2()返回的是一个值,而不是打印输出,如果要打印就得再次调用document.write,而document.write(sum = x * y + "</br>")的情况下,调用函数就会打印输出,不存在返回值,就不能用result=app2(2,3)接收返回的值了

    reply
    1
  • Cancelreply