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

能否通过Chrome DevTools查看JavaScript函数参数的类型?

Can I get the variable type of the message parameter in my alertMessage function?

Absolutely! You can absolutely check the type of the message parameter, and your assumption that it's a string in this case is correct. Here's how to do it:

1. Check the type directly inside the function

Use the typeof operator to log the parameter's type when the function runs. Modify your function like this:

function alertMessage(message){ 
  console.log(`Type of message: ${typeof message}`); // Will log "string"
  alert(message); 
}
alertMessage("Hello World");

When you run this, the Chrome console will show Type of message: string right before the alert pops up.

2. Check the type after function execution (if you save the parameter)

The function itself doesn't retain the value of message after it finishes running, but you can store the parameter in a variable outside the function to inspect it later:

let recentMessage;

function alertMessage(message){ 
  recentMessage = message;
  alert(message); 
}

alertMessage("Hello World");
// Now in the Chrome console, type:
typeof recentMessage // Returns "string"

A quick note about alert()'s behavior

You're right that alert() will display any input as a string, but that's because alert() internally converts the parameter to a string before showing it. However, the original type of the argument you pass to alertMessage() is preserved until alert() does that conversion. In your case, since you're passing the string literal "Hello World", the message parameter is indeed a string type from the start.

If you passed a different type (like a number 42 or a boolean true), typeof message would return "number" or "boolean" respectively, even though alert() would still display them as strings.

内容的提问来源于stack exchange,提问作者Hebert AG

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:12:20