Simple JavaScript getting started tutorial (1): basic, simple javascript

Source: Internet
Author: User
Tags hasownproperty

Simple JavaScript getting started tutorial (1): basic, simple javascript

This article requires programming experience in other languages.

Before learning

Most programming languages have good and bad parts. This article only describes the good part of JavaScript, because:

1. Only the good part can shorten the learning time
2. The code is more robust.
3. The code is easier to read.
4. easier maintenance of written code

Weak type and strong type

Generally, the sooner an error is fixed, the less costly it will be. The compiler of a strong language can check certain errors during compilation. While JavaScript is a weak type language, its interpreter cannot check for type errors, but the practice shows:

1. errors that can be avoided by a strong type are not critical errors.
2. Weak types can bring flexibility and do not need to carry a heavy burden.

JavaScript standards

The ECMA-262 standard defines the language ECMAScript. The JavaScript and ActionScript we are familiar with are based on ECMAScript. Currently the mainstream use of the fifth version of ECMA-262, Google's V8 engine is to achieve this.

Hello JavaScript

JavaScript is a scripting language and needs to be interpreted and executed by the interpreter. You can explain the execution of JavaScript in the browser or directly use node. js. node. js integrates Google's V8 JavaScript Engine. Because node. js is very convenient to use, here I use node. js to explain how to execute JavaScript. Now let's look at the first JavaScript program:
Copy codeThe Code is as follows:
// Test. js
Console. log ("Hello JavaScript ");

Run this program:
Copy codeThe Code is as follows:
Node test. js

Syntax

Note

JavaScript uses the same annotation method as C ++, // for single-line annotation,/***/for multi-line annotation.

Numeric type

JavaScript has only one numeric type, that is, a 64-bit floating point number. The numeric type has two special values: NaN and Infinity. NaN indicates not a number (not a number). Use the isNaN function to check whether it is NaN. The Infinity value indicates Infinity. In a Math object, there is a set of operation numbers. For example, the Math. floor method is used to perform down rounding.

String

The character string literal can be enclosed in single quotes or double quotation marks, and the Escape Character \ (unlike many other languages ). Each character in JavaScript is two bytes and uses the Unicode Character Set. The string has a length attribute:

Copy codeThe Code is as follows:
"Hello". length // The value is 5. Note that it is not "Hello". length ()

The string cannot be changed (the same as Lua). In addition to the length attribute, there are also some methods, such:

Copy codeThe Code is as follows:
'Cat'. toUpperCase () = 'cat'

Statement

The var statement is used to declare local variables. Otherwise, the variable is a global variable and the value of uninitialized variables is undefined:

Copy codeThe Code is as follows:
Function f (){
Var localVar = 123;
GlobalVar = 456;
Var I; // The value of I is undefined.
};
 
F ();
 
Console. log (globalVar); // OK
Console. log (localVar); // error. localVar is not defined

A group of statements wrapped by {} is called Block. Unlike other languages, functions in JavaScript will not create a new scope for the Block. For example:

Copy codeThe Code is as follows:
{
Var v = 123;
}
Console. log (v); // OK

If statement

Copy codeThe Code is as follows:
If (expression)
Statement

Or

Copy codeThe Code is as follows:
If (expression)
Statement1
Else
Statement2

Or

Copy codeThe Code is as follows:
If (expression1)
Statement1
Else if (expression2)
Statement2
Else if (expression3)
Statement3
Else
Statement4

The if statement determines whether to execute or skip some statements by judging whether the expression value is true or false. In JavaScript, the following values are false (other values are true ):

1. false
2. null
3. undefined
4. Empty string
5.0
6. NaN

The statement in if can be a statement or a statement block.

Switch statement

Copy codeThe Code is as follows:
Switch (n ){
Case 1: // if n is equal to 1
// Execute the code block
Break;
Case 2: // if n is equal to 2
// Execute the code block
Break;
Default: // if n is not 1, it is not 2.
// Execute the code block
Break;
}

Here, the break is used to exit the loop statement or switch statement. In JavaScript, two operators are used to compare whether two values are equal:

