Introduction to JS Basics

Source: Internet
Author: User

JS Introduction

Netscape developed the original JavaScript language, and a year later Microsoft emulated JavaScript to develop JScript, and in order to make JavaScript a global standard, several companies joined the ECMA (European computer Manufacturers Association) organizes custom JavaScript language standards, known as ECMAScript standards . "ECMA pronunciation: Ek ma, Aikma"

ECMAScript does not specify the language execution of the host environment, it only prescribes the grammar, such as types, statements, keywords, operators, reserved words, objects and other purely language-level things. JavaScript not only implements this standard, but also determines the hosting environment for execution as a Web browser, extending support for the DOM and BOM.

But most of the time, we still use the word JavaScript. If you encounter the word ECMAScript, simply replace it with JavaScript, because in the language that implements the ECMAScript standard, only JS is most famous.

JavaScript, which is a scripting language that can be run by a Web browser, cannot run independently from the browser. With the advent of node. js and popular, JS can also run on the server side, do background development, JS becomes more powerful.

0

ECMAScript standard is the basis of JS, is his core, and Dom and BOM is to let JS in the Web front-end development popular 2 big weapon.

The variables in JS

JS is a weak type, the variable is just a reference value/object name tag, the variable itself does not save type information. That is to say, a variable can be given any type of value at any time.

Define variables in JS using the keyword var. Variables defined with var are local variables in the function (the use of global variables is discouraged in JS, so forget about them).

function foo () {     var = +;      var name= "Luke Long";      = +;    // OK, no problem, but it's not encouraged    }

The data type in JS

There are only these types in JS: boolean, Number, String, object. There are also 2 built-in identifiers: null and undefined, which are built-in identifiers for the special values of the type.

undefined: represents a value for a variable that is defined

1, a var declaration of the variable if not initialized, then it gets the value undefined.

2. If there is no return value for the function, the return is undefined.

null: a reference to an empty object, and Null in Java is a meaning.

Null is a special value for the object type, which means that the variable does not refer to any valid object.

Boolean : boolean value, 2 built-in value, true false

The Boolean value in JS is not as strict as Java (the conditional statement can only be a Boolean expression), and any type in JS can be used as a Boolean value, and any value can be implicitly or explicitly converted to a Boolean type.

Type The case of false A condition that is true
Number 0,0.0 and Nan Other values (including positive and negative infinity)
String Empty string Non-empty string
Object Null Any non-empty object
Undefined Undefined must be false. /

JS can be automatically converted implicitly, you can also use the function Boolean explicit conversion.

function foo () {    var name= ';     if (name) {        console.log (name);    } Else {        console.log ("name is unknown");     // name is unknown     }       if(!  Boolean (name)) {         = "Luke Long";    }    Console.log ("Now name is" + name);      // Now name is Luke Long }

Number

All numbers are number types, unlike Java, which distinguishes int, long, float ...

JS numbers, internally, are stored with IEEE754 64-bit floating-point number, even if it is an integer. In a calculation in which some operators have only integers to participate in, JS converts the corresponding number to a 32-bit integer to participate in the calculation. So even if the actual storage is a double-precision floating-point number, the numbers in JS give developers an illusion of distinguishing between decimals and integers.

because of the language design flaws, JS number type has a lot of pits , need to use a large space to explain, here is unknown, at least prompt attention.

String

1, JS does not have char, are string.

2, utf-16 as internal character encoding. Therefore, a character is a byte and can be supported in Chinese.

3, the string can use single quotation marks, can also be double quotation marks.

4, the string object is immutable. The value of the string is immutable in memory, and changing the value of a string is simply that the variable references another new memory string data, and does not change the string in-memory value in situ.

var name = "Hello";

name = "Hello World";

Length
toUpperCase ()
toLowerCase ()

5. Methods for converting other types to strings.

Object

The object in JS is actually a set of data (the properties of the object) and the function (the object's methods).

1, like Java, the object type is the basis of all types in JS. The object class has the following methods

Constructor
hasOwnProperty (PropertyName) Determines whether the specified property is stored in the current instance (rather than in the prototype of the instance). The property name is given as a string
isPrototypeOf (object)
propertyIsEnumerable (PropertyName) Determines whether a given property can be enumerated with a for-in statement, and the property name is given as a string.
toLocaleString () Returns the local string representation of an object
ToString () Returns the string representation of an object
ValueOf ()

2. Methods for creating objects

(a) New Object ()

(b)

JS operator

typeof

New

++    - -

Operator type Operator Note
Arithmetic operations

++  --

+    -    *    /  %

Relationship

==

!=

>=

<=

>

<

===

!==

can be used for 2 string comparisons, compared to their Unicode character encodings.

Recommendation: Use only to compare 2 numbers.

Logic

&& (with)

|| Or

! (non)

Using short-circuit evaluation rules
Assign value

=

+=

-=

*=

/=

%=

<<=

>>=

>>>=

Supports compound assignment

var a = 1;

a+=2;

Ternary operators

Bool_exp?val1:val2

Returns VAL1 if the add expression is true, otherwise returns VAL2
-bit operation

>>

>>>

<<

Introduction to JS Basics

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.