Home Web Front-end JS Tutorial Teach you how to declare variables in JavaScript in different ways

Teach you how to declare variables in JavaScript in different ways

Aug 17, 2021 pm 02:13 PM
javascript variable

In the previous article "How to return array elements greater than the specified number through js", I introduced how to return array elements greater than the specified number through js. Interested friends can learn about it. ~

Then the theme of this article is to teach you how to declare variables in different ways in JavaScript!

First of all, everyone should also know that in JavaScript, variables can be declared in different ways by using different keywords. After all, each keyword has some specific functions in JavaScript; basically we can use Variables are declared in three different ways using the var, let and const keywords, and each keyword is used under certain conditions.

Note: Before 2015, we used the var keyword to declare JavaScript variables; ES2015 (ES6) added two important JavaScript keywords let and const.

The first wayvar

var:This keyword is used to declare variables globally. If you use this keyword to declare a variable, the variable can also be accessed and changed globally.

Note: It is suitable for shorter codes.

The syntax is:

var variableName = "Variable-Value;"
Copy after login

An example of declaring variables through var:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
</head>
<body>

<script>

    var geeks = "I love PHP中文网";
    console.log(geeks);

</script>
</body>
</html>
Copy after login

will output "I love PHP Chinese website", as shown below:

Teach you how to declare variables in JavaScript in different ways

The second waylet

let: This keyword is used As for declaring variables locally, if you declare a variable using this keyword, the variable is accessible locally and is also mutable.

Note: Variables declared by let are only valid within the code block where the let command is located.

Syntax:

let variableName = "Variable-Value;"
Copy after login

An example of declaring variables through let:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
</head>
<body>

<script>

    if (true) {

        let geeks = "I love PHP中文网";

        console.log(geeks);

    }
    

    /* 显示未定义错误 */

    console.log(geeks);

</script>
</body>
</html>
Copy after login

Output:

Teach you how to declare variables in JavaScript in different ways

The third wayconst:

const: This keyword is used to declare variables globally. If you declare a variable using this keyword, the variable is globally accessible and cannot be changed.

Note: const declares a read-only constant. Once declared, the value of the constant cannot be changed.

Grammar:

const variableName = "Variable-Value;"
Copy after login

An example of declaring variables through const:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
</head>
<body>

<script>
    const geeks = "I love PHP中文网";

    console.log(geeks);

</script>
</body>
</html>
Copy after login

Output:

Teach you how to declare variables in JavaScript in different ways

Finally for everyone Recommended "JavaScript Basics Tutorial"~Welcome everyone to learn~

The above is the detailed content of Teach you how to declare variables in JavaScript in different ways. 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

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

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

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

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

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

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

See all articles