JavaScript Basics-day1

Source: Internet
Author: User
Tags arithmetic operators logical operators alphanumeric characters

JavaScript introduction

You don't know what it is to learn? This is a web-embedded scripting language ... Just that.

JavaScript composition

A complete JavaScript implementation is made up of the following 3 different parts:

    • Core (ECMAScript)
    • Document Object Model (DOM) Documents object models (consolidated js,css,html)
    • Browser object models (BOM) Broswer object Model (integrated JS and browser)

The vast majority of Javascript is object-based and object-oriented in development.

ECMAScript describes the following:

    • Grammar
    • Type
    • Statement
    • Key words
    • Reserved words
    • Operator
    • Object (encapsulates inherited polymorphism) based on the language of the object. Working with objects

How JavaScript is introduced

// write directly, header inside <script>    alert (' Hello World ')</script>//  Import file <script src= "Hello.js" ></script>

Variable

Declaring variables using the var keyword does not require a variable type to be specified

// Normal Way var i;    // declaring variable i=10;    // assigning values to variables // Shorthand Method var j=20;    // declaring variables and assigning values

A row can declare multiple variables, which can be of different types

var name= "Bob", Age=20, job= "teacher";

If Var is not used when declaring a variable, then the variable is a global variable

The name of the variable, the first character can only be letters, underscores, $ US dollar symbol Three select one, the remaining characters can be underscores, dollar signs or any alphanumeric characters and case-sensitive, x and X are two variables the first letter of the camel is lowercase, and the following letters begin with uppercase characters.  For example:var mytestvalue = 0, Mysecondvalue = "Hi" = 0, Mysecondvalue = "Hi" = 0, Smysecondvalue = "HI";
Naming Conventions

Constant

Constants are data values that appear directly in the program, such as Pi 3.1415926, which do not change easily.

Identifier

Consists of letters, numbers, underscores (_), Dollar signs ($) that do not begin with a number, and are often used to denote names of functions, variables, and so on, such as: _abc, $ABC, abc,abc123 is an identifier, and 1ABC is not

Words that represent a particular meaning in the JavaScript language are called reserved words and do not allow programs to be redefined as identifiers

Data type

JavaScript is a weakly typed language. Strongly typed: The variable /object must be of a type, and once the type of the variable/object is determined, its type is no longer allowed to change, and the variables/objects of different types do not allow assignment and initialization. Weak type: variable /object type concept is weak or no type concept, different variable/object types can be changed, different types of variables/objects allow assignment and initialization of values.  static Type: Type checking occurs at compile time. When compiling or interpreting, you know the type of each variable, because the type is wrong and what you can't do is a syntax error. Therefore, when programming in a static language, you must define the type of the variable/object in advance so that the compiler or interpreter will know the specific type of each entity at compile time.   dynamic Type: Type checking occurs at run time. When compiling, you don't know the type of each variable, because the type is wrong and what you can't do is a run-time error. Therefore, when programming in a dynamic language , you do not have to define the type of the variable/object beforehand, and the language will automatically write down its type when you assign a value to the variable/object for the first time.
language Type 1

The ' 12 ' and the integer 3 are concatenated to get the string ' 123 ', which can then be treated as an integer 123 , all of which do not require any display conversions. So Python is both a dynamic type language (because it doesn't use a display data type declaration) and a strongly typed language (because it's actually the same type as long as a variable gets a data type). 
Language Type 2

number (value)
    • No distinction between integral and floating-point values
    • All numbers are stored in 64-bit floating-point format, equivalent to the double format in the Java and C languages
    • The maximum value that can be represented is ±1.7976931348623157 x 10308
    • The minimum value that can be expressed is ±5 x 10-324

Integer:
10 integers in JavaScript consist of sequences of numbers
What is the exact range of the expression?-9007199254740992 (-253) to 9007199254740992 (253)
Integer out of range, accuracy will be affected
Floating point number:
Using decimal points to record data
Example: 3.4,5.6
Use Index to record data
Example: 4.3e23 = 4.3 x 1023

