Home Web Front-end JS Tutorial js global and local variables and operators knowledge sharing

js global and local variables and operators knowledge sharing

Mar 07, 2018 am 11:17 AM
javascript Knowledge operator

1. This article mainly shares with you the knowledge of js global and local variables and operators. First, we will explain the relevant knowledge of local variables and global variables to you, hoping to help you.

2. Local variables and global variables
Local variables: Variables declared inside a js function are local variables, so they can only be accessed inside the function, that is, the scope of the variable is local. So you can use local variables with the same name in different functions.

  1. Use variables defined by var inside the function

  2. Parameter variables of the function

Global variables : A variable declared outside a function is a global variable and can be accessed by all scripts and functions on the web page.

  1. Variables defined outside the function body

  2. Variables defined inside the function body without var declaration

Priority: local variables are higher than global variables with the same name, parameter variables are higher than global variables with the same name, and local variables are higher than parameter variables with the same name.
When passing values ​​to parameters in functions, the values ​​must be enclosed in single quotes. It is strongly recommended to use single quotes.
Scope chain: The inner function can access the local variables of the outer function, but the outer function cannot access the local variables of the inner function.
Life cycle of variables: All variables start when they are declared. Local variables will be deleted after the function is run. Global variables will be deleted after the page is closed.
If a value is assigned to a variable that has not been declared, the variable will be automatically declared as a global variable.
3. Operator
+: Can be used to connect strings. Add two numbers and return the sum of the numbers. If numbers and characters are added, a string is returned. If you add a number to a string, the result will be a string.
==: Only compare values ​​
===: Absolutely equal, compare values ​​and types
! ==: Not absolutely equal (one of the value and type does not want to wait, or both are not equal)
Priority of logical operators:! , &&, ||
Ternary operator: (condition)? true-doing: else-doing
Loop: break ends the entire loop (used in loops and switch statements), continue ends this loop (only used in loops)
4. Type conversion
Number ( ) Convert to number String() Convert to string Boolean() Convert to Boolean value
The data type of NaN is number, array (Array), date (Date), and null type are all objects, and undefined variables are undefined .

2. Local variables and global variables
Local variables: The variables declared inside the js function are local variables, so they can only be accessed inside the function, that is, the scope of the variable is local. So you can use local variables with the same name in different functions.

  1. Use variables defined by var inside the function

  2. Parameter variables of the function

Global variables : A variable declared outside a function is a global variable and can be accessed by all scripts and functions on the web page.

  1. Variables defined outside the function body

  2. Variables defined inside the function body without var declaration

Priority: local variables are higher than global variables with the same name, parameter variables are higher than global variables with the same name, and local variables are higher than parameter variables with the same name.
When passing values ​​to parameters in functions, the values ​​must be enclosed in single quotes. It is strongly recommended to use single quotes.
Scope chain: The inner function can access the local variables of the outer function, but the outer function cannot access the local variables of the inner function.
Life cycle of variables: All variables start when they are declared. Local variables will be deleted after the function is run. Global variables will be deleted after the page is closed.
If a value is assigned to a variable that has not been declared, the variable will be automatically declared as a global variable.
3. Operator
+: Can be used to connect strings. Add two numbers and return the sum of the numbers. If numbers and characters are added, a string is returned. If you add a number to a string, the result will be a string.
==: Only compare values ​​
===: Absolutely equal, compare values ​​and types
! ==: Not absolutely equal (one of the value and type does not want to wait, or both are not equal)
Priority of logical operators:! , &&, ||
Ternary operator: (condition)? true-doing: else-doing
Loop: break ends the entire loop (used in loops and switch statements), continue ends this loop (only used in loops)
4. Type conversion
Number ( ) Convert to number String() Convert to string Boolean() Convert to Boolean value
The data type of NaN is number, array (Array), date (Date), and null type are all objects, and undefined variables are undefined .

Related recommendations:

PHP global and local variables

About the different distinctions between global variables global and $GLOBALS in PHP - WORSHIP Asaph

php global variables What are super global variables

The above is the detailed content of js global and local variables and operators knowledge sharing. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values ​​and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations

Operator precedence list in Go language, which operator has the highest precedence? Operator precedence list in Go language, which operator has the highest precedence? Jan 03, 2024 pm 04:59 PM

There are many operators in Go language, which are often used to perform various mathematical and logical operations. Each operator has its own precedence, which determines the order in which they are evaluated in an expression. This article will introduce you to the priority ranking of operators in the Go language and find out the operator with the highest priority. The operators in Go language are as follows in order from high to low precedence: parentheses: (). Parentheses are used to change the precedence order of operators. Parentheses in expressions are evaluated first. Unary operators: +, -, !. Unary operator means that only one

See all articles