Javascript beginners Article 1 JS basics page 1/2

Source: Internet
Author: User

Javascript: First JavaScript Basics

1. Javascript character set:
Javascript uses the Unicode Character Set encoding.
Why is this encoding used?
The reason is simple. The 16-bit Unicode code can represent any written language of the Earth. This is an important feature of language internationalization. (You may have seen writing scripts in Chinese, such as: function my function (){});
Each character in Javascript is expressed in two bytes. (Because it is a 16-bit code)

2. Case Sensitive:
Javascript is a case-sensitive language.
Note: The mistakes I have made before.
HTML is case-insensitive. I often see people writing this,
<Input type = "button" onclick = "A ()"/> (this is correct)
If you put it in JS, you must use onclick (lower case !)
At the same time, only lower-case characters can be used in XHTML.
We don't need to worry too much about this. In fact, you can set a standard for yourself and write it yourself.ProgramIn lower case.
In addition, the semicolon after each line of the program is also the same, we write.

3. Notes:
Single row:
// Note 1
/* Comment 2 */
Multiple rows:
/* Comment 3
* Note 3
* Note 3
*/

4. identifier:
An identifier is a name used to name variables and functions.
Rule: The first letter must be a letter, underscore (_), or dollar sign ($ ).
Why cannot the first letter be a number?
If the first digit is a number, it is easy for js to treat it as a number, so it makes no sense to name it. After JS defines it, it is easy to distinguish between identifiers and numbers.

5. Direct Volume:
Is the data value directly displayed in the program.
For example: 12, 1.2, "hello", true, null, [1, 2, 4]
These are all direct quantities.

6. Reserved Words and keywords:
Specifically, you can go to google.cn.
In fact, we will not conflict as long as we do not take some depressing names.

7. js data type:
Three basic types: Numbers, strings, and boolean values.
Two small data types: null and undefined. (why is it called a small data type? Because they only define one value)
One composite type: object. (In this type, its value can be either a basic data type or a composite type, such as other objects .)
Note: There is a special object in the object ---- function. (It is a executableCodeObject .)

Other objects:
Array:
Date class: indicates the date object.
Regexp class: the regular expression object.
Error class: an object with an error in Js.

8. Note the following when using data types:
1): Number:
What are the octal, decimal, and hexadecimal numbers...
Octal: var num = 011; // starts with "0"
Hexadecimal: var num = 0x1f; // It starts with "0x"
Therefore, attention must be paid to Javascript, a language that can be recognized.
Alert (377); // 377.
Alert (0377); // 255 = 3*64 + 7*8 + 7*1

There is an important object for arithmetic operations: math.
For details, download the Manual Online and query the methods in the manual.

Two useful functions: isnan () and isfinite ()
Isnan (): used to check whether the parameter is a non-numeric value. // Prompt: it is not a number. (Not a number)
Document. Write (isnan (0) // return false
Document. Write (isnan (5-2) // return false
Document. Write (isnan ("hello") // return true

The isfinite (number) function is used to check whether its parameter is infinite.
If the number is limited, true is returned. If the number is Nan (not a number) or infinite, false is returned;

2): character:
'You' re right ';
In this case, JS will mistakenly think that it will end after the letter you, causing an error.
In this case, escape must be used.
We can write as follows:
'You \'re right ';
In addition, you can search Google.com for the list in the descending order.

Simple string operation example:
VaR A = "cssrain ";
VaR B = A. charat (A. Length-1); // extract the last character from string. Output: N
VaR c = A. substring (0, 2); // truncates 1 or 2 Characters from string. Output: CS
VaR d = A. indexof ('s '); // locate the first s in string. Output: 1
From the example, we can see that the base numbers start from 0.
VaR E = A. substring (A. Length-1); // you can see that if the substring 2nd parameters are not written,
// The default value is the last one.
VaR F = A. substring (A. Length-1, A. Length); // equivalent

3) Conversion between numbers and characters:
Numeric to character:
VaR number_to_string = Number + ""; // Method 1: add an empty string.
VaR number_to_string = string (number); // Method 2: Use the string () function.
VaR number_to_string = number. tostring (); // method 3: Use the tostring () function.
Note: The tostring () method is converted in decimal format by default.
If you want to use an octal conversion, you can write this: Number. tostring (8 );

Character to digit:
VaR string_to_number = string-0; // Method 1: String minus 0.
VaR string_to_number = Number (string); // Method 2: Use the number () function.
VaR string_to_number = parseint (string); // method 3: Use the parseint () function.
Method 1 cannot use string + 0; this will cause String concatenation, rather than type conversion.
The number function in method 2 is strictly converted.
For example:
VaR A = "19cssrain86 ";
VaR B = Number (a); // output Nan.
If we use method 3.
VaR c = parseint (a); // output 19
We can see that parseint () will automatically ignore non-numeric parts.
Parseint () only takes the integer part, ignoring the fractional part.
Parsefloat () also obtains the fractional part.
Like tostring (), parseint is also in hexadecimal format. The default value is decimal.
If you want to use the octal format, you can write it like this: parseint ("077", 8); // output 63 = 7*8 + 7
When the character starts with 0, we must specify the second parameter, otherwise JS may convert it in octal format.

4): boolean type:
Boolean In the numeric environment: True is converted to 1, and false is converted to 0.
In the character environment, convert true to true and false to false ".
Boolean conversion:
var x_to_boolean = Boolean (x); // Method 1: Use the Boolean () function.
var x_to_boolean =! X; // Method 2: Use an exclamation point.

Related Article

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.