Conversion method: 1. Use parseInt() to convert the string to an integer, the syntax "parseInt("string")"; 2. Use parseFloat() to convert the string to a floating point number, the syntax "parseFloat ("string")"; 3. Use the multiplication operator, the syntax is "numeric string * 1".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Method 1: Use parseInt() to convert to an integer
console.log(parseInt("12345red")); //返回 12345 console.log(parseInt("0xA")); //返回 10 console.log(parseInt("56.9")); //返回 56 console.log(parseInt("red")); //返回 NaN console.log(parseInt("AF", 16)); //返回 175 console.log(parseInt("10", 2)); //返回 2 console.log(parseInt("10", 8)); //返回 8 console.log(parseInt("10", 10)); //返回 10 //如果十进制数包含前导 0,那么最好采用基数 10,这样才不会意外地得到八进制的值 console.log(parseInt("010")); //返回 8 console.log(parseInt("010", 8)); //返回 8 console.log(parseInt("010", 10)); //返回 10
Output result:
Method 2: Use parseFloat() to convert to floating point number
console.log(parseFloat("12345red")); //返回 12345 console.log(parseFloat("0xA")); //返回 NaN console.log(parseFloat("11.2")); //返回 11.2 console.log(parseFloat("11.22.33")); //返回 11.22 console.log(parseFloat("0102")); //返回 102 console.log(parseFloat("red")); //返回 NaN
Output result:
Method 3 : Use the multiplication operator
If a variable is multiplied by 1, the variable will be automatically converted to a numeric value by JavaScript. After multiplying by 1, the result is unchanged, but the type of the value is converted to a numeric value. If the value cannot be reduced to a legal number, NaN is returned.
var a = 1; //数值 var b = "1"; //数字字符串 console.log(a + (b * 1)); //返回数值 2
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to convert string to number in es6. For more information, please follow other related articles on the PHP Chinese website!

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall


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.

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools
