Table of Contents
Basic data types
Home Web Front-end JS Tutorial js data type conversion example

js data type conversion example

Mar 10, 2018 pm 03:11 PM
javascript Example Convert

This article mainly shares with you JS data type conversion examples, hoping to help everyone.

Basic data types

Convert other data types to number type

  • ##isNaN, Number, parseInt, parseFloat

  • When performing mathematical operations of addition, subtraction, multiplication and division

true -> 1  false -> 0'' ->
 0'1' -> 1'12px' -> true/12/NaNnull ->
  0undefined -> NaN{} /^$/ function()
  {} -> NaN[] -> 0[12,23] -> NaN//=>
   通过toStirng把数组转换为字符串<br>
   // 然后Number把字符串转换为数字
Copy after login

Mathematical operations in js

  • +, -, *, / Addition, subtraction, multiplication and division

  • Except for the addition operation, which has special features, the rest of the operators are mathematical operations, that is, when encountering Non-numeric types, first convert to

    number type, and then calculate

1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
Copy after login
Copy after login
  • Speciality of addition: When encountering a string, + is String concatenation is a mathematical operation without encountering a string

1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Copy after login
Copy after login
Thinking

1+null+true+undefined+[]+'xieth'+null+undefined+[]+10
Copy after login
Copy after login
Result

"NaNxiethnullundefined10"Number(undefined) -> 
NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaN
Copy after login
Convert other types to Boolean types

  • Boolean,!,!!

  • When judging conditions , also converted to Boolean type, and then verify the true or false condition

Only 0 NaN null undefined empty string-> Convert to false

The rest are true

Conversion of data types in js

Basic data types
Convert other data types to number type

  • isNaN, Number, parseInt, parseFloat

  • When performing mathematical operations of addition, subtraction, multiplication and division

true -> 1  false -> 0'' -> 0'1' -> 1'12px' -> 
true/12/NaNnull -> 0undefined -> NaN{} 
/^$/ function(){} -> NaN[] -> 0[12,23] -> 
NaN//=> 通过toStirng把数组转换为字符串<br>
// 然后Number把字符串转换为数字
Copy after login

Mathematical operations in js

  • +, -, *, / Addition, subtraction, multiplication and division

  • In addition to addition operations, there are special features , the rest of the operators are all mathematical operations, that is, when encountering a non-numeric type, first convert it to the

    number type, and then perform the calculation

1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
Copy after login
Copy after login
  • addition The particularity: when encountering a string, + is string concatenation, and when not encountering a string, it is a mathematical operation

1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Copy after login
Copy after login
Thinking

1+null+true+undefined+[]+'xieth'+null+undefined+[]+10
Copy after login
Copy after login
Result

"NaNxiethnullundefined10"Number(undefined) -> NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaN
Copy after login
Convert other types to Boolean types

  • Boolean,!,!!

  • When judging the condition, it is also converted to Boolean type, and then the true or false condition is verified.

Only 0 NaN null undefined empty string -> Convert to false

The rest are true

Related recommendations:

JavaScript Basics Data Type

Detailed explanation of php constants and data type instances

Detailed explanation of special data types in JavaScript

The above is the detailed content of js data type conversion example. 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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Quickly learn about ASCII value conversion in PHP Quickly learn about ASCII value conversion in PHP Mar 28, 2024 pm 06:42 PM

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values ​​in PHP and give specific code examples. 1. Change the characters

exe to php: an effective strategy to achieve function expansion exe to php: an effective strategy to achieve function expansion Mar 04, 2024 pm 09:36 PM

EXE to PHP: An effective strategy to achieve function expansion. With the development of the Internet, more and more applications have begun to migrate to the web to achieve wider user access and more convenient operations. In this process, the demand for converting functions originally run as EXE (executable files) into PHP scripts is also gradually increasing. This article will discuss how to convert EXE to PHP to achieve functional expansion, and give specific code examples. Why Convert EXE to PHP Cross-Platformness: PHP is a cross-platform language

See all articles