--- title: String.prototype.toUpperCase() slug: Web/JavaScript/Reference/Global_Objects/String/toUpperCase tags: - JavaScript - 原型 - 字符串 - 引用 - 方法 translation_of: Web/JavaScript/Reference/Global_Objects/String/toUpperCase --- <div>{{JSRef}}</div> <p><strong><code>toUpperCase()</code></strong> 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。</p> <div>{{EmbedInteractiveExample("pages/js/string-touppercase.html","shorter")}}</div> <p class="hidden">这个交互式示例的源代码存储在GitHub存储库中。如果您想对交互式示例项目作出贡献,请克隆<a href="/zh-CN/docs/">https://github.com/mdn/interactive-examples</a>并向我们发送请求。</p> <h2 id="语法">语法</h2> <pre class="syntaxbox notranslate"><var>str</var>.toUpperCase()</pre> <h3 id="返回值">返回值</h3> <p>一个新的字符串,表示转换为大写的调用字符串。</p> <h3 id="错误处理">错误处理</h3> <dl> <dt>{{jsxref("TypeError(类型错误)")}}</dt> <dd>在 {{jsxref("null")}} 或 {{jsxref("undefined")}}类型上调用,例如:<code>String.prototype.toUpperCase.call(undefined)</code>.</dd> </dl> <h2 id="描述">描述</h2> <p><code>toUpperCase()</code> 返回转为大写形式的字符串。此方法不会影响原字符串本身的值,因为JavaScript中字符串的值是不可改变的。</p> <h2 id="示例">示例</h2> <h3 id="基本用法">基本用法</h3> <pre class="brush: js notranslate">console.log('alphabet'.toUpperCase()); // 'ALPHABET' </pre> <h3 id="将非字符串类型的_this_(上下文)转为字符串">将非字符串类型的 <code>this</code> (上下文)转为字符串</h3> <p>此方法会将任何非字符串类型的值转为字符串, 当你将其上下文 <code>this</code> 值设置为非字符串类型</p> <pre class="brush: js notranslate">const a = String.prototype.toUpperCase.call({ toString: function toString() { return 'abcdef'; } }); const b = String.prototype.toUpperCase.call(true); // 输出 'ABCDEF TRUE'。 console.log(a, b); </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-string.prototype.touppercase', 'String.prototype.toUpperCase')}}</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div class="hidden">此页中的兼容性表是从结构化数据生成的。如果你想对数据有所贡献,请查看<a href="/zh-CN/docs/">https://github.com/mdn/browser-compat-data</a>并向我们发送请求。</div> <p>{{Compat("javascript.builtins.String.toUpperCase")}}</p> <h2 id="参见">参见</h2> <ul> <li>{{jsxref("String.prototype.toLocaleLowerCase()")}}</li> <li>{{jsxref("String.prototype.toLocaleUpperCase()")}}</li> <li>{{jsxref("String.prototype.toLowerCase()")}}</li> </ul>