Home  >  Article  >  Web Front-end  >  Learn how to write JavaScript

Learn how to write JavaScript

coldplay.xixi
coldplay.xixiforward
2020-11-24 16:40:544087browse

javascript column introduces how to write JavaScript.

Learn how to write JavaScript

Related free learning recommendations: JavaScript (Video)

JavaScript introductory article:

If you already know how to write html and css, but you haven't learned JavaScript yet, you can start learning from the author's article here.

JavaScript is a V8 engine executed in the JS engine.
Where to write JavaScript is the first thing we need to know.

can be written in three places in total.

  1. Inline, add events to elements
<button onclick=&#39;alert("hello")&#39;>按钮</button>
  1. Write inside the html
    (usually placed before the closing tag of the body, in all html After the content)
<script type="text/javascript">	//type="text/javascript"可忽略
	//你的JS代码</script>
  1. Write to the file with the external j suffix named .js, and then quote it.
    For example, now I write an index.js file and place it in the same directory as your html
<script src="./index.js"></script>

Note Points:
1. src introduces the external JS file path
2. must appear alone
3. Between the start and end tags. No JS code will appear, and it will not be executed if it appears.

JavaScript output input method

Output:

  1. alert('prompt message'); - - A warning box will pop up, and Interrupt browser rendering and other operations.
  2. document.write(‘content’); - - Print the output content to the document stream. This approach is not usually used. Can be understood.
  3. console.log('prompt content'); - - Output the log content to the console (the console is what appears after you use F12 on the page). Front-end programmers are something that must be learned, because continuous debugging is required during the actual project development process. Especially when annoying bugs appear. console.log For more ways to playYou can read the author’s other article: The cool console operations you don’t know

Input:

  1. prompt(prompt message)
    The user clicks the confirmation button to get the value entered by the user, and the user clicks the cancel button to get null
  2. Input box in the form
    For example, input, textarea Wait, or change the element into an editable state (if you don’t know how to change the element into an editable state, you can read the author’s rich text editor implementation principle)
JavaScript variables.

Variable: Data storage space in memory. The data stored in this space can change as the operation proceeds.
Three elements of variables:
1. Variable name
2. Variable value
3. Data type
JS is a weak (dynamic) type language and does not care about variables The data type at creation time can only be determined after running.
Syntax:
Define variables:

var 变量名 = 变量值;//分解var 变量名;变量名 = 变量值;

In actual project development process, var is basically not used to declare variables. Generally, const and let are used to declare variables, because var has problems with reputation enhancement and memory leaks. For information on const and let, please see author b’s introduction to ES6.
Naming rules for variables:
a. Characters that can be included: letters, numbers, _, $
b. It cannot start with a number
c. It cannot be a keyword or Reserved words
Keywords/reserved words: Words with fixed grammatical meaning
For details on keywords and reserved words, please go to ECMAScript keywords and reserved words for detailed introduction
d. Strictly distinguish between uppercase and lowercase letters
Naming specifications for variables:
a. Use concise English words as variable names, so that the meaning can be understood by the name.
b. When there are multiple words, the first letter of the first word is lowercase, and the first letter of other words is capitalized.

For a detailed introduction to the data types of variables, please go to Introduction to JavaScript Data Types

Introduction to JavaScript Article:

If you already know how to write html and css, but you haven’t learned JavaScript yet, you can start learning from the author’s article here.

JavaScript is a V8 engine executed in the JS engine.
Where to write JavaScript is the first thing we need to know.

can be written in three places in total.

  1. Inline, add events to elements
<button onclick=&#39;alert("hello")&#39;>按钮</button>
  1. Write inside the html
    (usually placed before the closing tag of the body, in all html After the content)
<script type="text/javascript">	//type="text/javascript"可忽略
	//你的JS代码</script>
  1. Write to the file with the external j suffix named .js, and then quote it.
    For example, now I write an index.js file and place it in the same directory as your html
<script src="./index.js"></script>

Note Points:
1. src introduces the external JS file path
2. must appear alone
3. Between the start and end tags. No JS code will appear, and it will not be executed if it appears.

JavaScript output input method

Output:

  1. alert('prompt message'); - - A warning box will pop up, and Interrupt browser rendering and other operations.
  2. document.write(‘content’); - - Print the output content to the document stream. This approach is not usually used. Can be understood.
  3. console.log('prompt content'); - - Output the log content to the console (the console is what appears after you use F12 on the page). Front-end programmers are something that must be learned, because continuous debugging is required during the actual project development process. Especially when annoying bugs appear. console.log For more ways to playYou can read the author’s other article: The cool console operations you don’t know

Input:

  1. prompt(prompt message)
    The user clicks the confirmation button to get the value entered by the user, and the user clicks the cancel button to get null
  2. Input box in the form
    For example, input, textarea Wait, or change the element into an editable state (if you don’t know how to change the element into an editable state, you can read the author’s rich text editor implementation principle)
JavaScript variables.

Variable: Data storage space in memory. The data stored in this space can change as the operation proceeds.
Three elements of variables:
1. Variable name
2. Variable value
3. Data type
JS is a weak (dynamic) type language and does not care about variables The data type at creation time can only be determined after running.
Syntax:
Define variables:

var 变量名 = 变量值;//分解var 变量名;变量名 = 变量值;

In actual project development process, var is basically not used to declare variables. Generally, const and let are used to declare variables, because var has problems with reputation enhancement and memory leaks. For information on const and let, please see author b’s introduction to ES6.
Naming rules for variables:
a. Characters that can be included: letters, numbers, _, $
b. It cannot start with a number
c. It cannot be a keyword or Reserved words
Keywords/reserved words: Words with fixed grammatical meaning
For details on keywords and reserved words, please go to ECMAScript keywords and reserved words for detailed introduction
d. Strictly distinguish between uppercase and lowercase letters
Naming specifications for variables:
a. Use concise English words as variable names, so that the meaning can be understood by the name.
b. When there are multiple words, the first letter of the first word is lowercase, and the first letter of other words is capitalized.

For a detailed introduction to the data types of variables, please go to JavaScript's data type introduction

The above is the detailed content of Learn how to write JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:What does jquery mean?Next article:What does jquery mean?