Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic syntax of JavaScript?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how the basic grammar of JavaScript is, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

History of JavaScript (JS)

In 1994, Netscape released version 0.9 of Navigator, which is the first mature web browser in the world and caused a sensation. But this is a veritable browser-only browsing pages, browsers can not interact with users, there are two ways to solve this problem, one is to use the existing language, allowing them to be embedded directly into the web page. The other is to invent a whole new language. LiveScript = > javaScript = = > ECMAscript

HTML: markup language

JavaScript: programming language

2. In 1995, Sun renamed the Oak language to Java and officially launched it to the market. Sun has trumpeted the promise that the language can be "written once, run everywhere" (Write Once, Run Anywhere), and it looks likely to dominate the future. 3. Netscape was tempted and decided to form an alliance with Sun. Brendan Eich, a 34-year-old system programmer, took the stage. In April 1995, Netscape hired him, and he designed the Javascript in 10 days. (polymorphic language) 5. (1) learn from the basic syntax of C language; (2) learn from the data types and memory management of Java language; (3) learn from Scheme language to enhance the function to the status of "first-class citizen" (first class); (4) learn from Self language and use the inheritance mechanism based on prototype (prototype).

What can JavaScript do?

1. Common web page effects [form validation, rowing pictures. ]

two。 Cooperate with H5 to realize the game [fruit ninja: http://www.jq22.com/demo/html5-fruit-ninja/]

3. Implementation of application-level programs [http://naotu.baidu.com]

4. Achieve statistical results [http://echarts.baidu.com/examples/]

5. Geolocation and other functions [http://lbsyun.baidu.com/jsdemo.htm#i4_5]

6. Online programming [https://codecombat.163.com/play/]

7. Js can realize artificial intelligence [facial recognition]

8. .

The composition of JavaScript

1. ECMASCRIPT: defines the syntax specification of javascript, describes the basic syntax and data types of the language 2. BOM (Browser Object Model): browser object model-there is a set of mature API that can operate the browser, and the browser can be operated through BOM. For example: pop-up box, browser jump, get resolution, etc. 3. DOM (Document Object Model): document object model-there is a mature set of API that can manipulate page elements, and you can manipulate page elements through DOM. For example: add a div, reduce a div, change the position of the div, etc.

Summary: JS is to manipulate the browser and tag structure through a fixed syntax to achieve various effects on the web page.

Where the JavaScript code is written

Like css, our js can be written on a page in a variety of ways to make it work.

Js can also be written in many ways, including inline type, embedded type and outer chain type.

Inline JS code (not recommended)

The js code written on the tag needs to be triggered by events (behaviors)

/ / I am the index.js file alert ('I am a pop-up layer')

Click and try.

Click it and try it.

Embedded JS code

Embedded js code will trigger directly when the page is opened.

Alert ('I am a pop-up layer')

Outer chain JS code (recommended)

As long as the outer linked js code introduces a html page, it will be triggered directly when the page is opened.

Create a new file with the .js suffix, write js code in the file, and introduce the written js file into the html page

Comments in JS

To learn a language, first learn the annotations of a language, because annotations are for us and for developers.

Writing a comment is helpful for us to read the code later.

Single-line comment

It is generally used to describe the function of the following line of code

You can write two / directly, or you can press ctrl + /

/ / I am a single-line comment / / the following code indicates that a pop-up layer alert appears in the browser ('I am a pop-up layer')

Multiline comment

It is usually used to write a long paragraph or to comment on a piece of code.

You can write / * * / directly and then write a comment between two asterisks, or you can press shift + alt + a

/ * I am a multiline comment * / / * the annotated code will not execute alert ('I am a pop-up layer') alert ('I am a pop-up layer') * / alert ('I am a pop-up layer')

Variable (emphasis)

A variable is a container that holds data in a program.

A variable is an identifier of data stored in computer memory, and the data stored in memory can be obtained according to the name of the variable.

In other words, we store a piece of data in memory, and then we have to give the data a name so that we can find it again later.

Syntax: var variable name = value

Define variables and assignments

/ / defining a variable var num;// assigns a value to a variable num = 100 *

Note:

00001. Only one value can be stored in a variable name

00002. When you assign a value to a variable again, the previous value is gone

00003. Variable names are case sensitive (JS is case sensitive)

Naming rules and conventions for variables

Rule: what must be obeyed, not obeying is wrong.

00001. A variable name can consist of numbers, letters, an underscore (_), and a dollar sign ($).

00002. Strictly case-sensitive

00003. Cannot start with a number, do not use Chinese characters to name

00004. Cannot be reserved words or keywords

00005. No spaces appear.

Specification: recommended compliance (developer default), do not comply with will not report an error

00001. Variable names try to be meaningful (semantic)

00002. Following the hump naming rules, when composed of multiple words, the first letter is capitalized starting with the second word.

Data type (emphasis)

Refers to the type of data we store in memory

We are usually divided into two categories: basic data types and complex data types.

Basic data type

1. Numeric type (number)

All numbers are numeric types (including binary, decimal, hexadecimal, etc.)

NaN (not a number), a non-numeric

2. String type (string)

Everything wrapped in quotation marks (either single or double quotation marks)

3. Boolean type (boolean)

Only two (true or false)

4. Null type (null)

There is only one, which is null, which means empty

5. Undefined type (undefined)

There is only one, which is undefined, which means no value.

Complex data type

00001. Object type (object)

00002. Function type (function)

00003. . . .

Determine the data type

Now that we have separated the types of data, we need to know what type of data we are storing.

Use the typeof keyword to determine

/ / the first use method var N1 = 100 typeof console.log (typeof N1); / / the second use method var S1 = 'abcdefg';console.log (typeof (S1))

Determine whether a variable is a number or not

You can use isNaN to determine whether a variable is a number or not

IsNaN: is not a number

/ / if the variable is a number var N1 = 100 console.log (isNaN (N1)); / / = > false// if the variable is not a number var S1 = 'Jack'console.log (isNaN (S1)); / / = > true

Data type conversion

Conversion between data types, such as converting numbers to strings, strings to Boolean, Boolean to numbers, etc.

Other data types are converted to numeric values

1. Number (variable)

You can cast a variable to a numeric type

You can convert decimals and keep decimals.

Boolean values can be converted

NaN will be returned if it is not convertible.

2. ParseInt (variable)

Check from the first place, convert the number, and know something that is not a number.

If it doesn't start with a number, return NaN directly.

Do not know the decimal point, can only keep integers

3. ParseFloat (variable)

Check from the first place, convert the number, and know something that is not a number.

If it doesn't start with a number, return NaN directly.

Know the decimal point once.

4. Mathematical operations other than addition

Both sides of the operator are operable numbers.

If the operator is not an operable number at any time, it returns NaN

Addition cannot be used.

Other data types are converted to strings

1. Variable .toString ()

There are some data types that cannot use the toString () method, such as undefined and null

2. String (variable)

All data types can

3. Use addition operation

In JS, + has two meanings

String concatenation: string concatenation will be carried out as long as there is a string on either side.

Addition operation: only when there are numbers on both sides, the mathematical operation will be performed.

Other data types are converted to Boolean

00001. Boolean (variable)

In js, only'', 0, null, undefined, NaN, these are false, and the rest are true

Operator

That is, the symbols used when performing operations in the code, not only mathematical operations, but also a lot of operations in js.

Mathematical operator

1. +

Addition is performed only when there are numbers on both sides of the symbol

String concatenation occurs as long as either side of the symbol is of a string type.

2.

Will perform a subtraction operation

Will automatically convert both sides into numbers for operation.

3. *

Can perform multiplication.

Will automatically convert both sides into numbers for operation.

4 、 /

Can perform division operations

Will automatically convert both sides into numbers for operation.

5.%

Will perform the remainder operation.

Will automatically convert both sides into numbers for operation.

Assignment operator

1. =

Is to assign the value on the right side of = to the variable name to the left of the equal sign.

Var num = 100

Is to assign 100 to the num variable.

Then the value of the num variable is 100.

2. + =

Var a = 10 * * a + = 10 * * console.log (a); / / = > 20

A + = 10 is equivalent to a = a + 10

3.-=

Var a = 10 * a-= 10 * console.log (a); / / = > 0

A-= 10 is equivalent to a = a-10

4. * =

Var a = 10 countries * = 10 countries console.log (a); / / = > 100

A * = 10 is equivalent to a = a * 10

5. / +

Var a = 10 * a / = 10 * console.log (a); / / = > 1

A / = 10 is equivalent to a = a / 10

6.% =

Var a = 10 + a% = 10 * * console.log (a); / / = > 0

A% = 10 is equivalent to a = a% 10

Comparison operator

1. =

Compare whether the values on both sides of the symbol are equal, regardless of the data type

1 ='1'

The values of the two are the same, so you get true

2, =

Compare whether the values and data types on both sides of the symbol are equal

1 ='1'

The two values are the same, but because the data types are different, you get false.

3! =

Compare whether the values on both sides of the symbol are not equal

1! ='1'

Because the values on both sides are equal, we get false when comparing them.

4.!

Compare whether the data types and values on both sides of the symbol are not equal

1! ='1'

Because the data types on both sides are really different, you get true

5, > =

Compare whether the value on the left is greater than or equal to the value on the right

1 > = 1 true

1 > = 0 true

1 > = 2 false

6 、 2 false

8 、

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report