Tutorial by Examples

Strings in JavaScript can be enclosed in Single quotes 'hello', Double quotes "Hello" and (from ES2015, ES6) in Template Literals (backticks) `hello`. var hello = "Hello"; var world = 'world'; var helloW = `Hello World`; // ES2015 / ES6 Strings can be created...
If your string is enclosed (i.e.) in single quotes you need to escape the inner literal quote with backslash \ var text = 'L\'albero means tree in Italian'; console.log( text ); \\ "L'albero means tree in Italian" Same goes for double quotes: var text = "I feel \"high\&quot...
The most "popular" way of reversing a string in JavaScript is the following code fragment, which is quite common: function reverseString(str) { return str.split('').reverse().join(''); } reverseString('string'); // "gnirts" However, this will work only so long as ...
To trim whitespace from the edges of a string, use String.prototype.trim: " some whitespaced string ".trim(); // "some whitespaced string" Many JavaScript engines, but not Internet Explorer, have implemented non-standard trimLeft and trimRight methods. There is a proposa...
Use .slice() to extract substrings given two indices: var s = "0123456789abcdefg"; s.slice(0, 5); // "01234" s.slice(5, 6); // "5" Given one index, it will take from that index to the end of the string: s.slice(10); // "abcdefg"
Use .split to go from strings to an array of the split substrings: var s = "one, two, three, four, five" s.split(", "); // ["one", "two", "three", "four", "five"] Use the array method .join to go back to a string: s.split(&...
All JavaScript strings are unicode! var s = "some ∆≈ƒ unicode ¡™£¢¢¢"; s.charCodeAt(5); // 8710 There are no raw byte or binary strings in JavaScript. To effectively handle binary data, use Typed Arrays.
To detect whether a parameter is a primitive string, use typeof: var aString = "my string"; var anInt = 5; var anObj = {}; typeof aString === "string"; // true typeof anInt === "string"; // false typeof anObj === "string"; // false If you ev...
To compare strings alphabetically, use localeCompare(). This returns a negative value if the reference string is lexicographically (alphabetically) before the compared string (the parameter), a positive value if it comes afterwards, and a value of 0 if they are equal. var a = "hello"; va...
String.prototype.toUpperCase(): console.log('qwerty'.toUpperCase()); // 'QWERTY'
String.prototype.toLowerCase() console.log('QWERTY'.toLowerCase()); // 'qwerty'
Say you have a <textarea> and you want to retrieve info about the number of: Characters (total) Characters (no spaces) Words Lines function wordCount( val ){ var wom = val.match(/\S+/g); return { charactersNoSpaces : val.replace(/\s+/g, '').length, characte...
Use charAt() to get a character at the specified index in the string. var string = "Hello, World!"; console.log( string.charAt(4) ); // "o" Alternatively, because strings can be treated like arrays, use the index via bracket notation. var string = "Hello, World!";...
To search for a string inside a string, there are several functions: indexOf( searchString ) and lastIndexOf( searchString ) indexOf() will return the index of the first occurrence of searchString in the string. If searchString is not found, then -1 is returned. var string = "Hello, World!&q...
The .indexOf method returns the index of a substring inside another string (if exists, or -1 if otherwise) 'Hellow World'.indexOf('Wor'); // 7 .indexOf also accepts an additional numeric argument that indicates on what index should the function start looking "harr dee harr dee harr&quot...
JavaScript has native conversion from Number to it's String representation for any base from 2 to 36. The most common representation after decimal (base 10) is hexadecimal (base 16), but the contents of this section work for all bases in the range. In order to convert a Number from decimal (base...
6 This can be done using the .repeat() method: "abc".repeat(3); // Returns "abcabcabc" "abc".repeat(0); // Returns "" "abc".repeat(-1); // Throws a RangeError 6 In the general case, this should be done using a correct polyfill for the ES6...
The method charCodeAt retrieves the Unicode character code of a single character: var charCode = "µ".charCodeAt(); // The character code of the letter µ is 181 To get the character code of a character in a string, the 0-based position of the character is passed as a parameter to charCo...

Page 1 of 1