Home Web Front-end JS Tutorial How to implement explicit conversion and implicit conversion in javascript

How to implement explicit conversion and implicit conversion in javascript

Jun 20, 2018 pm 04:21 PM
implicit conversion

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+"岁了");
Copy after login

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.如果字符串的本身是一个小数,也是可以转换成功的。
Copy after login

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.如果字符串里面是小数,那么转换后只能得到他的整数部分。
Copy after login

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();
Copy after login

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"
Copy after login

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。
Copy after login

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
Copy after login

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
Copy after login

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
//注意: 一定要和连接符做一个区分。
Copy after login

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
Copy after login

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 ""
Copy after login

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+"岁了");
Copy after login

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!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use short in java How to use short in java May 07, 2024 am 03:33 AM

short is a primitive data type in Java that represents a 16-bit signed integer in the range -32,768 to 32,767. It is often used to represent small integers, such as counters or IDs, and supports basic arithmetic operations and type conversions. But since short is a signed type, you need to be careful when using division to avoid overflow or underflow.

Usage of ifnull in sql Usage of ifnull in sql Apr 28, 2024 am 09:57 AM

The IFNULL function checks whether an expression is NULL and returns the specified default value if so, otherwise it returns the value of the expression. It prevents null values ​​from causing errors, allows manipulation of null values, and improves the readability of queries. Usage includes: replacing null values ​​with default values, excluding null values ​​from calculations, and nested usage to handle multiple null value situations.

How to calculate division in c language How to calculate division in c language Apr 13, 2024 pm 09:12 PM

In C language, the behavior of the division operator / depends on the data type of the operands: Integer division: When the operand is an integer, integer division is performed and the result is rounded down. Floating point division: When the operand is a floating point number, floating point division is performed and the result is a floating point number. Type conversion: When one operand is an integer and the other is not, the integer is implicitly converted to a floating point number, and then floating point division is performed. Divisor by 0: A mathematical error occurs when the divisor is 0. Modulo operation: Use the % operator for modulo operation instead of modulo division.

What does char in java mean? What does char in java mean? May 01, 2024 pm 06:15 PM

The char type in Java is used to store a single Unicode character, accounting for 2 bytes, ranging from U+0000 to U+FFFF. It is mainly used to store text characters. It can be initialized through single quotes or Unicode escape sequences, and can participate in comparison, Equality, inequality and join operations can be implicitly converted to int type or explicitly converted to Character objects.

Usage of (+ in oracle Usage of (+ in oracle May 08, 2024 pm 08:12 PM

The plus (+) operator in Oracle can be used to: connect strings, numbers, dates, and time intervals; handle NULL values ​​and convert NULL to non-NULL values; convert data types to string types.

What does * mean in mysql What does * mean in mysql Apr 26, 2024 am 07:21 AM

The asterisk (*) in MySQL means "all" and has different uses: Select all columns Select all rows JOIN wildcards for table LIKE clause Quantifier implicit type conversion for REGEXP clause

Let's explore common application scenarios of implicit type conversion! Let's explore common application scenarios of implicit type conversion! Jan 11, 2024 pm 04:45 PM

Let’s explore common application scenarios of implicit type conversion! Introduction: In programming languages, implicit type conversion is an automatically performed data type conversion process. In some programming languages, this conversion is performed implicitly, without the need to explicitly tell the compiler or interpreter to perform the conversion. Implicit type conversion has a wide range of application scenarios in programming. This article will discuss some of the common application scenarios. Implicit type conversion in numerical calculations In numerical calculations, operations between different types of data are often required. When different types of data

What are the matching rules for C++ function overloading? What are the matching rules for C++ function overloading? Apr 27, 2024 am 08:27 AM

The C++ function overload matching rules are as follows: match the number and type of parameters in the call. The order of parameters must be consistent. The constness and reference modifiers must match. Default parameters can be used.

See all articles