Home  >  Article  >  Web Front-end  >  What does javascript weak type mean?

What does javascript weak type mean?

青灯夜游
青灯夜游Original
2021-06-22 16:30:385471browse

In JavaScript, weak typing means that the data type can be ignored, and a variable can be assigned values ​​of different data types. JavaScript is a weakly typed language, which allows implicit conversion of variable types, forced type conversion, etc. For example, strings and values ​​can be automatically converted; while strongly typed languages ​​generally do not allow this.

What does javascript weak type mean?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Strongly typed language:

A language that enforces data type definitions. In other words, once a variable is assigned a certain data type, it will always be that data type if it is not cast. For example: if you define an integer variable a, then it is impossible for the program to treat a as a string type. A strongly typed definition language is a type-safe language.

Weakly typed language:

A language in which data types can be ignored. It is the opposite of a strongly typed definition language, in which a variable can be assigned values ​​of different data types.

Weakly typed languages ​​allow implicit conversion of variable types, forced type conversion, etc. For example, strings and values ​​can be automatically converted; while strongly typed languages ​​generally do not allow this.

The embodiment of js weak types

The general rule is that the stronger the constraint, the less error-prone it is, but the more troublesome it is when writing a program. In JavaScript, because the constraints are relatively weak, this kind of error is prone to occur: The simplest example:

 var a =200;
 var b ="1";
 var c= a + b;

You may expect c to be 201, but in fact it is "2001". This error occurs in strongly typed languages The center will never appear. However, precisely because JavaScript does not have these constraints, it can easily concatenate numeric and string types.

Another example is the following:

var a = '11';
a = a - '';
alert(typeof a);// -->number

"-" can be a unary operator (negative) or a binary operator (subtraction operation)

In loop statements (if, while), an Object object can be implicitly converted from a BOOLEAN value.

var obj = {name:'jack'}
if(obj){
    //do more
}

What is more difficult to find is in the alert() function.

String.prototype.fn = function(){return this};
var a = 'hello';
alert(typeof a.fn()); //-->object
alert(a.fn()); //-->hello

We know that this can be understood as an instance object of the current class. Since it is an object, typeof a.fn() naturally returns object.

The key is the last alert(a.fn()). What a.fn() returns is obviously an object, but it is implicitly converted into a string "hello" for display.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What does javascript weak type mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn