JavaScript Basics (data types and conversions, operators)

Source: Internet
Author: User
Tags logical operators

9 , data type

Concept: Represents the classification of data currently stored (representing numbers-integers and decimals)

U primitive Type (original value)-----"typeof operator: Determine the original type of the variable"

*number (number): Indicates the number var num1 = 10.123; Console.log (NUM1);

* NaN (not a number): All called not a numbers

*boolean (Boolean): only True/false value var boo = true; Console.log (Boo);

*undefined: There is only one value undefined [variable defined only, no assignment] variable is assigned a value of undefined

*null function: Resource release var n2=null;

U reference type-----"instanceof Operator: Judging the reference type of a variable"

* Number:new ( numeric ) var num1 = new (200);

* String: new String ( string ) var str2 = new string (' Hello ');

* Boolean:new Boolean (True/false) var Boo3 = new Boolean (true);

Ten , type conversion

U-Implicit type conversion

    • ? Convert to String type "num + ' boo + '" format: var result1 = num + '; Result: ' Number ' text '
    • ? Convert to numeric Type "+str +boo" format: var result2 = +str;

* Boolean type: True = 1; False = 0

* String type:' number ' = = number; ' text content ' = ' NaN '

Attached Nan:

Console.log (IsNaN (10)); Output False (10 is a numeric value)

Console.log (IsNaN ("10")); Output False (can be converted to a value of 10)

Console.log (IsNaN ("Blue")); Output true (cannot be converted to numeric value)

Console.log (IsNaN (true)); Output False (can be converted to a value of 1)

    • Convert to Boolean type "!! Num!! STR"format: var RESULT3 =!! Num

* Number type: 0, NaN = false; Not 0 = true

* String type: empty string = false; Non-empty string = = true

*undefined:= false;

*null:= false

"Return undefined":

Access to unmodified variable undefined;   Accessing properties that do not exist; Any variable that is set to the undefined value

There is no implicit function that defines a return expression, and the return expression does not explicitly return anything

U-Explicit type conversion

(1) How to construct a function

2 number ()

var result1 = number (str); var result2 = number (boo);

2 String ()

var result1 = String (NUM1); var result2 = String (Boo2);

2 Boolean ()

var result1 = Boolean (NUM1); var result2 = Boolean (STR2);

(2) Conversion method

U toString () -Convert to String

var result1 = num1.tostring (); var result2 = boo2.tostring ();

U parseint () -an integer converted to a number

var result1 = parseint (STR3); var result2 = parseint (Boo4);

* The string starting with the number = the number in front. * A string that starts with a non-digit = NaN.

U parsefloat () -Convert to a decimal number

var result3 = parsefloat (STR5); var result4 = parsefloat (BOO6);

Attached: Convert a string containing numbers and characters [parseint () or parsefloat () method]

Eg:var str = ' 10str ';

var result1 = parseint (str);  Ten var result2 = parsefloat (str); 10

A , operator

U arithmetic operator

2 Number [+,-,*,/,%]

Note:% remainder, a% B = The resulting integer part, and the sign depends on the positive or negative of a

2 string + number ==> "string + string" type result: '

2 String + boolean ==> "string + string" type result: '

Note: The plus sign-not an addition calculation, but a connector that represents a string (a string type-a puzzle appears)

★ A numeric type and a non-numeric string subtraction, resulting in a Nan Eg:console.log (' Hi '-3); NaN

★ A numeric type and a non-numeric string to divide, to get the Nan

2 Boolean + number ==> "number + number" type for addition calculation

2 self-increment or decrement operator

The increment operator (+ +)---increments by 1 each time, and the decrement operator (--)---decreases by 1

Pre-built: The increment operator precedes the operand. Add 1 First, then assign value.

Post-type: The self-increment operator is located after the operand. Assign value first, plus 1.

U comparison Operators

== ! = Unequal > >= < <=

Different types of comparison operators:

= =!== values are the same, the types are different; Compare numeric results only: values are the same, true

=== (congruent) Type, value is the same result: true

!== (Not congruent) Type, value has a different result: true; type, value is not the same result: false;

* Comparison of Nan values (Nan and itself): Console.log (nan = = nan); False

* IsNaN () Method: Determine if the specified variable is not a number Eg:var result = No. (' String ');//nan

Console.log (IsNaN (Result));//true

U logical operators

&& (after the symbol is really taken; pre-fetch) | | or (the symbol is really taken before; fake take) ! Non-

Short Circuit principle:

&&--&& Front is false, the result will return && previous value, before true, the result will return && subsequent values.

|| --- || False before, the result will return | | The following value, preceded by true, returns the result | | The preceding value.

U assignment operator

A + = B is a = a + B; A-= B is a = a - b; a *= b is a = /c7>a * B;

A /= b is a = a / b; a %= b is a = a % B; Note: Left efficiency > right side effect Rate

U string Join operator

numeric + digital ==> addition calculation; numeric + string ==> string concatenation calculation

* Operator naming method:

Unary operators: operation of only one variable; binary operator: manipulating two variables; ternary operator: manipulating three variables

U condition (Ternary) Operator:

Format: An expression ? Results 1 : Results 2 ;

The result is true, execution result 1, result is false, execution result 2.

Allow nesting, but with too many levels of nesting, performance degrades and readability is reduced

JavaScript Basics (data types and conversions, operators)

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.