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 basic knowledge points of variables in JavaScript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about JavaScript variable basics. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

variables

1.1 Concept of variables:

A variable is a named unit of storage in a program whose primary function is to provide a container for storing information for data operations. A variable can be thought of as a container for data.

1.2 Declarations and assignments of variables

In JavaScript, variables need to be declared before they are used, and variables are declared using the system keyword var. You can also assign a variable with the assignment sign "=" when declaring a variable. The syntax is as follows:

var variable name = variable value

Examples:

var name ; //Declare a variable var name,city,like ; //Declare multiple variables, separated by commas in English var name = "Aze"; //Declare a variable and assign values

1.3 Naming conventions for variables

Variables cannot start with a number, they can start with a letter or underscore.

JavaScript variable names are case-sensitive.

You cannot use keywords in JavaScript as variable names.

2. the data type of the variable

The type of variable depends on the type of value.

2.1 numeric

Numeric variables can be mathematically manipulated, including integers, floating-point types, and NaN.

Example:

var a = 10;var y = 0.1;var x = 100;

NaN (not a number) is not a number. When other data types are converted to numerical types and cannot be turned over, but the program cannot report errors, a NaN value will be returned. The following circumstances apply:

php.cn var length = "500m"; length = length*5; [xss_clean](length);

Now we want to make something five times longer, a string that can't be converted to meaningful numbers, can only be converted to NaN.

A string containing pure numbers, can be converted to meaningful values, you can modify the length of a string of pure numbers, output to view the results.

2.2 Character type

A string enclosed in single or double quotes.

It should be noted that single quotation marks and double quotation marks can be nested with each other; only double quotation marks can be nested within single quotation marks, and only single quotation marks can be nested within double quotation marks.

Document var name = "Aze"; var str = "My name is '" +name+"" [xss_clean](str)

Where the plus sign is a string connector, if you want to nest double quotes inside double quotes, double quotes inside must be escaped (\"). The escape character in JS is the backslash (\).

Common escape characters are: \',\",\\,\r,\n, etc.

Document var name = "Aze"; var str = "My name is\"" +name+"\"" [xss_clean](str) y){ [xss_clean](a+"greater than"+b+"); }else{ [xss_clean](b+"greater than"+a+"); }

2.4 undefined type

Undefined values have only one undefined.

A variable definition, if not assigned, returns undefined; an object, if the attribute does not exist, returns undefined;

Example: No assignment

Document var x; [xss_clean](x);

2.5 space form

The null type has only one value: null.

When an object does not exist, null is returned; if you want to clear the value of a variable, you can assign null.

Document var x = null; var a = 100; var a = null ; [xss_clean](x); [xss_clean](a); Thank you for reading! About "JavaScript variable basic knowledge points what" This article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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