Last active
July 3, 2018 13:04
-
-
Save jastisriradheshyam/9a4a2d7fa955b8798eace195adea77d3 to your computer and use it in GitHub Desktop.
Java Script Tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ucFirstAllWords( str ) | |
{ | |
var pieces = str.split(" "); | |
for ( var i = 0; i < pieces.length; i++ ) | |
{ | |
var j = pieces[i][0].toUpperCase(); | |
pieces[i] = j + pieces[i].substr(1).toLowerCase(); | |
} | |
return pieces.join(" "); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Converts the string to number | |
// If passed string is not a number then it will return NaN (Not a Number) | |
function toNumber( strNumber) | |
{ | |
return +strNumber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+strNumber is causing the string to be a positive no., in case the string is not a number then the casting fails and returns NaN (Not a Number).
It also works by using -strNumber but will result in negative no. if positive no. is passed and positive if a negative no. is passed.