Skip to content

Instantly share code, notes, and snippets.

@rizalp
Last active July 2, 2022 05:29
Show Gist options
  • Save rizalp/5467463 to your computer and use it in GitHub Desktop.
Save rizalp/5467463 to your computer and use it in GitHub Desktop.
JavaScript: String #JavaScript #string
var a = 9 + "7"; // = "97, string"
var b = "7" + 9; // = "79 string"
var fooInt = parseInt("34s12", 10); //34
fooInt = parseInt("hello", 10); //NaN
var fooFloat = parseFloat("3.14", 10); // 3.14
var words = "Cities of the Interior";
alert(words.split(" ")); //can only do split on string
// [Cities,of,the,Interior]
words.charAt(0); //C
var array = ["a", "b", "c d"];
alert(array.join(" ").split(" ")); //command always start from the back
// [a,b,c,d]
function startsWith(string, pattern) {
return string.slice(0, pattern.length) == pattern;
}
alert(startsWith("rotation", "rot")); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment