Home  >  Article  >  Web Front-end  >  ECMAscript variable scope summary tutorial

ECMAscript variable scope summary tutorial

亚连
亚连Original
2018-05-19 09:35:541581browse

This article mainly introduces the ECMAscript variable scope, and explains it through two scenarios: using the var operator declaration and not using the var operator declaration. You can check the detailed explanation below for the specific operation steps. Interested readers Partners can refer to it.

The difference between variables declared using the var operator and variables declared without the var operator

Javascript is a product that follows the ECMAScript standard , Naturally, ECMAScript standards must be followed

ECMAScript variables are loosely typed, that is, they can be used to save any type of data (uninitialized variables will save a special value of undefined).

Not declared using the var operator

  function test() {
    message='hi';
    console.log(message);
  }
  console.log(message);

The variable message not declared using the var operator is global Variable, if the test() method is not called, the message is in an undefined state.

function test() {
    message='hi';
    console.log(message);
  }
  test();
  console.log(message);

The variable message that is not declared using the var operator is a global variable and the test() method must be called. will be effective.

Use the var operator to declare

local variables

function test() {
    var message='hi';
    console.log(message);
  }
  test(); 
  console.log(message);

For variables declared with var within a function, the message is destroyed after calling the test() method, and the console output cannot find this variable.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

jsDetailed explanation of the steps for case conversion of on object array key values

Steps to use the created method in vue.js

Steps to use the created method in vue.js

The above is the detailed content of ECMAscript variable scope summary tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn