// Two strings: var firstString = 'Hello world' var secondString = "nice to meet you"
String
method:
var newString = String( 6+5+"hello" ) // newString is now the string '11hello'
var firstString = 'Hello world' var secondString = "nice to meet you" var message = firstString + " it is " + secondString console.log(message)would result in
var x = 3, y = 5 var sumString = "The sum of " + x + " and " + y + " is " + (x+y) console.log(sumString)would result in
Character | Interpretation | Example | Example Returns |
---|---|---|---|
\0 | Null character, escape in numeric string | "hello\0goodbye" "85\04" | hellogoodbye 85 |
\' | Single quotation mark | "hello\'goodbye" | hello'goodbye |
\" | Double quotation mark | "hello\"goodbye" | hello"goodbye |
\\ | Backslash | "hello\\goodbye" | hello\goodbye |
\n | New line | "hello\ngoodbye" | hello goodbye |
\x | Character with hex value HH | "\x23" | # |
\xHH | Character with hex value HH | "\x23" | # |
\uXXXX | Character with hex value XXXX | "\u00E9" | é |
var aString = "Hello world" aString.length // returns 11
var hiddenMessage = String.fromCharCode(72,69,76,76,79,32,87,79,82,76,68) console.log( hiddenMessage )would result in
var anchorText = "Section One" document.body.innerHTML = anchorText.anchor('section_one_anchor')would output the following HTML:
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var firstLetter = lorem.charAt(0) var tenthLetter = lorem.charAt(9) var lastLetter = lorem.charAt(lorem.length - 1) console.log( firstLetter, tenthLetter, lastLetter )would result in
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var firstLetter = lorem.charCodeAt(0) var tenthLetter = lorem.charCodeAt(9) var lastLetter = lorem.charCodeAt(lorem.length - 1) console.log( firstLetter, tenthLetter, lastLetter )would result in
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var firstA = lorem.indexOf('a') var firstIp = lorem.indexOf('ip') console.log( firstA, firstIp )would result in
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var lastA = lorem.lastIndexOf('a') var lastIp = lorem.lastIndexOf('ip') console.log( lastA, lastIp )would result in
var linkText = 'Mercyhurst Math Site' var MMurl = 'http://math.mercyhurst.edu' document.write( 'Get more information at the ' + linkText.link(MMurl)would result in the following text, with link, in the HTML document:
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var loremSubstring = lorem.slice( 6, 17 ) console.log( loremSubstring )would result in
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var loremSubstring = lorem.substr( 6, 11 ) console.log( loremSubstring )would result in
var dogString = "labrador,boxer,papillon,chow chow,bulldog" var dogArray = dogString.split(",") console.log(dogArray)would result in
var myString = "Hello WORLD" console.log( myString.toLowerCase() )would result in
var myString = "Hello WORLD" console.log( myString.toUpperCase() )would result in
var myString = " Hello WORLD " console.log( myString.toUpperCase() )would result in
var lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." var pattern1 = /(in)/g // find all occurrences of 'in' var result1 = lorem.match(pattern1) // result1 = [ 'in', 'in' ] var pattern2 = /l\w+/gi // find all words starting with l, regardless of case var result2 = lorem.match(pattern2) // result2 = [ 'Lorem', 'lor', 'lit', 'labore', 'lore', 'liqua' ]
var sampleString = "Hello world!" var pattern = /l/ var result = sampleString.search(pattern) // find index of first 'l' in string result = 2
var sampleString = "Hello world!" var pattern = /(hello)/i // find first occurrence of hello, regardless of case var result = sampleString.replace(pattern, 'Goodbye') // result = 'Goodbye world!' var pattern = /l/g // find all occurrences of 'l' var result = sampleString.replace(pattern, 'k') // result = 'Hekko workd!' var nameString = "John Smith" var pattern = /(\w+)\s(\w+)/ // look for pattern word-space-word var newNameString = nameString.replace(pattern, '$2, $1') // second word-comma-first word // newNameString = 'Smith, John'