SoFunction
Updated on 2025-03-10

A brief introduction and example of variable scope, value transmission and address transmission in the basics of JavaScript

javascript: declaration of variables
Here are some ways to declare variables

Copy the codeThe code is as follows:

 var value;
var value,value1,value2;//Declare multiple variables at the same time, but the values ​​of these variables are undefined
var i = 0,j = 0,k=100;//Variable declaration, initialization integration.
//If you try to read a variable that does not exist, you will report an error! But try to assign a value to a variable that is not declared using Var, javascript
//The variables will be declared implicitly, and the declared variables are still global. Details: So whenever you create variables, try to use Var
//The scope of variables (This question is also easy to come up with, everyone should understand)

javascript: scope of variables
These are all details, just like me, those who are beginners must pay attention to avoid it!
Copy the codeThe code is as follows:

var global = "golbal"; //Global variables
 var local ="local";
function area()
 {
//The priority of local variables is higher than that of global variables
var local = "arealocal"
// When the variable name declared in the function body is the same as the global variable name, javascript will hide the global variable
var golbal ="areagolbal";

("local is :"+local + "and golbal is :" + golbal +"<br />");
 }

area();
//Output:local is :arealocaland global is :areagolbal

What will happen if local variables are defined in nested functions? Look at the following:
Copy the codeThe code is as follows:

var hope = "moremoney";
function createmore()
{
var hope = "have more money";//Parent
function createmoreto()//Nested functions
{
var hope = "have more money to much";//Parent
("Createmoreto hope is :"+hope +"<br />");
//Output:Createmoreto hope is :have more money to much
}
createmoreto();//Call
 ("Createmore hope is :" +hope +"<br />");
//Output: Createmore hope is :have more money
}
createmore(); //Call

javascript: pass value and pass address
This is also a relatively important concept! Don't miss it.

  Pass the value Transfer address
copy There are different and independent copies of the actual copied values. All that is copied is a reference to the number. If the value is modified through this new reference, this change will be visible to the original reference.
transfer The independent copy of the value passed to the function has no effect on its change outside the function What is passed to the function is a reference to the numeric value. This change is also visible if the function modifies the value through the reference passed to it.
Compare Comparing these two opposing values ​​is usually compared byte byte to determine whether they are equal. Comparing two references is done to determine whether they refer to the same value.

javascript: basic types and reference types

The basic rules of javascript are: basic types operate by passing values, and reference types operate by passing addresses. (See my previous article for the value type or quotes for the event)
Pass by value

Copy the codeThe code is as follows:

 var value = 1;
var copyvalue = value; // Assign value to another variable
  function addTotal(total,arg)
  {
total+= arg; //total = total + arg The effect is equivalent
 }
//Calling the function and passing two parameters (you may think that this function changes the value of the global variable, but in fact it does not. The function also uses an opposing copy)
 addTotal(value,copyvalue);
 if(value == 1) copyvalue = 2;
 ("total \t" + value + "and copyvalue \t\t" + copyvalue+"<br />");
//Last output: total 1and copyvalue 2

Pass by address
Copy the codeThe code is as follows:

 var array = new Array("Javascccp");
 var objarray = array;
 function modifyArray(arr)
 {
 arr[0] = "JAVASCRIPT";
 }
//Before the function is called
 (array[0] +"<br />");
//Output Javascccp;
//After calling the function
 modifyArray(array);
 (array[0]+"<br />");
//Output capital JAVASCRIPT
//The same effect will be achieved by modifying objarray
 objarray[0] = "Frank";
 (array[0]+"<br />");
//Output Frank;

Summary: I hope everyone will not miss the above content, it is still very helpful for learning the subsequent knowledge!