js cross-platform scripting language (preview)

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Learning, preview first, and then review can achieve twice the result with half the effort!


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is js?

A cross-platform scripting language

Platform: It is the operating environment, which generally refers to the operating system.

Cross-platform: It can run in various environments.

Features of scripting language: it cannot run independently, but depends on web pages

The operation of HTML web pages is inseparable from the operation of browser JS programs cannot be separated from HTML web pages

The use of basic syntax
script tags

Attributes:

type = 'text, javascript' declares the text format written in the current tag

src = 'demo.js' Import external .js files
Note: There can be multiple script tags, and multiple script tags are executed sequentially from top to bottom

​ A script tag can only do one thing

alert() pops up an alert box on the current page and displays the content to be displayed in the alert box

output method

document.write() outputs content on the current page

​ Note: If the content contains tags, it will be automatically parsed

console.log() outputs content on the console

escape character:

​ <; stands for < >; stands for >

Constants and Variables
Constants

Definition: A value that cannot be changed is called a constant

type of data:

​ Basic data types:

​ numbers

Type: number

​ Example: 100 -20

​ boolean value

Type: boolean

​ Example: true false

​string

​ Type: Everything with single or double quotes is called a string. (Single quotes/double quotes have the same effect and must appear in pairs)

​ Example: 'hello' "world"

​ Special data types:

​ null 空 undefined NaN(not a number)

The data type of NaN is a number type, that is, the result of typeof(NaN) is number

​ NaN is not equal to any number, including itself, NaN == NaN returns false

variable

Definition: A value whose value can be modified is called a variable

Memory:

1. Declare variables (must be declared before they can be used)

​ Keywords (words with special functions requisitioned by the system are called keywords): var declares variables

2. Initialization: When declaring a variable, directly assigning a value to the variable is called initialization.

3. If the variable is not copied when declaring the variable, the system defaults to undefined

Variable Naming and Weak
References Identifiers

Definition: All user-defined names

Variable names are also identifiers

Naming rules:

Only numbers, letters, underscores and dollar signs ($)
The first letter cannot be a number
Cannot be named with reserved words and keywords
Case-sensitive As
the name implies (use the full English name as much as possible) Two camel-case names
with more words
className
underlined class_name
variable: weak reference type. What type is assigned to is what type

keyword typeof

Format: typeof constant/variable

Function: Output the data type of the current constant or variable

The difference between parseInt, parseFloat, and Number All
three are functions that are forced to convert to numbers, but in some specific scenarios, there are differences. If they are converted to strings of boolean values, undefined, null, and pure integers, they are the same If the string of pure numbers with a decimal point is converted, the result of parseInt is only an integer, and parseFloat and Number are decimals. If there are other characters in the front of the conversion, parseInt and ParseFloat will be converted to numbers, but Number is NaN

Answer analysis: All three are functions that are forced to convert to numbers, but in some specific scenarios, there are differences. If the conversion is a string of boolean values, undefined, null, and pure integers, they are the same. A string of pure numbers with a decimal point. The result of parseInt is only an integer. ParseFloat and Number are decimals. If the number in front of the conversion is followed by other characters, parseInt and ParseFloat will be converted to numbers, but Number is NaN

Hexadecimal Conversion
Decimal to Binary

Modulo 2 remainder, fixed process

arithmetic operator
operator

1. Arithmetic operators

+, -, *, / , % (remainder symbol)

2. Relational operators

> < <= >= == != === !==
1
Note: value, definitely boolean

Automatic data type conversion:

Like other operators, the following rules apply when relational operators operate on non-numeric values:

If both operands are numeric values, compare the numeric values. If
both operands are strings, compare the encodings of the two strings corresponding to the strings. If
one of the two operands is a numeric value, convert the other into a numeric value. Numerical comparisons
In comparisons of equality and inequality, if the operands are non-numeric values, the following rules apply:

If one operand is a Boolean value, it is converted to a value before comparison, false is converted to 0, and true is converted to 1;
if an operand is a Boolean value, it is converted to a value before comparison;
one operand is NaN , then == returns false, ! = returns true, and NaN is not equal to itself;
=== identity, the number and data type must be equal, otherwise return false
3. Logical operators

