JavaScript/JS basic knowledge points

JavaScript basic syntax

First acquaintance with JavaScript

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <script src=""></script>
    </head>
    <body>
        <script>
            // 这里是输出
            alert('我叫闫海静!');
            console.log('大家都这样说!!');
            document.write('我比李赔钱要好看');
        </script>
    </body>
</html>
  • <script>The JavaScript code is placed inside.

  • Comment

    // 单行注释符 ctrl + /
    /* 块状注释符,多行注释符 */ alt + shift + a
    

    effect:

    1. Write program description.
    2. Some codes are not used for the time being, please comment them out first.
    3. Debug the code.
  • Gadgets that output content:

    • alert(), Output the content in the form of a pop-up box.

    • console.log(), To print the content in the form of console log.

      We often look at the console in the later learning process.

    • document.write()To display the content on the page.

  • Every statement in ES ends with a semicolon. The semicolon can be omitted, if it is omitted, the parser will determine the end of the statement.

    But the suggestion is always added.

  • Regarding blanks in the program (spaces, new lines, blank lines)

    These blanks are ignored. There are blanks to increase the readability of the program.

__Note: __Everything in JavaScript is case sensitive (variables, functions).

VS-CODE close prompt:

// //关闭自动提示
// "editor.quickSuggestions": false,
// "editor.suggestOnTriggerCharacters": false,
// "editor.parameterHints": false,
// "editor.wordBasedSuggestions": false,
// "editor.snippetSuggestions": "none",
// "files.autoSave": "off",
// "editor.autoClosingBrackets": "never",
// "editor.autoClosingOvertype": "never",
// "editor.autoClosingQuotes": "never",
// "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
// "html.autoClosingTags": false,
// "liveServer.settings.donotShowInfoMsg": true,

Other ways to use JavaScript

  • Inline writing

    • The limitation is very big, it can only be added for the event, and it is used less.
    • The code separation is the worst, and the readability is not strong.
  • JavaScript code embedded in the page

    • Write the script tag at the end of the body tag and then write the js code.
    • It is most used in teaching and practical projects.

    Internal fitting type:

    If you put the script code in the head, the browser will execute the JavaScript code first and then execute the code in the body.

    If you put the script code at the bottom of the body, the browser will execute the content on the body first, and then execute the JavaScript content.

  • Contains external javascript code

    <script>Include external javascript files through tags.

    The src attribute is required, and its value points to the location of the js file.

    As long as the file that outputs the javascript code is imported, there is no problem.

variable

What is a variable

Variables that can be changed are called variables. What is a container used to store data?

Variables are like a cup (container), and water is like our data. When you get the cup, you get the water in it.

The role of variables (why have variables?)

Perform calculations.

  • 1Byte (byte, the smallest storage unit) = 8bit (8 0 or 1)
  • 1KB = 1024Byte
  • 1MB = 1024KB
  • 1GB = 1024MB
  • 1TB = 1024GB
  • 1PB = 1024TB
  • 1EB = 1024PB

Byte and bit should be case sensitive.

Definition of variables

  1. Define variables and then assign values.
 var a;
a = 10;
a = 20;
console.log(a);
1. 先在内存中定义一个容器(a)
2. 将10放进了这个容器中。
3. 将10从这个容器中删除,然后将20放入到这个容器中。
4. 拿到a这个容器就相当于拿到了里面的值(20),拿到之后打印出来。
  1. Assign values ​​directly when defining variables.
var b = 10;
b = 20;
console.log(b); // 返回: 20

The above two methods are equivalent.

  1. Special case
c = 200;
console.log(c);

No one uses this method. You can temporarily think that this is a defined variable, but strictly speaking it is not.

  1. Define multiple at once
  • The first case
var d= 1 , e = 2;
console.log(d);
console.log(e);
  • Second case
var d = e = 30;
console.log(e);
console.log(d);
  1. First declare an e (variable without var) container, and put 30 into this container.
  2. Declare a variable d (with var), and then equal to e; equivalent to var d = e;
  3. Get the value 30 in e and put this value in d.

Define multiple variables, the first case is commonly used.

Variable definition rules

There are some rules for naming variables.

