Explain Strings in JavaScript like I am 6
Strings in JavaScript

Explain Strings in JavaScript like I am 6

As a newbie in the world of programming, learning a new language like JavaScript can be quite challenging. I titled this article “Explain Strings in JavaScript like I am 6” as is one of the most known analogies when it comes to explaining basic concepts in simple principles.


In my own experience, I believe that one of the fundamental concepts in JavaScript is?strings.


In this article that I take from my own notes, I will share some of the important data types called strings in JavaScript from a newbie perspective. I

I will cover some key points that every beginner should know. So let’s go!!

What is a string?

A string in JavaScript is essentially a series of characters that can be a single letter, a phrase, or even a whole paragraph. It is wrapped up into a single (’)quotation or double quotation (”) mark, which tells JavaScript that everything between those marks is a string. For example, the string “JavaScript!” is a phrase made up of the characters J,a,v,a,S,c,r,i,p,t, and exclamation mark.

String in JavaScript for a 6 years kid explained:

When it comes to concepts, I like to explain them to my son when I play with him, but in his terms, as he is 6.

We can think of strings in JavaScript as a bracelet that has beads with letters on them. Just like the photo. When the beads are together they create a message, in our case, it is written “JavaScript”.

To conclude this analogy we could say that a string is a group of letters combined to create a specific message, and in programming, we use strings to communicate to the computer what we want to do.

4 types of string

However, when a word contained within the string has a quotation mark, like “Someone else’s”, it can interfere with the string and cause errors.

This is because the quotation mark is seen as the end of the string, and anything after it is seen as separate code.

For example, if we try to create a string like “I’m a programmer”, JavaScript will see the apostrophe as the end of the string and will throw an error.

To solve this issue, we have a few solutions. Today I will share the most used ones that I am aware of at this moment.

1. Wrapping double quotation marks.

The first is to use single quotation marks for the word that contains the apostrophe and wrap the whole phrase in double quotation marks. So, the example string would look like this: “I’m a ‘programmer’”.

This way, JavaScript knows that the word ‘programmer’ is still part of the string and won’t cause any errors.

2. Backslash.?The second solution is to use an escape character, which is a backslash () followed by the character you want to escape. In our previous example, we could write the string as “I’m a programmer”, which tells JavaScript to treat the apostrophe as part of the string rather than the end of it.

3. The escape character. Using a traditional string, we would need to use the escape character and write the string as “Someone\nElse”. However, with Template Strings, we can write the string as `Someone Else` The \n will not only be much more readable and easier to work with, but it will also add a break and our code will be like this now:

”Someone

Else”

No alt text provided for this image
Strings

4. Template Strings.?I have left the 4th solution as the last, as it is my favorite one. This is to create strings in JavaScript by using Template Strings.

Template Strings are created by wrapping the string with a backtick (`) character. These are more versatile than traditional strings, as they can be used to create multi-line strings without using escape characters. For example, if we wanted to create a string that said: “Someone Else”.

No alt text provided for this image
Template Strings


Recap:

A string is a single character: a single letter, phrase, or even paragraph.

  • It is wrapped up into a single quotation or double quotation
  • When a word contained within the paragraph or phrase has a quotation it will interfere with the string as its matches the string.
  • Examples of solutions are to use 1) a?single?quotation?for the word with the phrase wrapped in the double quotation, or 2) using?escape.
  • A backslash followed by n will make a break the word like this:

someone

else

  • The most versatile solution is the use of the Template Strings that consist of a backtick (`)

I hope you enjoyed reading this article originally written on my blog.

Is this helpful for you? Why not subscribe to my newsletter and receive an e-mail with all the articles I write about coding, and dev things, mainly JavaScript?

Subscribe to the monthly Newsletter



要查看或添加评论,请登录

Luc Constantin的更多文章

  • 8 Keys Factors to improve SEO

    8 Keys Factors to improve SEO

    #seo #contentwriting #contentcreation #webdevelopment Article originally written on https://blog.accolades.

  • Career Timeline’s Power

    Career Timeline’s Power

    The Power of a Career Timeline: A Late Starter’s Guide to Success in the Tech Industry Article originally written on…

    1 条评论
  • 3 Keys Introduction To Objects Arrays

    3 Keys Introduction To Objects Arrays

    If you are a web developer, and I assume you are since you landed here reading this, you know that one of the most used…

    1 条评论
  • 3 empowering Keys to enhance your Captcha Knowledge

    3 empowering Keys to enhance your Captcha Knowledge

    Have you wondered what CAPTCHA stands for? It stands for Completely Automated Public Turing test to tell Computers and…

  • 3 Keys in JavaScript Arrays

    3 Keys in JavaScript Arrays

    If you are new to programming and you are learning JavaScript, or if you already have met JavaScript you know the term…

  • Boost Your Profit with Google Local Business

    Boost Your Profit with Google Local Business

    As someone who aims to assist small businesses in enhancing their online visibility, I frequently suggest creating a…

  • Math ceil()in Javascript

    Math ceil()in Javascript

    Some time ago, I finished one of the Javascript projects from Chris Dixon’s series Math ceil()in Javascript is a very…

    1 条评论
  • I am a Slow Learner, is that bad?

    I am a Slow Learner, is that bad?

    Since I was a small kid I remember struggling to learn, so I guess that makes me a slow learner. The learning path for…

  • I am bad at coding, please help!

    I am bad at coding, please help!

    How many times you haven’t felt this way? How many times you’ve looked at your code and had the feeling of being the…

  • Start Your Freelance Journey in Four Easy Steps

    Start Your Freelance Journey in Four Easy Steps

    Freelance is scary, but it’s also freedom. Of course, there are already a plethora of articles, blogs, videos, and…

社区洞察

其他会员也浏览了