&& (and operation)

Format: expression1 && expression2

Law: The entire expression is true only if both expressions are true

Short-circuit operation: When expression 1 is false, expression 2 will not be executed, and the result will be false directly.

|| (or operation)

Format: expression1 || expression2

Law: Only when both expressions evaluate to false, the entire expression evaluates to false

Short-circuit operation: When expression 1 is true, expression 2 will not be executed, and the result will be true directly.

! (not operation)

Format:! expression

Rule: first automatically convert the value of the expression into a Boolean value, and then negate it.

Note: If it is not 0, it is true; if it is empty, it is positive

law:

The operand is an empty string, returns true;
the operand is a non-empty string, returns true;
the operand is the value 0, returns true;
the operand is any non-zero value (including Infinity), false;
the operand NaN, Return true;
the operand is undefined, return true;
4. Unary operator

++ –

++a value: ++ postposition, first take the value of a as the value of the a++ expression, and then perform +1 on a.

a++ value: ++ is prepended, the +1 operation is performed on a first, and then the value of a is used as the value of the ++a expression.

Function: perform +1 operation on the original variable

Example: var a = 10;

​ alert(a++ + ++a + a + ++a + a++ +a);

​ alert(10 + 12 + 12 +13 + 13 +14);

a-- value: – postfix, first take the value of a as the value of a – expression, and then perform -1 on a.

–a value: –preposition, first perform -1 operation on a, and then use the value of a as the value of –a expression.

Function: Perform -1 operation on the original variable.

Example: var a = 10;

​ alert(a-- + --a + a + --a + a-- +a);

​ alert(10 + 12 + 12 +13 + 13 +14);

5. Assignment operator

​ <1>Basic assignment operator =

​ Function: Assign the value on the right side of the equal sign to the variable on the left side of the equal sign

​ <2> Compound assignment operator += -= ... x=

​ Expression: Any combination of data and operators is called an expression.

Note: Perform arithmetic operations on different data types

Automatic data type conversion: There is no way to perform calculations between different data types, and the data is converted into the same data type for calculation.

1. One of the operands must be a string, the operator must be a + sign, and other data types are converted into strings for string splicing

PHP processing code for extracting binary images from the database
pdf

0 stars
More than 10% of resources
28KB

Download
​ 2. Any operation other than wigs on data and strings, then the strings must be converted to numbers before performing operations.

​ <1> If the string is a string of purely numeric characters, convert it to the corresponding number

<2> If the string contains characters other than numbers, convert it to NaN, NaN and any data operations are NaN.

3. In addition to adding any other data with strings, when performing arithmetic operations with numeric types, other data will be automatically converted into numbers.
1
Arithmetic operator
exercises Exercises:

<script>
        /*
            1、入职薪水为10K,每年涨幅入职薪水的5%,50年后的工资是多少?
        */
        var money = 10000;
        //money = money + money * 0.05 * 50;
        money += monry * 0.05 * 50;
        alert(money);
     /*
            2、为抵抗洪水,战士连续作战89小时,编程计算供多少天多少个小时?
        */
        var total = 89;
        var day = parseInt(total / 24);
        var hours = total % 24;
        alert("一共花了" +  day + "天" + hours + "小时");
 /*


            3. Xiao Ming wants to travel to the United States, but the temperature there is recorded in Fahrenheit.
            It requires a program that converts Fahrenheit (80 degrees) to Celsius and displays that temperature separately in Fahrenheit and Celsius.
            Tip: The conversion formula between Celsius and Fahrenheit is: Celsius=5/9.0*(Fahrenheit-32)
     

  */
        var Fahrenheit = 80;
        var centigrade = (Fahrenheit - 32) * 5 / 9.0;
        alert("摄氏度:" + centigrade + "℃")
      /*


        4. Given a three-digit number, display the first, tens, and ones of the number
       

 123
        */
        var num = 123;
        var a = num % 10;
        var b = parseInt(num / 10) % 10;
        var c = parseInt(num / 100);
        
        alert("百位是:" + c  + "十位是:" + b  + "个位是:" + a);

 </script>


