Home Web Front-end JS Tutorial Things about global variables and local variables_Basic knowledge

Things about global variables and local variables_Basic knowledge

May 16, 2016 pm 05:43 PM
global variables local variables

Variables are very familiar to students who learn js and programming languages. I won’t elaborate on the official definition here. There are too many online. Today we will understand them from life

1. What are variables?
For example:
A water glass contains water, and the water glass is a variable;
A bottle of beer, and the beer bottle is a variable;
A variable is a carrier, a medium

2. Define variables
var a=12; //typeof a=Numer
var a='aaa' //typeof a =string
This shows the type of the variable Depends on what value is paid to him
For example, a cup filled with water is a water glass, filled with wine is a wine glass, and filled with vinegar is a vinegar bottle

3. Variable type
Variable types are divided into: basic types and reference types
Basic types are divided into: Number (numeric type), String (string type), Boolean (Boolean type), Undefined (undefined), Null
Reference types: Most of them are Objects
Basic type values ​​are simple data stored in stack memory, and they occupy a location in memory;
Reference type values ​​are objects stored in heap memory. What is stored in the stack memory is the address, which points to the object in the heap memory

1. Local variables

Copy code The code is as follows:


functionaaa()
{
vara=10;
}
functionbbb()
{
alert(a)
}
aaa()
bbb()


Running result: Error: "a" is undefined, a is a local variable, it only belongs to function aaa, not function bbb
2. Global variable 1
Copy code The code is as follows:


vara
functionaaa()
{
vara=10;
}
functionbbb()
{
alert(a)
}
aaa()
bbb( )


Run result: Undefined pops up. This is also one of the types of variables, but it is an undefined type. It is not the same as the first The type of an undefined
variable is determined by the value assigned to the variable. At this time, a in the bbb function is a global variable. Although it is var, it does not specify a value, so it is undefined
3. Global variable 2
Copy code The code is as follows:


vara
functionaaa()
{
a=10;
}
functionbbb()
{
alert(a)
}
aaa()
bbb()


Running result: 10, a is a global variable and passed The function aaa is assigned a value - 10
ps: We often call undefined as undefined, both through 1 and 2. Can we say undefined≠undefined?
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

Does Go language have static global variables? Does Go language have static global variables? Jul 11, 2023 pm 03:37 PM

The go language does not have static global variables. It uses a more flexible way to handle the need for global variables. Global variables are usually declared at the package level, that is, variables declared outside the function. These global variables are throughout the package. are visible and can be used in any function in the package.

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

Implementing global variable safety in JavaScript Implementing global variable safety in JavaScript Jun 15, 2023 pm 10:33 PM

As JavaScript becomes more popular, more and more websites and applications rely on JavaScript. However, the use of global variables in JavaScript can have security issues. In this article, I will introduce how to implement global variable safety in JavaScript. The best way to avoid using global variables is to avoid using global variables. In JavaScript, all variables are global by default unless they are declared within a function. Therefore, local variables should be used whenever possible

C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? Aug 22, 2023 am 09:22 AM

C++ is an object-oriented programming language, and its flexibility and power often provide programmers with great help. However, precisely because of its flexibility, it is difficult to avoid various small errors when programming. One of the most common mistakes is that when a function returns a pointer or reference, it cannot return a local variable or temporary object. So how to deal with this problem? This article will introduce the relevant content in detail. The cause of the problem is that in the C++ language, local variables and temporary objects are dynamically allocated during the running of the function. When the function ends, these local variables and temporary

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Local variable type inference in Java 10: How to simplify your code using var keyword Local variable type inference in Java 10: How to simplify your code using var keyword Jul 29, 2023 pm 07:32 PM

Local variable type inference in Java10: How to use the var keyword to simplify code Introduction: In Java10, the feature of local variable type inference is introduced. By using the var keyword, the code writing process can be simplified. This article will introduce the use of the var keyword and demonstrate its effect of simplifying the code through sample code. 1. What is local variable type inference? Local variable type inference means that when declaring local variables, you can use the var keyword instead of explicit type declaration. The compiler will express

Redeclaration of global variables in C program Redeclaration of global variables in C program Sep 20, 2023 pm 10:29 PM

We will see how C and C++ behave differently when redeclaring a global variable without initialization, redeclaring a global variable with initialization, and redeclaring a global variable and initializing it twice. Additionally, we will repeat the above combination using local variables. 1.A) C program: Re-declare global variables without initialization #include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;} output Var=0B) C++ program:

See all articles