1. javaScript

Strings in JavaScript

In JavaScript, a string is an immutable ordered sequence of 16-bit values where each of bit represents a Unicode Character and combination of these strings is used for representing text.

If in case you don’t know about Unicode Characters then head over to Wikipedia to learn these – Unicode Characters Wikipedia.

Combination of many strings make Text in JavaScript.

Escape Character inside strings in JavaScript

JavaScript interprets words inside single quotes(‘ ‘) or double quotes(” “) as a single string. But if in case you want to put an apostrophe, for example – “You’re right”. This sentence would not be interpreted as You’re right rather JavaScript would think this as –

You re right

Which is not what programmer have intended. This is just an example, there does exist many other this kind of scenarios.

In order to espace these kind of misinterpretation, JavaScript uses Backslash Character(\) which combined with other character that it follows represent a character that is not otherwise representable within a string.

Like for above listed example – Rather than “You’re right” as a JavaScript Developer you should write “You\’re right”.

Intended – You’re right. JavaScript thinks – You(newline) re right Right approach – You\’re right

Backslash could be really useful in certain scenarios, but JavaScript have certain other internally encode characters which start with backslash. Please keep in mind that which using these interpretation would be totally different as backslash being only escape character. Here are some of these Backslash characters which have different meaning then just escaping quotes in javaScript.

SequenceInterpreted As – By JavaScript
\0Null Character
\t Horizontal tab
\vVertical tab
\fForm feed
\rCarriage return
Special Escape Characters in JavaScript

Working with Strings in JavaScript

As is the case with any other programming language, usually we need to add/cut or do a lot of things with variables declared inside code. For doing this stuff, atleast for programming languages which I’ve learned so far Syntax seems kinds of common. Here are some of operations which can be done in JavaScript –

coding = "Computer Science Hub";
coding.length; (20) //would return length of "Computer Science Hub"

coding.chartAt(0); (C) //would return character at position zero in string

coding.substring(1, 3); (om) //would return string of characters starting from position 1 to position 3(Not including it) 

coding.slice(1,3); (om) //kind of similar as .substring

coding.toUpperCase(); (COMPUTER SCIENCE HUB) //would change whole string to uppercase letters

These are some of common operations which can be performed on strings in JavaScript. Despite these there’re a lot of other methods which could be called on string object in JavaScript. But don’t worry about knowing all of those other methods, for time being just learn these and over time you would automatically pick those up, as you become more experienced in programming.

JavaScript String Pattern Matching

Doing Pattern Matching is what I do kind of each day, while writing code. Either it be comparing two texts or finding something out of large text(thousands of words) Pattern Matching is the way to go.

For specifically in JavaScript, it’s internally built in for Strings. Meaning Strings have certain methods which could take a Regular Expression in and process it. Below are some examples showing Pattern Matching for Strings in JavaScript –

Note – If in case your’s not aware of how to make a pattern or Regular Expression, then you’re in trouble as this is one of most important aspect of programming. So better learn it, Regexone has awesome free tutorial for this you can check it out – Introduction to Regex.

  • /^Programming/ (Match each letter of Programming word at the start of a string)
  • /[1-9][0-9]*/ (Match a non-zero digit, followed by any number of digits)
  • var text = “testing: 1, 2, 3”; (Sample text)
  • var pattern = /\d+/g; (Pattern matching all occurring of one or more digits)
  • pattern.text(text); (Returns => true, meaning there does exist a match)
  • text.search(pattern); (Returns => 9, position of first matching)
  • text.match(pattern); (Returns => [“1”, “2”, “3”] all matches characters)
  • text.replace(pattern. “Computer”); (Returns => “testing: #, #, #”)
  • text.split(/\D+/); (Returns => [“”, “1”, “2”, “3”] split string on non-digits)


Dealing with strings in JavaScript aren’t that complicated, you as a Developer just keep in mind the fundamentals of language which I’ve described above. Moreover if incase you just want to clarify something or may be what to ask a question, then comment section is open and your more than welcome to ask me questions. I would reply as soon as possible.

Also if you’re learning JavaScript then I do have other articles about different aspects of language and how to become proficient in coding it, you can check these out here – JavaScript Computer Science Hub.

Comments to: Strings in JavaScript

Your email address will not be published.