16 binary and 8 decimal expressions:
16 binary data before adding 0x, octal front plus 0;16 is composed of 16 characters, such as 0-9,a-f, 8 binary number consists of 0-7 8 digits

2 binary: 1111 0011 1101 0100   <-----> 16 binary: 0xf3d4 <-----> 10 binary: 624201 111 001 111 010 <----- ; 8 binary: 0171724
16 binary and 8 binary and 2 binary conversion

String (String): Focus on the following string object

is a sequence of Unicode characters, numbers, and punctuation marks.

String constants are surrounded by either single or double quotation marks.

There are no character types in JavaScript.

The expression of a common special character in a string.

Some special characters in a string must be accompanied by a right dash \.

Common escape characters \ n: newline \ ': single quote \ ': double quote \ \: Right Dash

Boolean (Boolean value)

The Boolean type has only two values: True and False, also representing 1 and 0, in the actual operation True=1,false=0
Boolean values can also be considered on/off, yes/no, 1/0 correspondence True/false
The Boolean value is primarily used for JavaScript control statements, such as:

if (X==1) {      y=y+1;} Else {      y=y-1;      }

Undefined and null

Undefined type

The Undefined type has only one value, which is Undefined. When the declared variable is not initialized, the default value of the variable is undefined.

When the function has no definite return value, the returned value is also "undefined";

Null type

Another type with only one value is null, and it has only one private value, NULL, which is its literal. The value undefined is actually derived from the value null, so ECMAScript defines them as equal.

Although the two values are equal, they have different meanings. Undefined is a value that is assigned to a variable when it is declared but not initialized, and null is used to represent an object that does not already exist (this is briefly described when discussing the typeof operator). If the function or method is to return an object, the object returned is usually null when it is not found.

Operator

arithmetic operators:     +   -    *    /     %       + +-        - comparison operator    :>   >=   <    <=!    =    = =    = = = = =   !== logical operator     :&&   | |    ! Assignment operator:    = = = =  *=   /= string operator:    +  join, either side operand has one or two is a string do join operation

Note: Symbols not mentioned below are the same as Python

Arithmetic operators

Note 1: Self-added self-reduction

If x=2, then the value of the X + + expression after the execution of the 3,x--expression is 1;i++ equivalent to i=i+1,i--equivalent to i=i-1;
Increment and decrement operators can be placed before variables or after variables:----

var i=10; Console.log (i+ +); Console.log (i); Console.log (+ +i); Console.log (i); Console.log (i--); Console.log (I.);

i++ and ++i are different, when there are assignment operations, such as ret=i++ and Ret=++1, the former represents the first assignment and then operation, that is, the RET value of 10,i is 11, the latter represents the first operation and then assignment, that is, the RET value of 11,i is 11.

NOTE 2: Unit operations

-In addition to representing a minus sign, you can also indicate a minus sign such as: x=-y

+ In addition to a connection that can be used to represent an addition operation for a string, for example: "abc" + "def" = "abcdef"

Note 3:nan

The Nan value belongs to the number type: When a string is encountered that is not valid, a Nan data is obtained

var s= "Yuan"; var ret2=+S;console.log (Ret2); Console.log (typeof Ret2)

Characteristics

var n=nan;alert (n>3), alert (n<3), alert (n==3), alert (n= =  NaN); alert (n!=nan); // all of the operations that Nan participates in are false, except! =

Comparison operators

When using a control statement:

