This article introduces the simple introduction of JavaScript variables and data types, is the basic knowledge of JS, the need for friends can refer to the
JavaScript data type:
One of the most fundamental features of a programming language is a set of data types that it supports. These are the types of values for programming languages that can be represented and manipulated.
JavaScript allows three basic data types:
Numbers such as. 123, 120.50 and so on.
String such as "This text string".
A Boolean type, such as true or false.
JavaScript also defines two types of data: null and undefined, each of which limits only a single value.
In addition to these basic data types, JavaScript supports composite data types called objects. We'll see the details of the object in a separate section.
Note: Java does not make the difference between integer and floating-point values. All numbers in JavaScript are represented as floating-point values. JavaScript represents the use of a number in the 64-bit floating-point format defined by the IEEE 754 standard.
JavaScript variables:
Like many other programming languages, JavaScript has variables. A variable can be considered a naming container. You can put data into these containers and then refer to the data to simply name the container.
To use a variable in a JavaScript program, you must declare it. Variables are declared with the VAR keyword as follows:
?
1 2 3 4 5 6 |
<script type= "Text/javascript" > <!--var money; var name; --> </script> |
You can also declare multiple variables with the same var keyword as follows:
?
1 2 3 4 5 |
<script type= "Text/javascript" > <!--var money, name; --> </script> |
The value stored in the variable is called initialization of the variable. You can initialize a variable when it is created or updated, and you need a variable, as follows:
For example, you can create a variable money and 2000.50 value, and then assign it. For another variable, you can assign a value when initialized as follows:
?
1 2 3 4 5 6 7 |
<script type= "Text/javascript" > <!--var name = "Ali"; var money; Money = 2000.50; --> </script> |
Note: Use the VAR keyword to declare or initialize only. Once the variable name declares its lifecycle throughout the document. You do not need to declare the same variable two times.
JavaScript is a typed language. This means that JavaScript variables can hold values of any data type. Unlike many other languages, you do not have to declare variables on what type of value the variable will hold to tell JavaScript. The value type of a variable can be changed during program execution and JavaScript automatically.
JavaScript variable range:
The scope of a variable is the region in the program that defines it. JavaScript variables will have only two categories.
Global variables: Global variables have global scope, which means that it is ubiquitous in the definition of JavaScript code.
Local variable: Local variable will only be there the function that it is defined to be visible. The parameters of the function are local functions.
In the body of a function, a local variable takes precedence over a global variable with the same name. If the declaration has the same name as a global variable, a local variable or function parameter, you can effectively hide the global variable. Let's illustrate the following examples:
?
1 2 3 4 5 6 7 8 9 |
<script type= "Text/javascript" > <!--var myVar = "global"; Declare a global variable function checkscope () {var myVar = ' local ';//Declare a local variable document.write (Myva R); }//--> </script> |
This will produce the following results:
?
Variable name for javascript:
Although variable naming in JavaScript maintains the following several rules.
You should not use any reserved javascript keyword as the variable name. The next section mentioned in these keywords. For example, a break or a Boolean variable name is not valid.
JavaScript variable names should not start with a number (0-9). They must be marked with letters or underscores. For example, 123test is the name of an invalid variable, but _123tes T is a valid one.
JavaScript variable names are case-sensitive. For example, name and name are two different variables.
Reserved words for javascript:
The following are reserved words in JavaScript. They cannot be used for such as JavaScript variables, functions, methods, loop tags, or any object names.