Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the requirements of variable naming in javascript

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the requirements for the naming of variables in javascript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "what are the requirements for variable naming in javascript"?

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, you can use the keyword: const,var,let to define variables, syntax:

Keyword variable name

The variable is equivalent to the container, the value is equivalent to what is contained in the container, and the variable name is the label affixed to the container, through which you can find the variable so that you can read and write the value it stores.

It should be noted that variable names are not optional, and a valid variable name should meet the following requirements:

Variable names are case sensitive

Variable names can only contain letters (Abeliz), numbers (0,9) and underscores (_). Spaces and other punctuation marks are not allowed.

Variable names must begin with a letter or underscore, not with a number

Prohibit the use of JavaScript keywords and reserved words as variable names

The length of variable naming should be as short as possible, and grasp the main points, as far as possible to reflect the type of value in the variable name.

Try to avoid using meaningless naming

The common naming methods of variable names are Hungarian naming, hump naming and Pascal naming.

Hungarian nomenclature

Hungarian nomenclature is a kind of variable naming rule in computer programming, which can be subdivided into systematic Hungarian nomenclature and Hungarian application nomenclature.

Hungarian nomenclature is language independent and is widely used in BCPL for the first time. Because BCPL has only one data type, machine words, the language itself does not help programmers remember the types of variables. The Hungarian nomenclature solves this problem by specifying the data type of each variable.

In Hungarian nomenclature, a variable name begins with one or more lowercase letters that help you remember the type and purpose of the variable, followed by any name chosen by the programmer. The first letter of the second half can be capitalized to distinguish the preceding type indicator letter.

Grammar

Variable name = type + object description

Type refers to the type of variable

Object description refers to the full name or part of the name of the object, which is required to have a clear meaning, and the naming should be easy to remember and understand.

The type of the variable is indicated by prefixing the symbol of the corresponding lowercase letter to the variable name, followed by a combination of one or more words that describe the purpose of the variable. If it is a jquery object, use $as the prefix for the variable name.

Prompt

Although there is no type on the surface of the javascript variable, the corresponding type is assigned to the variable internally in javascript.

The Hungarian nomenclature was invented by a Microsoft programmer and is used by most cMagnec programs.

Types

Javascript variable naming type variable naming prefix array array aboolean Boolean value bfloat floating point lfunction function fnint integer iobject object oregular regular rstring string s

Example

Var aName = [1,2,3]; var oBtn = document.getElementById ('btn'); function fnName () {}; var iCount = 0polivar sName = "zhuyujia"

Hump nomenclature

When the variable name and function name are unique identifiers formed by two or more words linked together, the use of "hump case" can increase the readability of variables and functions.

"the word Camel-Case comes from the mixed case format commonly used in the Perl language, and the cover picture of Larry Wall et al's best-selling book Programming Perl (published by O'Reilly) is a camel."

The "hump case" naming rule can be regarded as a convention, not absolute or mandatory, in order to increase identification and readability. Once the naming rules are selected or set, you should maintain a consistent format when writing the program.

Grammar

Variable names or function names are linked by one or more words, the first word begins with a lowercase letter, and all subsequent words begin with uppercase letters, so the variable name looks like a camel hump. Hence the name.

Example

Var myName = "zhuyujia"; var formSubmit = document.getElementById ("submit"); function timeCount () {}

Pascal nomenclature

Pascal naming method (Pascal Case, Pascal naming method / Pascal naming method), a set of naming rules (conventions) for computer programming.

When the variable name and function name are unique identifiers formed by two or more words linked together, it is used to increase the readability of variables and functions.

Words are not broken by spaces or linked with a connection sign (-) or underscore (_). The first single prefix letter is capitalized, and the first letter of subsequent words is also capitalized, such as FirstName, LastName. The first letter of each word is named in uppercase format, which is called "Pascal nomenclature". It is derived from the naming convention of Pascal language, and some people call it "great hump nomenclature" (Upper Camel Case), which is a subset of hump capitalization.

Pascal nomenclature can be regarded as a naming convention, not absolute or mandatory, in order to increase identification and readability. Once the naming rules are selected or set, the format should be consistent when writing the program.

Grammar

Similar to hump naming, except that the first letter of the first word needs to be capitalized.

Example

Var MyName = "zhuyujia"; var FormSubmit = document.getElementById ("submit"); function TimeCount () {} so far, I believe you have a deeper understanding of "what are the requirements for naming variables in javascript". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report