javascript--variable and primitive type

Source: Internet
Author: User

JavaScript variables

JavaScript variable identifiers
identifier var + variable name to define the variable.
The variable name begins with a letter, an underscore _, a dollar $ sign, and the remaining characters can be letters, numbers, underscores, dollar signs.
Eg:var name = ' Hello ';
var a1 = ' Hello ';
var _test = ' Hello ';
var $test = ' Hello ';
The above variable names are correct.
var 1a = ' Hello '; The number begins with an error.
JavaScript allows multiple variables to be declared at once. The middle is separated by commas.
Eg:var a1 = ' Hello1 ', a2 = ' Hello2 ';

JavaScript is case sensitive.
var a = ' hello1 ';
var A = ' Hello2 ';
A and a are two different variables.
Function name () {}
function Name () {}
The function name and name are two different functions.

JavaScript is a weakly typed language.
The so-called weakly typed language is a variable with no fixed type and no explicit type.
In contrast to the Java language, defining a variable must declare its type.
Java code
eg:string str = ' Hello ';
int num = 19;
JavaScript code
Eg:var str = ' Hello ';
var num = 19;
The types of variables in JavaScript change based on their content.
As above, the variable str is a variable of type string, and the variable num is a variable of number type.
This is the simple benefit of programming for weak types, but it is also confusing for beginners.

Scope of the variable
Variables are divided into global variables and local variables by scope. The difference is in the life cycle.
A local variable is generally declared inside a function, and its scope of use begins at the place where it is declared.
To the end of the function code block where it resides. The function ends with its internal local variable logoff.
Global variables are declared outside the function, and their use ranges from where they live </script>
The label ends, the browser closes, and the global variable is unregistered.
EG1:
<script>
var A;//global variable A, available anywhere
function Test1 () {
var a = 12;//local variable A
It can only be used between the end of the code block where it is declared. That is test1.
}
function Test2 () {
alert (a); Global variable A
}
</script>

In addition, after the use of a variable that is not declared, it becomes a global variable.
EG2:
<script>
var AAA = 111;
QQQ = aaa+222;
function Test () {
alert (QQQ);
}
Test ();
</script>
Declaring the variable AAA, the variable AAA + 222 is assigned to a variable QQQ that has never been declared,
It is found that QQQ can be used as normal zero and QQQ can be used inside the function.
It is visible as a global variable.

JavaScript Raw Value data type
In JavaScript, data classes fall into two categories-primitive value types and reference value types.
The biggest difference between the raw value data type and the reference value data type is the storage structure.
The original value type is stored in the stack, and its value directly exists where the variable is accessed.
The reference value type is stored in the heap and stored in the variable 0 access is an address that points to a small chunk of space in memory.
There are 5 data types in the original value type.
Null,undefined,string,boolean,number
Only 1 data types are used in reference value types
Object
All things are objects, function,array,regexp ...

An explanation of the original data types
Describe the operation of a special operator prior to elaborating the original value data type--typeof
typeof is used to determine the data type of a variable.
The alert () function, which pops up in the browser, is used more incorrectly in JS debugging.

String Type--string
strings are enclosed by single or double quotation marks.
Each character of a string has its specific position, and the first character, index, is 0.
eg
<script>
var str = ' Hello ';
Alert (typeof str);//string
alert (str.length);//5
Alert (Str.charat (0));//h
</script>

Number Type--number
can represent a 32-bit integer or a 64-bit floating-point number.
Can be expressed as octal must start with 0, or it can be expressed as hexadecimal 0, followed by the X-letter.
Scientific notation represents numbers.
EG1:
var num1 = 12;
var num2 = 12.01;
Alert (typeof num1);//number
Alert (typeof num2);//number
EG2:
var num1 = 030//equals decimal 24
var num2 = 0x1a//equals to decimal 26
EG3:
var num1 = 2.13e6//2.13*10 Six-time square.
Boolean type--boolean
Represents logic True and false.
cannot be quoted when defined.
True and false whose value can be equal to the 0,1 price.
eg
<script>
var a = true;
Alert (typeof a)//boolean
alert (a==1); True
alert (a==0); False
var a = 1;
Alert (typeof a);//number
alert (a==true); True
var b = 1;
Alert (a==b)//true
</script>

Undefined type
Undefined is undefined type. Its default value is undefined.
For a non-existent variable or variable, the unassigned initial value is the undefined type.
The default value for the undefined type is undefined, but the value undefined differs from the undefined value.
eg
<script>
var A; Variable A does not have an initial value assigned to it.
Alert (typeof a);//Type Undefined,
alert (a==undefined);//value is undefined
Alert (typeof c); Undefined value of C.
alert (c==a); Error, the use of other operators except the TypeOf operator for C will be faulted
Because a non-existent variable cannot be operated on
</script>
Null type
Represents a null value, and its default value is null.
null and undefined
The undefined is derived from null.
A small problem with null null typeof is an object
In the preceding we say NULL is the original value type and the reference value type is the object.
But a null typeof is an object because of a bug that occurs due to historical reasons, and later explains null
As a placeholder for the object.
eg

<script>
var a = null//variable A has value, value is null
Alert (typeof a); Object
var b//variable B has no value nudefined
alert (a==b);//true
</script>


Reference data type
There is only one object of reference data type.
Borrow a word from Java, all things are objects, this lesson is not discussed object,
We'll cover it in more detail later in the course. The reference type.

Variable Naming conventions
In the previous article our variable names have been randomly named, for programming specifications, our
Variable names should make sense.
The three common naming conventions are described below.
Hump Naming method
The first letter is lowercase and the next letter is the beginning of the uppercase character
var myName = ' Hello ';
Pascal Labeling Method
Capitalize the first letter, and the next letter is the beginning of the uppercase character
var MyName = ' Hello ';
Hungarian Naming Act
Prefix + Next letter uppercase characters start
var smyname = ' Hello ';
We will use the Hump naming convention for future courses.
Also be careful to avoid using JavaScript to keep words and keywords when naming.
Reserved words and keywords are used for the JavaScript language.
The following attached sections retain words and keywords.
Reserved word: Float
Goto
Implements
Import
Native
Package
Private
Protected
......
Key words:
The JavaScript keyword is reserved and cannot be used as a variable name or function name
Var
Inch
If
Break
Do
For
If you use a keyword as a variable name or function name,
May get such as "Identifier expected" (there should be identifiers, expected identifiers)
Such an error message


Limited to the length of the article, here is just a tip. Because the author's level is limited, the writing time is also very hasty,
There will inevitably be some errors or inaccuracies in the text, the wrong place to ask the reader to criticize


Say in the last words:

The BO will open a JS column, long-term update, with all the system to learn JavaScript, to make colorful JS effects.

If it is useful to you, please pay attention to it.

This article from "The Heart has the tiger, the fine smell Rose" the blog, please must keep this source http://zhangdongxu.blog.51cto.com/12029530/1955945

javascript--variable and primitive type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.