How to get started with Javascript: First Javascript Basics

Source: Internet
Author: User
Tags array definition
This is the note I have read about DOM programming art, and I understand JavaScript and javascript authoritative guide 5. I am not qualified and cannot write Advanced articles. If you think the notes are not well written, you can leave it alone. This article is a waste of minutes. This is the note I have read about DOM programming art, and I understand JavaScript and javascript authoritative guide 5. I am not qualified and cannot write Advanced articles. If you think the notes are not well written, you can leave it alone. This article is a waste of minutes.

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,
(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 all the lower-case letters when writing programs.
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: The object contains a special object called function. (It is an object that can execute code .)

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 from 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 end. 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: convert true to 1 and false to 0.
In the character environment, convert true to true, and convert 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.

5): Function Definition:
Method 1: General Definition

function square(x){       return x*x; }

Method 2: Define the function quantity directly

Var square = function (x) {return x * x;} // recommended


Method 3: construct parameters

Var square = new Function ("x", "return x * x;"); // Low Efficiency



6): object:
If there is an object named cssrain, it has a height attribute.
So we can reference it like this:

cssrain.height;


You can also use the associated array definition: cssrain ["height"];

Creation object:
Method 1:

var point = new Object(); point.x = 3; point.y = 5;

Method 2: Use direct object quantity
Var point = {x: 3, y: 5}
Of course, json can also be used.

The toString () method is called when the object is in a character environment.
In the digital environment, the valueOf () method is called.
In a Boolean environment, the non-null object is true;

7): array:
Regular array: Use a non-negative integer as the subscript. Image [0]
Join array: uses the character as the subscript. For example, image ["width"]
Js does not support multi-dimensional arrays, but Arrays can be nested.

Create an array:
Method 1:

Var a = new Array (); a [0] = "1"; a [1] = 2; a [2] = {x: 1, y: 3 }; method 2: var a = new Array ("1", 2, {x: 1, y: 3 });

NOTE: If only one parameter is passed, for example, var a = new Array (3 );
It indicates the new array of three undefined elements.
Method 3: use an array
Var a = ["1", 2, {x: 1, y: 3}]; // note that the parentheses are not curly brackets.

8): null and undefined:
Null indicates no value;
Undefined: Use a variable that is not declared, or use a declared variable but is not assigned a value, or use an attribute that does not exist.
Undefined = null
To distinguish:
You can use the = or typeof operator.

9. New users often encounter the following problems:

var s =”you are right”; var b = s.substring(s.lastIndexOf(“ ”)-1 , s.length);

Q: Is s an object or a string? Why is there a method for a string?
A: s is a string. The method is available because the string type has a corresponding object class (String ).
The same Number and Boolean have the corresponding Number and Boolean class.
Js will encapsulate the object internally. The String object replaces the original String.

Summary:
This section briefly introduces some concepts (lexical structures) and data types (parts) in JavaScript ).
Now, let's talk about it today. We will continue tomorrow. ^_^.

I have already read the notes for some group members, and have good comments and bad comments. Therefore, I solemnly declare that:
This is the note I took when I read the DOM programming art and understood JavaScript and javascript's authoritative guide 5,
I am not qualified to write profound articles,
If you don't think your notes are well written, you can leave it alone. This article is a waste of minutes.

The above is the first Javascript basics. For more information, see the PHP Chinese website (www.php1.cn )!

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.