freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../es6/compare-scopes-of-the-var-a.../index.md

3.0 KiB
Raw Blame History

title localeTitle
Compare Scopes of the var and let Keywords 比较var的范围并让关键字

:triangular_flag_on_post:如果卡住,请记得使用**Read-Search-Ask** 。尝试配对程序:busts_in_silhouette:并编写自己的代码:pencil:

问题说明:

我们需要改变varlet我们的职能范围和添加let我们的块范围。

:speech_balloon:提示1

  • 找到var并替换为let

现在尝试解决问题

  • let添加到if语句中的变量i

现在尝试解决问题

扰流警报!

警告牌

提前解决!

:beginner:基本代码解决方案

    function checkScope() { 
      "use strict"; 
      let i = "function scope"; 
      if (true) { 
        let i = "block scope"; 
        console.log("Block scope i is: ", i); 
      } 
    console.log("Function scope i is: ", i); 
    return i; 
    } 

:rocket: 运行代码

代码说明:

通过使用let您可以声明与其范围相关的变量。

相关链接

:clipboard:捐款说明:

  • :warning: 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
  • 添加解决方案的说明。
  • 将解决方案分为以下类别之一 - 基本 中级高级:traffic_light:
  • 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 :warning: 不要 删除任何现有的用户名

看到:point_right: Wiki Challenge Solution Template供参考。