JavaScript Learning Summary-Basic syntax-(ii)

Source: Internet
Author: User

2.4. Data type 2.4.1. typeof operator

There are 4 basic data types in ECMASCRIPT:

Undefined, Boolean,number, and string

Typeof

The keyword typeof can get the type of a variable.

<script type= "Text/javascript" >         var a = +;          var b = 3.14;         var C = true;         var d = ' a ';         var e = "Hello";         var F;         document.write (A + "<br/>");         document.write (b + "<br/>");         document.write (c + "<br/>");        document.write (d + "<br/>");         document.write (E + "<br/>");         document.write (f + "<br/>");         document.write ("

  

Summary of types:

All values are number type

Character and string are string types

Boolean is a Boolean type

If a variable has no initialization value, its type is undefined type. Indicates no definition.

The representation of numeric values in javascript:

<script type= "Text/javascript" >        var a =;        var b = 012; Octal        var c = 0x12;//16 Binary        document.write (A + "<br/>");        document.write (b + "<br/>");        document.write (c + "<br/>");      </script>

Usage Details:

1, octal if the data exceeds 0~7, the default resolution is decimal data.

<script type= "Text/javascript" >        var a=09;//Invalid octal         document.write (A + "<br/>");</script>

Floating point number:

To define a floating-point value, you must include a decimal point and a number after the decimal point (for example, 1.0 instead of 1). This is considered a floating-point literal. For example:

<script type= "Text/javascript" >        var a = 3.14;//floating point        var B = 1.//Legal, no number after the decimal point, resolves to 1        var c = 10.0//integer, resolves to 1 0.        document.write (A + "<br/>");        document.write (b + "<br/>");        document.write (c + "<br/>");      </script>

Because the memory space required to save the floating-point number is twice times the positive value, Ecmascrip converts the floating-point value to an integer value. If the decimal point is not followed by any number and the floating point is itself an integer, it is converted to an integer.

Convert to Digital

ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat (). These methods are called only for String types, and are returned as NaN for the other types.

parseint () and parsefloat () will parse the string carefully before judging whether the string is a numeric value.

The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns NaN, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.

For example, if you want to convert the string "12345red" to an integer, then parseint () returns 12345, because when it checks to the character R, it stops the detection process.

<script type= "Text/javascript" >         var num = parseint ("1234abc");        document.write (num); 1234      </script>

The numeric literals contained in the string are converted correctly to numbers, such as "0xA" being correctly converted to the number 10. However, the string "22.5" is converted to 22 because the decimal point is an invalid character for integers.

<script type= "Text/javascript" >         var num1 = parseint ("12345red");         Returns 12345         var num2 = parseint ("0xA");        Returns ten         var num3 = parseint ("56.9");         Returns         the $ var num4 = parseint ("red");        Returns NaN         document.write (num1 + "<br/>");         document.write (num2 + "<br/>");         document.write (num3 + "<br/>");        document.write (num4 + "<br/>");      </script>

  

<script type= "Text/javascript" >        alert (  parsefloat ("123")  );//Can        alert (  parsefloat (" 123.53 ")  ); Can        alert (  parsefloat ("012")  ),//        alert (  parsefloat ("000012")  );        Alert (  parsefloat ("0x20")  );//result is 0        alert (  parsefloat (" -59")  );//Negative        Alert (  parsefloat ("abc123")  );//NaN (not a number), indicating that it is not a digit.        Alert (  parsefloat ("123AB2C")  );//The result is 123, which resolves the preceding correct number until an incorrect number is encountered.      </script>

  

Determine if it is a valid value.

IsNaN

IsNaN (expression), whether the returned result is Nan (non-numeric)

<script type= "Text/javascript" >         document.write (IsNaN ("abc") + "<br/>");//true         document.write (IsNaN ("123") + "<br/>"); False      </script>
2.5. Operator (operator) 2.5.1. Arithmetic

Addition operator

The addition operator is represented by a plus sign (+):

The addition of ECMAScript also has some special behaviors:

One of the operands is Nan, and the result is Nan.

string concatenation

If the two operands are all strings, concatenate the second string to the first one.

If only one operand is a string, the other operand is converted to a string, and the result is a string of two strings concatenated into it.

For example:

var result = 5 + 5; Two digits

alert (result); Output "10"

var result2 = 5 + "5"; A number and a string

alert (result); Output "55"

Attention:

var a = 5;var B = True;alert (A + b);//6

Note: Variables are boolean type when true=1, false=0;

var a = 5;var B = False;alert (A + b);//5

Multiplication operator

The multiplication operator is represented by an asterisk (*) and is used to multiply two numbers.

The multiplication syntax in ECMAScript is the same as in the Java language:

Note: If the operand is a number, perform a regular multiplication, that is, two positive or two negative numbers are positive, two operands are different, and the result is negative.

Division operator

The division operator is represented by a slash (/), with the second operand in addition to the first operand:

var iresult = 88/11;

Attention:

var a = 10;var B = 3;alert (A/b); 3.3333333333333335
2.5.2. Comparison

Comparison operators that are less than, greater than, less than or equal to, and greater than or equal to two numbers are performed

var bResult1 = 2 > 1//true

var bResult2 = 2 < 1//false

Comparing numbers and strings

var bresult = "+" < "3";

alert (bresult); Output "true"

The above code compares the strings "25" and "3". Two strings compare the character order of the two strings when compared to each other.

Note: If the string is compared, the string is converted to a number and then compared.

If you change an operand to a number, the result will be different:

var bresult = "+" < 3;

alert (bresult); Output "false"

Here, the string "25" will be converted to the number 25, and then compared with the number 3, the results are expected.

Whenever a number and a string are compared, ECMAScript converts the string to a number and then compares them in numerical order.

2.5.3. Logical operators

&& and

|| Or

! Non -

Logical AND Operator

In ECMAScript, the logical AND operator is represented by the double ampersand (&&):

var btrue = True;var Bfalse = False;var bresult = btrue && bfalse;

Similar to the logical AND operation in Java, the logical AND operation in ECMAScript is also a simple operation, that is, if the first operand determines the result, the second operand is no longer counted. For a logical AND operation, if the first operand is false, the result cannot be equal to true regardless of the value of the second operand.

Consider the following example: var btrue = True;var Bresult = (btrue && bunknown);   An error alert (bresult) occurred;                This line does not execute this code when a logical and operation throws an error because the variable bunknown is undefined. The value of the variable btrue is true because the logical AND operation will continue to evaluate the variable Bunknown. Doing so throws an error because the value of bunknown is undefined and cannot be used for logical and operations.

Logical OR operator

The logical OR operator in ECMAScript is the same as in Java, and is made up of double vertical bars (| | ) indicates

var btrue = True;var Bfalse = False;var bresult = Btrue | | Bfalse;

Like the logical AND operator, a logical OR operation is a simple operation. For a logical OR operator, if the first operand is true, the second operand is no longer evaluated.

Logical operator not.

In ECMAScript, the logical NOT operator is the same as the logical not operator in Java, denoted by an exclamation point (!).

Typically, this operator is used to control loops: var bfound = False;var i = 0; while (!bfound) {  if (avalue[i] = = vsearchvalues) {    bfound = true;  } else {    i++;  }}
2.5.4. Assignment operators

The simple assignment operator is implemented by an equal sign (=), but assigns the value to the right of the equal sign to the variable to the left of the equals sign.

For example:

var iNum = 10;

  

A compound assignment operation is implemented by adding an equal sign (=) to a multiplicative operator, an additive operator, or a displacement operator. These assignment operators are abbreviated as the following common cases:

var iNum = 10;inum = INum + 10;

  

You can overwrite the second line of code with a compound assignment operator:

var INum = 10;inum + = 10;

  

There are compound assignment operators for each of the major arithmetic operations and several other operations:

    Multiplication/Assignment (*=)    Division/Assignment (/=)    modulus/Assignment (%=) addition/assignment (    + =)    subtraction/Assignment (-=)

  

2.5.5. Three Mesh

The ECMAScript operator is the most functional operator in the same form as in Java.

  

The expression is based on conditionally assigning a value to a variable according to the results of the boolean_expression calculation. If Boolean_expression is true, the True_value is assigned to the variable, and if it is false, the False_value is assigned to the variable.

For example:

  

In this example, IMax will be given the maximum value in the number. Expression declaration if INUM1 is greater than iNum2, the INUM1 is given to IMax. But if the expression is false (that is, iNum2 is greater than or equal to INUM1), the iNum2 is given to IMax.

  

JavaScript Learning Summary-Basic syntax-(ii)

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.