First, the writing position of JS

The inline type

The js code can be written to the onclick property of the tag, which is executed when the button is clicked.

Although they can be written in tag attributes, they are not recommended because they are coupled with behavior and structure and are not easy to maintain

<button onclick="alert "> </button>Copy the code

embedded

You can write it in a script tag

<script> alert(" I am code in script tag "); </script>Copy the code

Outside the chain

It can be written in an external JS file, imported by the script tag

<script src="js/script.js"></script>
Copy the code

Writing to external files can be referenced in different pages at the same time and can take advantage of the browser’s caching mechanism. Recommended.

Two, js components

ECMAscript: the core syntax of JS

  • ECMAScript (ES) is the language specification for JavaScript.
  • ES6 was released in 2015, so it’s also called ES2015. A new version is released every June, named ES+ year
  • Compared with ES5, ES6 has made major changes, from the previous specification of more than 200 pages, to 600 pages.

DOM

Document Object Model, which provides properties and methods for manipulating DOM elements

BOM

The Browser Object Model provides properties and methods for manipulating objects. Browser elements

Three, variables,

Meaning: A container or pronoun containing a value, with no specific meaning

Six ways to create variables

  • Var declares a variable (es3)
  • Function Declare a function (es3)
  • Const declares a constant (es6) (no changes allowed)
  • Let declares a variable (ES6)
  • Class Creates a class (ES6)
  • Import Es6-based modules to import the required information (ES6)
VAR n = 10; vAR n = 10; // Create a variable called M without pointing to any value var M; Console.log (n, m); console.log(n, m); // create a variable based on ES6 LET that points to 100, but then redirect it to 200; let a = 100; a = 200; console.log(a); // create a variable that points to 1000 based on cONST Es6, then B can't point to any other value. So variables defined by cONST are also called constants; const b = 1000; b = 2000; console.log(b); //=>Uncaught TypeError: Assignment to constantvariable.Copy the code

4. Naming conventions of JS

Case sensitive

Follow: “Camel name”, start with numbers, letters, underscores, and $(but do not start with a number)

If your name is made up of multiple meaningful English words, capitalize all the words that follow, except the first lowercase letter.

Example: var studentName= lili

Cannot be a keyword or reserved word

  • Keywords are names that represent special meanings and functions, such as var and function
  • Reserved word, that is, is not a keyword now, but later may be specified as a keyword, reserved in advance

Names should be semantically named for later maintenance, such as goodMessage

Add a semicolon at the end of a sentence

Js data type

Raw value data type

  • The number of digital
  • String string
  • Boolean Boolean
  • BigInt tarsus
  • Null Null object pointer
  • Undefined null values
  • Es6 adds a new data type: Symbol, which represents a unique value

Object data type