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

Case Analysis of JavaScript variables and transformations

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

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "JavaScript variables and transformation case analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "JavaScript variables and transformation case analysis" bar!

1. Statement

Be sure to declare variables with the keyword var before using them. When a variable is declared and not assigned, the initial value of the variable is undefined.

Var ageconsole.log (age)

two。 Naming convention

① can be numbers, letters, underscores, $

② cannot start with a number

③ cannot be a keyword, reserved word.

3. Promotion of variable declaration age=10console.log (age) var age

The above code still works, because each major browser vendor has its own parser, and when translating the code, it will raise the declaration part of the variable to the top of the current area.

4. Judgment of data type

In JS, we use the keyword typeof to detect any type of literal amount or the type of data stored in a variable.

Console.log (typeof 123) console.log (typeof '123')

5. Conversion of data types

① when there is no string in the statement, the plus sign (+) is a mathematical addition, which can be used for mathematical addition.

② if a string appears in the statement, the plus sign (+) is no longer a mathematical addition, but a hyphen that concatenates numbers into strings.

6. Convert a string to a number

In JS, we can convert strings to numbers through the built-in functions parseInt and parseFloat.

① parseInt: you can convert numeric characters in a string to numbers. (integer part)

② parseFloat: you can convert numeric characters in a string to numbers. (accurate to the fractional part)

Console.log (parseInt ("123")) console.log (parseInt (" 123.123 ")) console.log (parseFloat (" 123.123 "))

7. Other ways of declaring variables

You can assign a value when declaring:

Var age = 18pr name = 'Zhang San' 8. Mathematical object

In the JS language, a built-in mathematical object (Math) is provided, which has many properties and methods to use.

Console.log (typeof Math) console.log (Math)

In the above figure, those without f are attributes and those with f are methods.

Parentheses are not required after the properties of the ② Math object, and parentheses are required after the method.

Console.log (Math.PI) console.log (Math.abs (- 1)) console.log (Math.pow (2pm 3))

At this point, I believe that you have a deeper understanding of "JavaScript variable and transformation example analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report