{"id":28461,"date":"2021-01-14T21:58:22","date_gmt":"2021-01-15T05:58:22","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=28461"},"modified":"2021-01-20T04:52:50","modified_gmt":"2021-01-20T12:52:50","slug":"javascript-strings","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/javascript-strings\/","title":{"rendered":"An Introduction to Strings in JavaScript"},"content":{"rendered":"\n<p>A string in JavaScript, as is the case in many other programming languages, is a data type that holds data in text form. A common use for strings in the context of a web application is holding a user\u2019s input from a form. Taking an example of a search form, the search query is stored as a string.<br><\/p>\n\n\n\n<p>Once the user input is submitted in our search form, some comparative operations are performed on the submitted string. This is to find the matching credentials and return the results to the user. Comparative operations are JavaScript operators, i.e. \u2018=\u2019, \u2018==\u2019, \u2018===\u2019, \u2018&lt;\u2019, \u2018&gt;\u2019, to name a few. These operators return a true or false value after comparing the values held by a string.<br><\/p>\n\n\n\n<p>This article focuses on the equality operators for strings. This <a href=\"https:\/\/careerkarma.com\/blog\/js-comparison\/\">guide<\/a> goes deeper on JavaScript operators in general.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a JavaScript String?<\/h2>\n\n\n\n<p>A string in JavaScript is one of many native data types. It holds values represented as text and can be constructed a few different ways. The first way is known as a string primitive. This consists of storing a string literal in a variable.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const stringPrim1 = 'Hello World!'\nconst stringPrim2 = &quot;Hello World!&quot;\nconst stringPrim3 = `Hello World!`<\/pre><\/div>\n\n\n\n<p>A string literal can be wrapped in single quotes (\u2018\u2019), double quotes (\u201c\u201d), or backticks (&#8220;). Know that a JavaScript string can only be interpolated by using backticks. String interpolation embeds an expression in a string.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const name = &quot;John&quot;\nconsole.log(`Hello World! My name is ${name}`)\n\nlog: &quot;Hello World! My name is John&quot;<\/pre><\/div>\n\n\n\n<p>We have a variable storing a string with a value of \u201cJohn\u201d in this example. By using backticks, we can use the JavaScript string interpolation method. We can reference the value of name in an abstract way.<br><\/p>\n\n\n\n<p>This is only one way to accomplish string interpolation in JavaScript. If you want to learn more ways, this <a href=\"https:\/\/careerkarma.com\/blog\/javascript-string-interpolation\/\">guide<\/a> is a good place to start.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Comparative Operators on JavaScript Strings<\/h2>\n\n\n\n<p>We need to disregard case sensitivity in our strings for a search form to be effective. It would be tedious for our users if their input had to exactly match the values stored in our database. JavaScript provides a work around with the <code>toUpperCase()<\/code> and <code>toLowerCase()<\/code> methods.<br><\/p>\n\n\n\n<p>Let\u2019s search for cute kitten videos on a video web app.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const userInput = 'kittens'\nconst searchResult = 'Kittens'\n\nconsole.log(userInput == searchResult)\nlog: false<\/pre><\/div>\n\n\n\n<p>This way of programming a comparative search would yield no results. We know that the internet is full of kitten videos, so let\u2019s disregard case sensitivity and return the results the user meant to search for.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const userInput = 'kittens'\nconst searchResult = 'Kittens'\n\nconsole.log(userInput.toUpperCase() === searchResult.toUpperCase())\nlog: true<\/pre><\/div>\n\n\n\n<p>Now that we have converted both strings to be all uppercase letters after input, we can return the accurate search results. The same result can be accomplished by converting both strings to all lowercase letters.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const userInput = 'kittens'\nconst searchResult = 'Kittens'\n\nconsole.log(userInput.toLowerCase() === searchResult.toLowerCase())\nlog: true<\/pre><\/div>\n\n\n\n<p>This example is meant to show the comparative logic behind how a search form may be written. There are many more methods and operators available for JavaScript strings and can be found <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this introduction to JavaScript strings, we have briefly discussed what a string is and a few ways to work with them. The subject of strings in JavaScript is a broad one. Understanding what a string is can be a simple task, yet the included methods to work with strings is extensive.<br><\/p>\n\n\n\n<p>After understanding the concepts in this article, using the above guide to further explore string methods will make more sense. JavaScript strings are essential data types to understand in order to make dynamic, robust web apps.<\/p>\n","protected":false},"excerpt":{"rendered":"A string in JavaScript, as is the case in many other programming languages, is a data type that holds data in text form. A common use for strings in the context of a web application is holding a user\u2019s input from a form. Taking an example of a search form, the search query is stored&hellip;","protected":false},"author":104,"featured_media":12811,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-28461","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>An Introduction to Strings in JavaScript | Career Karma<\/title>\n<meta name=\"description\" content=\"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/javascript-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to Strings in JavaScript\" \/>\n<meta property=\"og:description\" content=\"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/javascript-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-15T05:58:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-20T12:52:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"663\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ryan Manchester\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ryan Manchester\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/\"},\"author\":{\"name\":\"Ryan Manchester\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/92fd52a503f77fc058ec2d0666da9bd5\"},\"headline\":\"An Introduction to Strings in JavaScript\",\"datePublished\":\"2021-01-15T05:58:22+00:00\",\"dateModified\":\"2021-01-20T12:52:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/\"},\"wordCount\":548,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/code-coding-computer-developer-574080.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/\",\"name\":\"An Introduction to Strings in JavaScript | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/code-coding-computer-developer-574080.jpg\",\"datePublished\":\"2021-01-15T05:58:22+00:00\",\"dateModified\":\"2021-01-20T12:52:50+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/92fd52a503f77fc058ec2d0666da9bd5\"},\"description\":\"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/code-coding-computer-developer-574080.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/code-coding-computer-developer-574080.jpg\",\"width\":1000,\"height\":663},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-strings\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"An Introduction to Strings in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/92fd52a503f77fc058ec2d0666da9bd5\",\"name\":\"Ryan Manchester\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ryan-manchester-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ryan-manchester-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ryan-manchester-150x150.jpg\",\"caption\":\"Ryan Manchester\"},\"description\":\"Ryan is a technical writer at Career Karma, where he covers programming languages, technology, and web development. The Texas native earned his Bachelor's of Music Composition from the University of North Texas. Ryan is currently pursuing further education in web development, aiming to graduate from Flatiron School with a certification in full stack web development. Since joining the Career Karma team in November 2020, Ryan has used his expertise to cover topics like React and Ruby on Rails.\",\"sameAs\":[\"http:\\\/\\\/www.ryanmanchester.info\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/ryan-manchester-6537a630\\\/\"],\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/ryan-manchester\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"An Introduction to Strings in JavaScript | Career Karma","description":"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/javascript-strings\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to Strings in JavaScript","og_description":"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.","og_url":"https:\/\/careerkarma.com\/blog\/javascript-strings\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-01-15T05:58:22+00:00","article_modified_time":"2021-01-20T12:52:50+00:00","og_image":[{"width":1000,"height":663,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg","type":"image\/jpeg"}],"author":"Ryan Manchester","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Ryan Manchester","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/"},"author":{"name":"Ryan Manchester","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/92fd52a503f77fc058ec2d0666da9bd5"},"headline":"An Introduction to Strings in JavaScript","datePublished":"2021-01-15T05:58:22+00:00","dateModified":"2021-01-20T12:52:50+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/"},"wordCount":548,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/javascript-strings\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/","url":"https:\/\/careerkarma.com\/blog\/javascript-strings\/","name":"An Introduction to Strings in JavaScript | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg","datePublished":"2021-01-15T05:58:22+00:00","dateModified":"2021-01-20T12:52:50+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/92fd52a503f77fc058ec2d0666da9bd5"},"description":"Strings in JavaScript are a data type that holds text as its value. In this introduction, learn about what a string is and common use cases.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/javascript-strings\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/code-coding-computer-developer-574080.jpg","width":1000,"height":663},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/javascript-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"An Introduction to Strings in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/92fd52a503f77fc058ec2d0666da9bd5","name":"Ryan Manchester","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/ryan-manchester-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/ryan-manchester-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/12\/ryan-manchester-150x150.jpg","caption":"Ryan Manchester"},"description":"Ryan is a technical writer at Career Karma, where he covers programming languages, technology, and web development. The Texas native earned his Bachelor's of Music Composition from the University of North Texas. Ryan is currently pursuing further education in web development, aiming to graduate from Flatiron School with a certification in full stack web development. Since joining the Career Karma team in November 2020, Ryan has used his expertise to cover topics like React and Ruby on Rails.","sameAs":["http:\/\/www.ryanmanchester.info\/","https:\/\/www.linkedin.com\/in\/ryan-manchester-6537a630\/"],"url":"https:\/\/careerkarma.com\/blog\/author\/ryan-manchester\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/28461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=28461"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/28461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/12811"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=28461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=28461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=28461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}