Home >Web Front-end >Front-end Q&A >Why JavaScript is a loosely typed language
Reason: JavaScript is a literal scripting language, and its data type can be ignored. When assigning a value to a variable, its type does not need to be considered; it will not be considered until the variable is assigned a certain value. Have a certain type. Simply put, JavaScript variables can be interpreted as different types on different occasions.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript variables and constants
Learning any language must start with variables. A variable can be thought of as a named categorical container used to hold specific data. The data can be in many forms: integer, decimal, string or other data types. JS variables can be used directly without prior declaration. At the same time, JS variables can be named in any way, but for the convenience of reading, we generally just use numbers, letters, dollar signs, and underscores to name variables. The variable naming rules of JS are as follows:
1) JavaScript variables do not need to be declared in advance before use, but in order to prevent hard-to-find errors during program execution, we need to declare the variables in advance before using them. The declaration method is as follows
var mypetcat
2) JavaScript variables can be named with any symbol, but for the convenience of reading, they are usually named with letters, numbers, dollar signs, and underscores. Moreover, when declaring variables, start with a letter and do not use numbers alone for variable declaration
3) JavaScript is a case-sensitive language, and the variables MYPETCAT and mypetcat are different. Programmers are accustomed to using a "camel case" (or mixed case) method when declaring variables, that is, words and phrases are written together, without spaces, the first letter of each word is capitalized, but the entire name is The first letter can be uppercase or lowercase. Such as MyPetCat
4) There are many ways to assign values to JavaScript variables. You can directly assign the value
var myPetCat=11.2
or
myPetCat=11.2
when declaring the variable. You can also declare the variable first and then assign the value
var myPetCatmyPetCat=11.2
5) JavaScript can be called directly after declaration
alert(myPetCat)
6) Constants in JavaScript are defined using the Math method, which will be introduced later.
7) JavaScript can use the const method to declare constants. Once a constant is declared, it cannot be changed
const a=1;
JavaScript Data Types
The term "data type" represents the essential characteristics of the data a variable contains. A string variable contains a string, a numeric variable contains a numeric value, and so on. JavaScript is a "loosely typed" programming language, which means that JavaScript variables can be interpreted as different types on different occasions.
As can be seen from the previous content, in JavaScript, variables can be used without declaring their data type in advance. At this time, the JavaScript interpreter will make what it thinks is the correct judgment based on the situation. If we save a string in a variable now and want to use it as a numerical value later, this is completely feasible in JavaScript, provided that the string must contain something like a number.
1) Integers
Integers can be positive integers, negative integers and 0, such as 33, -100000
2) Floating point numbers
are different from integers Yes, floating point numbers have a fractional part, but the fractional part can be 0. The floating-point number representation can be 3.1415926 or the exponential form 35.4e5
In the exponential notation, e represents the power of 10. Using the exponential notation, you can easily express particularly large or small numbers.
3) Non-numeric value (NaN)
When the script tries to process some non-numeric data as a numerical value, the return value is NaN. The isNaN() function can be used to detect non-numeric values. When input When the parameter is a non-numeric value, return true; when the input parameter is a numerical value, return false
4) parseFloat() function
This function parses a string and returns a floating point number
If the first character of the parsed string is a number, the function will continue to parse until the end of the number, and then return a numerical value instead of a string
5) parseInt() function
This function is similar to the parseFloat() function and the return value is an integer or NaN. In addition, this function has a second optional parameter, which is used to specify the base of the value, thereby returning the decimal number corresponding to the binary, octal or other base value
6) Infinity
The maximum value that JavaScript can represent is 2 to the 53rd power. If it exceeds this value, it will be marked as Infinity. In addition, the keyword that indicates your negative infinity is - Infinity.
Use isFinite() to determine whether a value is infinite. When the input value is NaN, Infinity or - Infinity, false is returned, and true is returned in other cases
7) Boolean value
Boolean type data has only two values: true and false, which are commonly used to save the results of logical operations in code
When assigning a Boolean value, be careful not to include the value in quotation marks, such as
A=ture
If a Boolean value is used for calculation, JS will automatically convert true to 1 and false to 0
8) "Not" operator (!)
Be an operator! When placed before a Boolean value, JS interprets it as non, that is, the opposite value
9)null(空)
当我们让变量具有有效值,却又不是任何具体值时,就把null赋值给变量。对于数值来说,null就是0,对于字符串来说,null就是””,对于布尔变量来说null表示false。
10)undefined(未定义)
undefined不是关键字,而是预定义的全局变量。当某个变量已经在语句里使用了,但没有被赋予任何值时,他的值不是0或者null,而是undefined,表示JS不能识别他
javascript弱(宽松)类型的体现
一般规律是,约束越强越不容易出错,但编写程序时也越麻烦。在JavaScript中,因为约束比较弱,所以容易出现这种错误:最简单的例子:
var a =200; var b ="1"; var c= a + b;
你可能期望c是201,但实际上它是"2001",这个错误在强类型语言中决不会出现。然而正是因为JavaScript没有这些约束,所以可以很方便地拼接数字和字符串类型。
【相关推荐:web前端开发】
The above is the detailed content of Why JavaScript is a loosely typed language. For more information, please follow other related articles on the PHP Chinese website!