Variable name definition:

  • It is composed of numbers, letters, underscores and $ signs.

  • Cannot start with a number.

  • Cannot have the same name as keywords and reserved words.

    Keyword: is the language that has been used.

    Reserved words: it is possible to use the language in the future.

  • Keywords and reserved words

关键字
break do instanceof typeof
case else new var
catch finally return void
continue for switch while
debugger* function this with
default if throw
delete in try

保留字
abstract enum int short
boolean export interface static
byte extends long super
char final native synchronized
class float package throws
const goto private transient
debugger implements protected volatile
double import public

Keywords and reserved words don’t need to be memorized and avoided naturally. Moreover, keywords and reserved words are subject to changes in the specification.

Conventions are things made by conventions.

When naming it, you should think about it and make it meaningful. Do not use Pinyin and Chinese casually.

Generally we use var a; var b when this variable has no semantics. If we write a large piece of code where the variables have certain semantics, we need to define the variable names as having semantics.

Conventional naming method (recommended):

  • Big hump nomenclature:, var BackgroundColorcapitalize the first letter of each word. (Not used much)
  • Little hump nomenclature:, var backgroundColorthe first letter of the first word is lowercase, and the first letter of each remaining word is uppercase.
  • Underscore:, var background_coloruse _to separate each word .

Case: There are two variables, one is stored in 100 and the other is stored in 200, exchange the values ​​(please use two methods).

  • Method 1: Declare a third-party variable.
var a = 100;
var b = 200;
var c;

c = a;
a = b;
b = c;
console.log(a);
console.log(b);
  • Method two: use addition and subtraction
var a = 100;
var b = 200;

a = a + b;//300
b = a - b;//100
a = a - b;//200

console.log(a);
console.log(b);
  • If the console reports red when running, it means that the program is interrupted and the following code will no longer be executed.
  • If you use keywords as variable names, the console will report an error.

Type of variable (content in container)

The variable is a cup, and there are many things in the cup. Water, beverages, soy sauce

  • type of data

    Basic data types, object (complex, reference) types.

  • Basic data class

    Number

    String

    Boolean

    undefined

    null

  • Object type (described later)

  • Number type

    • Integer
var int = 10;
console.log(Number.MIN_VALUE);//最小值 5e-324
console.log(Number.MAX_VALUE);//最大值 1.7976931348623157e+308
console.log(int);

If more than the maximum and minimum values, it will be automatically switched Infinity, -Infinity.

Numerical inside eit, is representative of scientific notation.

  • Decimal

    • Floating point numbers must contain a decimal point, there must be a digit after the decimal point, and there is no need for a digit before it.
      var flo = .23Equivalent to var flo = 0.23.
    • Floating point numbers are inaccurate when calculating. This is true for all computer languages.
ar flo1 = 0.1;
var flo2 = 0.2;
console.log(flo1 + flo2); //返回:0.30000000000000004

Solution: Enlarge the decimal to an integer (multiply), perform arithmetic operations, and then reduce it to a decimal (divide).

Or directly use the designated math library to operate in the future.

var flo1 = 0.1;
var flo2 = 0.2;
console.log((flo1*10 + flo2*10)/10); // 0.3
  • Floating point storage takes up more memory than integer storage, so the engine will convert floating point values ​​to integers at certain times.
var flo = 10.0;
console.log(flo)// 返回:10
//这里的flo输出时,不要加引号,不然会当做字符串处理。
  • Other values ​​that are not commonly used
    • Binary, starting with 0b. 0, 1
    • Octal, starting with 0o. 0~7
    • Hexadecimal, 0x starts with 0-9a-f

Now there is a number that is 10 in decimal and I want to convert it to binary.

  • String type
    Strings, strings can be enclosed by double quotation marks or single quotation marks. There is no difference between these two methods in ES.
var str = '李沛华';
str = '123';//也是字符串。
str = '';//空串
str = '     ';//空白串
str = 'I\'m fine';
console.log(str);

If there are nested quotation marks, use them alternately. Double quotation marks are used outside and single quotation marks are used inside. Use single quotation marks outside and use double quotation marks inside. You can also use escape characters.

\n,换行
\t,制表符
\r,回车
\\,斜杠
\',单引号
\",双引号

The escape character can be interpreted in the string.

Guess you like

Origin blog.csdn.net/weixin_47021982/article/details/112955678