search
HomeWeb Front-endFront-end Q&AWhy 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.

Why JavaScript is a loosely typed language

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!

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
React: Creating Dynamic and Interactive User InterfacesReact: Creating Dynamic and Interactive User InterfacesApr 14, 2025 am 12:08 AM

React is the tool of choice for building dynamic and interactive user interfaces. 1) Componentization and JSX make UI splitting and reusing simple. 2) State management is implemented through the useState hook to trigger UI updates. 3) The event processing mechanism responds to user interaction and improves user experience.

React vs. Backend Frameworks: A ComparisonReact vs. Backend Frameworks: A ComparisonApr 13, 2025 am 12:06 AM

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

HTML and React: The Relationship Between Markup and ComponentsHTML and React: The Relationship Between Markup and ComponentsApr 12, 2025 am 12:03 AM

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React and the Frontend: Building Interactive ExperiencesReact and the Frontend: Building Interactive ExperiencesApr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React and the Frontend Stack: The Tools and TechnologiesReact and the Frontend Stack: The Tools and TechnologiesApr 10, 2025 am 09:34 AM

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React's Role in HTML: Enhancing User ExperienceReact's Role in HTML: Enhancing User ExperienceApr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React Components: Creating Reusable Elements in HTMLReact Components: Creating Reusable Elements in HTMLApr 08, 2025 pm 05:53 PM

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React Strict Mode PurposeReact Strict Mode PurposeApr 02, 2025 pm 05:51 PM

React Strict Mode is a development tool that highlights potential issues in React applications by activating additional checks and warnings. It helps identify legacy code, unsafe lifecycles, and side effects, encouraging modern React practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft