Home  >  Article  >  Web Front-end  >  Is javascript a strongly typed language?

Is javascript a strongly typed language?

青灯夜游
青灯夜游Original
2021-11-19 18:20:372802browse

Javascript is not a strongly typed language, but a weakly typed language. JavaScript is a dynamically typed, weakly typed, prototype-based language. It 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.

Is javascript a strongly typed language?

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

JavaScript is a literal scripting language, a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called a javascript engine, which is part of the browser and is widely used in client-side scripting languages. It was first used on HTML web pages to add dynamic functions to HTML web pages. JavaScript is compatible with the ECMA standard, so it is also called ECMAScript.

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
}

【Recommended learning: javascript advanced tutorial

The above is the detailed content of Is javascript a strongly typed language?. 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