You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求极简JavaScript乘法函数:返回两个数值而非对象(非生成器函数)

Solution for Returning Two Values in Basic JavaScript

Got it, let's sort this out for you! Since you can't use objects or generator functions, the simplest beginner-friendly way to return two separate values in JavaScript is to use an array—arrays are perfect for holding multiple values without any fancy syntax.

Your current code uses the comma operator, which only returns the last expression in the sequence (that's why you're only getting 25 instead of both 500 and 25). Here's the minimal, clean fix:

function multiplier() {
  var number = 25;
  return [number * 20, number * 1];
}

// To use the two results:
// Option 1: Store the array and access elements by index
const results = multiplier();
console.log(results[0]); // Outputs 500
console.log(results[1]); // Outputs 25

// Option 2: Use destructuring (still beginner-friendly!)
const [calculatedValue, originalValue] = multiplier();
console.log(calculatedValue); // Outputs 500
console.log(originalValue); // Outputs 25

If you just need to log the results immediately without saving them for later, you could also do this (though returning an array is more flexible):

function multiplier() {
  var number = 25;
  console.log(number * 20);
  console.log(number * 1);
}

multiplier(); // Logs 500, then 25

Both solutions stick strictly to basic JavaScript—no advanced features, no redundant code. The array approach is the best choice because it lets you reuse the values elsewhere in your code if you need to.

内容的提问来源于stack exchange,提问作者Democratize The Enterprise

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:56:38