


How to implement explicit conversion and implicit conversion in javascript
The editor below will share with you an article about explicit conversion and implicit conversion (detailed explanation) based on javascript. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.
Display conversion
1. Question: Please enter your age this year and ask for 5 years How old will you be?
//a.prompt接收到的数据是string类型的。 var age = prompt("请输入你今年的年龄"); alert(typeof age); var age5 = age + 5; // 这里只会拼接成了15,而不是加5 alert("我今年"+age+"岁了,5年后我"+age5+"岁了");
2. The string must be converted into number type.
In response to the above problem, age needs to be converted into a numeric type
2.1 You can use Number (the content that needs to be converted);
var str = true; var num = Number(str); console.log(num); //1 console.log(typeof num); //number // 注意: // 1.如果这个转换的字符串本身就是一个数字,那么可以转换成功; 如果这个字符串本身不是一个数字,那么转成NaN. // 2.如果这个字符串本身是一个数字,前后有空格,也是会转换成功的; 如果中间有空格,就转成NaN. // 3.如果是一个"",或者是" ",或者是flase,那么会转换成0.true转成1,undefined转成NaN // 4.如果字符串的本身是一个小数,也是可以转换成功的。
2.2 You can use parseInt (required Content to be converted);
var str = "123"; var num = parseInt(str); console.log(num); //123 console.log(typeof num); //number // 注意: // 1.从左往右查找,直到遇到第一个非数字为止,前面的所有的内容转换成数字。 // 2.如果找完了,都没有找到一个数字,那么就转换成NaN. // 3."" 和 " " 转化成NaN // 4.如果字符串里面是小数,那么转换后只能得到他的整数部分。
2.3 You can use parseFloat(Content to be converted);
var str = "123.24ll"; var num = parseFloat(str); console.log(num); //123.24 console.log(typeof num); //number // 注意:如果字符串里面是小数,那么转换后还是小数。。 //******注意: true会转成1,false会转换0. // 如果字符串转数字,一般的使用parseInt或者parseFolat。 // 如果是其他类型,比如布尔类型,使用Number();
3. Other types of data are converted into string types.
3.1 You can use String (the content that needs to be converted);
var num = 123; var str = String(num); console.log(str); //"123" console.log(typeof str); //string //注意: "123" "true" "false" "undefined" "null" "NaN"
3.2 You can use the content that needs to be converted.toString();
var num = 123; var str = num.toString(); console.log(str); //"123" console.log(typeof str); //string //注意: "123" "true" "false "NaN" //undefined 和null 不能使用toString。
4 .Other data types are converted into boolean types.
4.1 You can use Boolean (the content that needs to be converted);
var num = ""; var res = Boolean(num); console.log(res); //false console.log(typeof res); //boolean //注意: //那些可以转换成布尔类型的false: 0 -0 false "" undefined null NaN //" "会转换成true
Implicit conversion
1. Convert other types to number type.
1.1 You can add a positive sign in front of the content that needs to be converted.
var str = "123"; var res = +str; console.log(res); //123 console.log(typeof res); //number
1.2 The content that needs to be converted can be used for arithmetic operations and cannot be used later.
var str = "123"; var res = str * 1; console.log(res); //123 console.log(typeof res); //number //注意: 一定要和连接符做一个区分。
2. Other types can be converted into string type. Use the connector ""
var num = 123; var str = num + ""; console.log(str); //"123" console.log(typeof str);//string
3. Convert other types to boolean type. Use the negation operator
var num = undefined; var res = !!num; console.log(res); //false console.log(typeof res);boolean // 注意:那些能转成布尔类型的false:0 -0 false undefiend null NaN ""
1. Question: Please enter your age this year and find out how old you will be in 5 years.
//a.prompt接收到的数据是string类型的。 var age = +prompt("请输入你今年的年龄"); var age5 = age + 5; alert("我今年"+age+"岁了,5年后我"+age5+"岁了");
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to connect a printer in javaScript
Introduces in detail the use of Vue event modifier capture
How to pass events in the vue component
404 problem occurs when refreshing the page in react-router
The above is the detailed content of How to implement explicit conversion and implicit conversion in javascript. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