if (2>1) {       //  3  0  false null undefined []    console.log ("condition set!") )}

equals and non-equal operators are all equal and non-full equals. The two operators do the same as equals and non-equals, except that they do not perform type conversions until they are checked for equality.

Console.log (2==2), Console.log (2== "2"), Console.log (2=== "2"); Console.log (2!== "2") ;

Note 1:

varBresult = "Blue" < "Alpha"; alert (bresult);//Output TrueIn the example above, the string "Blue" is less than "alpha" because the character code of the letter B is 66, and the character code for the letter A is 97.. Comparing numbers and strings another tricky situation occurs when you compare numbers in the form of two strings, such as:varBresult = "3" < "; alert (bresult);//output "true"The above code compares the strings "25" and "3". Two operands are strings, so the comparison is their character code ("2" character code is 50, "3" character code is 51). However, if one of the operands is a number, the result is interesting:varBresult = "3" <; alert (bresult);//output "false"here, the string"25" will be converted to the number 25, then with the number 3compared, the results were expected. Summary: Comparison operators on both sides if one is a numeric type and one is a different type, its type is converted to a numeric type. If both sides of the comparison operator are of type string, the ASC code of the highest bit is compared, and if the highest bit is equal, continue to take the second bit comparison.
comparison Operators

NOTE 2:

equality operators: The rules for performing type conversions are as follows: If an operand is a Boolean value, convert it to a numeric value before checking for equality.  false to 0,true to 1nullnullfalse  Truetrue

logical operators

if (2>1 && []) {    Console.log ("condition and")}//  think back content? Console.log (1 && 3);    // 3console.log (0 && 3);    // 0console.log (0 | | 3);    // 3console.log (2 | | 3);    // 2

Process Control

    • Sequential structure (executed from top to bottom)
    • Branching structure
    • Loop structure

Sequential structure

<script>    console.log ("Monday");    Console.log ("Tuesday");    Console.log ("Wednesday"); </script>

Conditional branching structure

If...else ... Single branch structure

if (expression) {   statement 1;   ......   
Else { Statement 2; ..... } // function Description: Execute statement 1 if the value of the expression is true, otherwise execute statement 2

If...else If...else ... Multi-branch structure

if (expression 1) {    statement 1;} Else if (expression 2) {    statement 2;} Else if (expression 3) {    statement 3;} Else {    statement 4;}

Example:

var num=67; if (num>90) {    alert ("excellent")}Elseif (num>80 ) {    alert ("benign" )}Elseif (num>60) {    alert ("Pass")}Else {    alert ("failed")}

Switch-case Multi-branch structure

Switch (expression) {    case value 1: statement 1;  break;      Case value 2: statement 2;  Break ;      Case Value 3: statement 3;  Break ;     default : statement 4;}

Example:

Switch(x) { Case1:y= "Monday"; Break; Case2:y= "Tuesday"; Break; Case3:y= "Wednesday"; Break; Case4:y= "Thursday"; Break; Case5:y= "Friday"; Break; Case6:y= "Saturday"; Break; Case7:y= "Sunday"; Break;default: y= "Not defined";}

Switch is more concise and clear than the else if structure, making the program more readable and more efficient.

Loop structure

For loop structure (recommended)

 for (initial expression; conditional expression; self-increment or decrement) {        EXECUTE statement ...        } // function Description: Implement the condition loop, when the condition is established, EXECUTE statement 1, otherwise jump out of the loop body

For-Loop iterative

 for inch array or object)    {        EXECUTE statement ...    }

Example:

 for (var i=0;i<10;i++) {    console.log (i);}

While loop

 while (condition) {    statement 1; ...    } // function Description: Run function and for similar, when the condition is formed loop execution statement curly braces {} Inside the statement, otherwise jump out of the loop; also support continue and break statements

Example:

var count=0; var sum=0;  while (count<101) {    sum+ =count;    Count+ +;} Console.log (sum);

exception handling

Try {    // This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running }catch  (e) {     // If an exception is thrown in a try code block, the code in the catch code block is executed.     //e is a local variable that points to the error object or other thrown object }finally  {      // the finally code block is always executed, regardless of whether the code in the try is thrown abnormally (even if there is a return statement in the try code block).  }// Note: Unsolicited exception throw error (' xxxx ')

JavaScript Basics-day1

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.