1.1 Converting a numeric type to a string type 1.1.1 Implicit Conversion

In JS, when the operator is in operation, if the two sides of the data are not unified, the CPU can not calculate, then the compiler will automatically convert the data on both sides of the operator into the same data type and then calculate.

This method of automatic conversion by the compiler without manual conversion by the programmer is called implicit conversion.

For example, the line of code 1 > “0” will not report an error in JS. The compiler will convert the “0” on the right side of the operator into the number 0 and then compare the size.

  • var

    n1

    =

    123

    ;

  • var

    n2

    =

    ‘123’

    ;

  • var

    n3

    =

    n1

    +

    n2;

  • //

    Implicit conversion

  • console

    .

    log

    (

    typeof

    n3

    );


1.1.2 Type Casting

Casting is a very common technique, and although it has caused many hidden bugs in the past, we should not throw the baby out with the bath water. Only by understanding how it works can we enjoy its benefits and reduce bugs.

  • var

    n1

    =

    123

    ;

  • //

    Cast String(),toString()

  • var

    str1

    =

    String(n1);

  • console.log(

    typeof

    str1);

  • var

    num

    =

    234

    ;

  • console.log(num.toString())


1.2 Converting a string type to a numeric type

  • var

    stringNum

    =

    ‘789.123 wadjhkd’

    ;

  • var

    num2

    =

    Number(stringNum);

  • console.log(num2)

  • //

    ParseInt () parses a string and returns an integer

  • console.log(parseInt(stringNum))

  • console.log(parseFloat(stringNum));


1.3 Any data type can be converted to Boolean

  • var

    b1

    =

    ‘123’

    ;

  • var

    b2

    =

    0

    ;

  • var

    b3

    =

    123

  • var

    b4

    =

    Infinity;

  • var

    b5

    =

    NaN;

  • var

    b6

    ;

    //

    undefined

  • var

    b7

    =

    null

    ;

  • //

    The 0 is true

  • console

    .

    log

    (

    Boolean

    (

    b7

    ))



Go to top

2. Process control

JavaScript’s flow control syntax is very similar to Java’s, so you can read my Java Basics – Flow Control and Flow Control (continued) in more detail than this

2.1 If Statement 2.1.1 If Format

  • var

    ji

    =

    20

    ;

  • if

    (

    ji

    > =

    20

    ) {

  • console.log(

    ‘Congratulations, chicken, good luck.’

    )

  • }

  • alert(

    ‘alex’

    );

    //

    The following code will also execute


2.1.2 the if – else format

  • var

    ji

    =

    20

    ;

  • if

    (

    ji

    > =

    20

    ) {

  • console.log(

    ‘Congratulations, chicken, good luck.’

    )

  • }

    else

    {

  • console.log(

    ‘Sorry to try again next time’

    )

  • }


2.1.3 If-else If-else format

  • if

    (

    true

    ) {

  • //

    Perform operations

  • }

    else

    if

    (

    true

    ) {

  • //

    Conditional execution

  • }

    else

    if

    (

    true

    ) {

  • //

    Conditional execution

  • }

    else

    {

  • //

    Conditional execution

  • }


2.2 logic and &&, logic, or | |

Case 1:

  • //

    1. If the total score >400 and the math score >89 are recorded by Tsinghua University

  • //

    This is true only if both logic and && conditions are true

  • if

    (

    sum

    >

    400

    &&

    math

    >

    90

    ) {

  • console.log(

    ‘Tsinghua University entry success’

    )

  • }

    else

    {

  • alert(

    ‘Failed in the college entrance examination’

    )

  • }



Case 2:

  • //

    2. If your total score >400 or your English > 85, you will be admitted by Fudan University

  • //

    Logic or only holds if one condition holds

  • if

    (

    sum

    >

    500

    ||

    english

    >

    85

    ) {

  • alert(

    ‘Admitted by Fudan University’

    )

  • }

    else

    {

  • alert(

    ‘Failed the sats again’

    )

  • }


2.3 the switch

  • var

    gameScore

    =

    ‘better’

    ;

  • switch

    (gameScore){

  • //

    Case means that if a condition is met it will walk in and break out. If a break is not written in a condition, the program stops until it hits the next break

  • case

    ‘good’

    :

  • console.log(

    ‘Had a good time’

    )

  • //

    Break: Exit

  • break

    ;

  • case

    ‘better’

    :

  • console.log(

    ‘That was a hell of a game.’

    )

  • break

    ;

  • case

    ‘best’

    :

  • console.log(

    ‘Congratulations on eating chicken.’

    )

  • break

    ;

  • default

    :

  • console.log(

    ‘I’m sorry’

    )

  • }


2.4 the while loop

Cycle in three steps:

1. Initialize the loop variable

2. Determine loop conditions

3. Update loop variables

  • var

    i

    =

    1

    ;

    //

    Initialize the loop variable

  • while

    (

    i

    < =

    9

    ) {

    //

    Judgment cycle condition

  • console.log(i);

  • i

    =

    i

    +

    1

    ;

    //

    Update cycle condition

  • }





2.5 do_while

  • //

    The code in do is going to go once regardless of whether the conditions in the while are met or not

  • var

    i

    =

    3

    ;

    //

    Initialize the loop variable

  • do

    {

  • console.log(i)

  • i

    + +;

    //

    Update cycle condition

  • }

    while

    (

    i

    <

    10

    )

    //

    Judgment cycle condition


2.6 a for loop

  • for

    (

    var

    i

    =

    1

    ;

    i

    < =

    10

    ;

    i

    ++

    ) {

  • console.log(i)

  • }