1. = (corresponding! = Operator), equal, two operands of different types, this operator tries to convert the operand type before comparison, for example:
Copy codeThe Code is as follows:
Var x = 1;
X = 1; // true
X = "1"; // true

2. = (corresponding! = Operator), completely equal. Compare two operands without converting the operand type. For example:
Copy codeThe Code is as follows:
Var x = 1;
X = 1; // true
X = "1"; // false

Note that NaN and any value are not equal. If x is NaN, then x! = X (only for NaN), we can implement the isNaN function as follows:

Copy codeThe Code is as follows:
Function isNaN (n ){
Return n! = N;
}

In the preceding switch statement, the if statement is:
Copy codeThe Code is as follows:
If (n = 1)
//...
Else if (n = 2)
//...
Else
//...

While and do-while statements

Copy codeThe Code is as follows:
While (expression)
Statement

If expression is true, execute statement again until expression is false.

Copy codeThe Code is as follows:
Do
Statement
While (expression );

Similar to the while loop, the statement is executed first, and then the conditional expression is checked.

For statement

Copy codeThe Code is as follows:
For (initialize; test; increment)
Statement

Initialize is executed once (usually used to initialize cyclic variables), and then test the test condition (often used to test cyclic variables). If the test condition is false, the loop is stopped; otherwise, statement is executed, then execute the increment (often used to update the cyclic variable), and then perform the test condition test, so that the loop is executed. Example:

Copy codeThe Code is as follows:
For (var I = 0; I <5; ++ I ){
Console. log (I );
}

Another form of for is used to enumerate all attribute names of an object:

Copy codeThe Code is as follows:
For (variable in object)
Statement

Example:
Copy codeThe Code is as follows:
Var obj = {
A: 1,
B: 2,
C: 3
};
 
For (var name in obj)
Console. log (name );

It should be noted that we use the hasOwnProperty method to check whether the property name belongs to this object or from prototype chain (prototype chain, prototype will be described in the next article:

Copy codeThe Code is as follows:
For (var in obj ){
If (obj. hasOwnProperty (var )){
//...
}
}

Return Statement

The return statement is used to let the function return a value. If the function does not explicitly use return, undefined is returned:
Copy codeThe Code is as follows:
Function f (){}
Var v = f (); // v = undefined

? : Conditional operator (the only ternary operator in JavaScript)
? : Conditional operators exist in many programming languages. When the first operand is true, the operator returns the value of the second operand. Otherwise, the value of the third operand is returned. Example:

Copy codeThe Code is as follows:
Function abs (){
Return x> 0? X:-x;
}

Typeof Operator

The typeof operator is used to obtain the type of a variable. Its return values include:

1. 'number'
2. 'string'
3. 'boolean'
4. 'undefined'
5. 'function'
6. 'object'

Special typeof null returns 'object '. Example of typeof:

Copy codeThe Code is as follows:
Var a = typeof 'hello'; // a = 'string'
Var B = typeof null; // B = 'object'

+ Operator

+ Operators can be used in addition operations in JavaScript or for String concatenation:
Copy codeThe Code is as follows:
Var message = 'hello' + 'World'; // message = 'helloworld'

& | Operator

& Operator returns the value of the first operand when the first operand is false. Otherwise, the value of the second operand is returned.
| Returns the value of the first operand when the first operand is true. Otherwise, the value of the second operand is returned.

Copy codeThe Code is as follows:
Var a = 1 & true; // a = true
Var B = 1 | false; // B = 1

| Usage:
Copy codeThe Code is as follows:
Name = name | 'unknown '; // set the default value for name to 'unknown'


Who can give me a comparison of those books after learning the java framework?

Aren't you going to access the Internet?
Use Google or Baidu.

H: hibernate (haibernett)

I am a freshman. I just finished learning a book "java programming language" (basic). I want to learn more about algorithms. Can you help me to recommend a book?

I strongly recommend that you do not pursue any books recommended by Daniel. Now, when laying the foundation, you need to understand that you can find a book in the library, and then read through it, in fact, the "java programming language" (basic) you mentioned is good. You can do all the above programs yourself ??? Do you know why the main function is so written? Do you know why I ++ and I are different ?? It's all basic. Come on! The Foundation is Wang Dao ,,,,

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.