JavaScript Strings

var myName = "your name";

var sampleStr = "Alan said, \"Peter is learning JavaScript\".";

/* Other String Escape
Code	Output
\'	single quote
\"	double quote
\\	backslash
\n	newline
\r	carriage return
\t	tab
\b	word boundary
\f	form feed

// Concatenate
var ourStr = "I come first. " + "I come second.";

// Concatenating Strings with the Plus Equals 
var ourStr = "I come first. ";
ourStr += "I come second.";

// Variables in strings
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";

var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is ";
ourStr += anAdjective;

console.log("Alan Peter".length);

// Bracket notation
var firstName = "Charles";
var firstLetter = firstName[0];

var firstName = "Ada";
var lastLetter = firstName[firstName.length - 1];

var firstName = "Augusta";
var thirdToLastLetter = firstName[firstName.length - 3];

Leave a Reply

Matt is the author of five Apress books including Learn RStudio IDE, Quick, Effective, and Productive Data Science, Objective-C Recipes, Swift Quick Syntax Reference, Objective-C Quick Reference, and the upcoming Pro Data Visualization with R and JavaScript. He has over 20 years of experience in technology, psychometrics, and data analytics working in major higher education institutions such as The College Board and Educational Testing Service. He has earned a Master’s degree in Information Systems Management and a Bachelor’s degree in Quantitative Psychology.