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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