Coercion data type conversion
Boolean() coerces other data types to Boolean value

​ If not 0, it is true; if it is not empty, it is true

Number() coerces other data types to numbers

Note: Only string numbers composed of purely numeric characters can be used as numbers, and all others are NAN

parseInt()

1. rounding

2. To convert other bases to decimals, a string must be passed in

var star = '110100';
alert(parseInt(star, 2))


parseFloat() number of floating points

flow control statement
sequence structure

Code is executed sequentially from top to bottom

Branch structure/selection structure/conditional structure

Single branch statement:

grammar:

 if(判断的条件){
        执行语句;(判断条件为true的时候执行)
}



Example: Determine whether a number is even or odd

var num = 8;
if(num % 2 == 0){
       alert("这个数是偶数");
   }


To perform different operations according to different conditions

Double branch statement:

grammar:

 if(判断的条件){                   
        执行语句;(判断条件为true的时候执行)
      }
      else{
          执行语句;(判断条件为false的时候执行)
      }



Example: Determine whether a number is even or odd

var num = 8;
if(num % 2 == 0){
       alert("这个数是偶数");
   }
else{
    alert("这个数是奇数");
}


step:

1. Determine what the judgment conditions are

2. According to the different results, write the corresponding execution statement

Note: Any expression can be written in if(), and the data type will be automatically converted into a Boolean value.

Example: Find the maximum of two numbers

var num1 = 10,num2 = 20;
    if(num1 > num2){
        alert("最大数是num1:" + num1);
    }
    else{
        alert("最大数是num2:" + num2);
    }

Example: To determine whether a year is a leap year or a normal year;

Note: It can be divisible by 4 and cannot be divisible by 100, or it can be divisible by 400;

var year = 2014;
    if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
        alert("这是一个闰年:" + year);
    }
    else{
        alert("这是一个平年:" + year);
    }



Multi-branch statement:

grammar:

if(判断条件1){
        执行语句1;
}else if(判读条件2){
        执行语句2;
}

else{
        执行语句;(上述所有条件都不成立,执行这里)
}



Example: Calculate the y value and output it

where y = 0;
    where x = 10;

 

  //判断x区间
    if(x < 1){
        y = x;
    }else if(x>=1 && x < 10){
        y = 2 * x + 1;
    }else{
        y = 5 * x - 17;
    }
    alert("当x的值是:"+ x + ", y的值是:" + y);



Example Question: Grade Judgment

 var score = 90;
    if (score > 85){
        alert("优秀");
    }else if(score = 75 && score <= 85){
        alert("良好");
    }else if (score >= 60 && score < 75){
        alert("及格");
    }else{
        alert("不及格");
    }



do one thing repeatedly

select structure

switch statement

 switch(表达式){
     case 常量1:语句;break;
     case 常量2:语句;break;
     case 常量3:语句;break;
     ...
     case 常量1:语句;break;
     default:语句;break;         
 }



Note: if the result of the expression is equal to the constant of which case, then execute the following statement, jump out of the switch structure after executing the break, and execute the default statement if it is not satisfied:

The function of break: to jump out of the switch structure, if there is no break, continue to execute the following branch statement (without judgment)

Notice:

1. Pay attention to the application scenarios of the switch

2. Pay attention to case penetration. To add a break statement.

3. Each switch statement needs to add a default statement.

If the loop condition is true, execute the loop statement until the loop condition is not true

do_while and for loop _break and continue
do_while

grammar:

do{
    循环语句;
}while(循环条件)


[Note] Do not omit the semicolon after the do...while loop.


The difference between while loop and do...while loop:

1. While loop, first judge the loop condition

2. In the do_while loop, first run the loop statement once, and then judge the loop condition.

break: terminate the current loop

continue: Skip this cycle and execute the next cycle.


Summarize

The above is what I want to share today. I just have a rough understanding of the js cycle, and I have to wait for further study before I can share more content! Finally, I wish you in front of the screen a happy life and all the best~

Guess you like

Origin blog.csdn.net/m0_72975897/article/details/126591835