JavaScript Basic String Functions
K-Sato

K-Sato @k_penguin_sato

About: Software Engineer

Joined:
Nov 14, 2018

JavaScript Basic String Functions

Publish Date: Nov 13 '19
42 37

Here are some of the most fundamental JS string functions.

Assume the code below is declared at the top level in the following examples.

var string = "string"
Enter fullscreen mode Exit fullscreen mode

Changing the case

toLowerCase()

Converts a string to lowercase letters.

let s6 = "STRING".toLowerCase();
console.log(s6); //=> "string"
Enter fullscreen mode Exit fullscreen mode

toUpperCase()

Converts a string to uppercase letters.

let s7 = string.toUpperCase();
console.log(s7); //=> "STRING"
Enter fullscreen mode Exit fullscreen mode

Searching for a substring

indexOf(subst, pos)

It looks for the substr in a string, starting from the given position pos.

console.log(string.indexOf('i')) //=> 3

let string2 = 'string string'

console.log(string2.indexOf('string', 2)) //=> 7
Enter fullscreen mode Exit fullscreen mode

includes()

Checks whether a string contains the specified string/characters.

console.log(string.includes("s")); //=> true
Enter fullscreen mode Exit fullscreen mode

match()

Searches a string for a match against a regular expression, and returns the matches.

const regex = /\w/g;
let s4 = string.match(regex);
console.log(s4); //=> ["s", "t", "r", "i", "n", "g"]
Enter fullscreen mode Exit fullscreen mode

Getting a substring

slice()

Extracts a part of a string and returns a new string.

let s2 = string.slice(0, 3); ///=> str
console.log(s2);
Enter fullscreen mode Exit fullscreen mode

substring()

Extracts a part of a string and returns a new string.

let s2 = string.substring(0, 3); ///=> str
console.log(s2);
Enter fullscreen mode Exit fullscreen mode

Transforming a string

split()

Splits a string into an array of substrings.

let s1 = string.split("");
console.log(s1); //=> ["s", "t", "r", "i", "n", "g"]
Enter fullscreen mode Exit fullscreen mode

concat()

Joins two or more strings, and returns a new joined strings.

let s3 = string.concat("string");
console.log(s3); //=> stringstring
Enter fullscreen mode Exit fullscreen mode

replace()

Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

let s5 = string.replace(/s/, "S");
console.log(s5); //=> String
Enter fullscreen mode Exit fullscreen mode

trim()

Removes whitespace from both ends of a string.

let s8 = " strn g   ".trim();
console.log(s8); //=> strn g

let s9 = "  string  ".trimLeft();
console.log(s9); //=> "string  "

let s10 = "  string    ".trimRight();
console.log(s10); //=> "  string"
Enter fullscreen mode Exit fullscreen mode

References

Comments 37 total

  • Alicia Black
    Alicia BlackDec 23, 2019

    Strings are an important part of any programming language, and JavaScript has many powerful built-in functions that make it easy for developers to use strings. I am also a developer and associate with Masters Dissertation Help UK.

  • Amelia
    AmeliaSep 3, 2020

    Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up.

    Visit: العاب كرتون نتورك بالعربية

  • Cristina Merry
    Cristina MerryJan 1, 2021

    Hi this is very instrumental for us to read this post! Being a QC officer at
    Write essay for me UK. This adds value to our knowledge since I am following this platform I read an article / Post daily to learn new things daily.

  • Susan Benjamin
    Susan BenjaminJan 15, 2021

    I heads off to the java scrip developer, how they make a website because I faced a lot of issues when I working on Law Essay writer, I think The string arrangement of JavaScript variables has been continuously agonizing. No more, JavaScript Template Literals and Tagged Templates will save a great deal of your time

  • harrywilliams1
    harrywilliams1Feb 23, 2021

    String functions are used in computer programming languages to manipulate strings or query information about strings (some functions will be executed at the same time). Most programming languages with string data types will have some string functions. Source: professional assignment writing UK by British Essay Writers.

  • elmadavis
    elmadavisMar 26, 2021

    Is there any replace function used in Java? These mentioned methods or functions are useful. I need assignment writing help from any developer here I need to use these functions. Can we use them with individual data types like int, var, char rather than a string?

  • Myprivate Mentor
    Myprivate MentorApr 5, 2021

    I was just tasked to replace function using java while seeking for assignment writing help . Thanks for this information as it is really very helpful.

  • OliviaSmith21
    OliviaSmith21Apr 10, 2021

    After high school, I started learning Java from a local institute. Today I am in another country to pursue the degree course of Java. But I am unable to access some of the websites due to restrictions. Someone suggested taking a VPN between Hola VPN Vs TunnelBear to get the access of restricted websites.

  • kittydurgans
    kittydurgansApr 14, 2021

    This is needed! slope unblocked

    • Jasmine
      Jasmine Jan 16, 2023

      Having a basic understanding of java can come in handy a lot of times in life. Thank you for sharing this with us. w3school is a life saviour! Coffee beans Ireland

  • kittydurgans
    kittydurgansApr 14, 2021

    Yep, this... Really, this is the only thing I'm missing... really like it otherwise. cookie clicker

  • arial hales
    arial halesMay 5, 2021

    I know a little bit know about programming recently I was working on the website and perform some functions to create the structure of vogueproperties and have a great experience of creating the website and grow skills.

  • Arnold Miler
    Arnold MilerJun 2, 2021

    With over ten years of experience servicing clients nationally, we feel we are the Top Digital Marketing Company. We use digital platforms to share your story and bind your brand to clients, boosting your company and assisting you in reaching your full online potential.

  • shaikhaliictsystem
    shaikhaliictsystemJun 3, 2021

    Javascript string functions. To begin with, a string is an object in javascript capable of storing records which is within the Software House In Karachi text layout. In easy phrases, a character array or a chain of letter or characters in javascript can be referred to as strings.

  • ChristinaBassett
    ChristinaBassettJul 1, 2021

    Javascript is a top programming language. In Java programming, the threads are treated as articles. There is only one cut-price essay writing have No.1 Uk dissertation writing service. Students can hire them at the Affordable price.

  • kumarpit47
    kumarpit47Jul 1, 2021

    Great work. So if you are looking to start a startup or looking for a business loan to start your software or application based business. Then you can always get in touch with zatpat loans. As they are one of the best private business loan lenders out there. Helping many business by offering them loans at a very minimal interest rate.

  • Tom
    TomJul 6, 2021

    Java is one of the most useful programming languages and the way you explain is very easy to understand. However, I don't have much interest in
    programming but whenever i need any assistance my first choice will be dissertation writing services .

  • Dr. Tutor Rex
    Dr. Tutor RexSep 11, 2021

    It is indeed helpful especially having the java knowledge in replacing functions. For instance when asked to solve Pyscho pharm assignment help it took me a short time to solve it.

  • farhadmoradik
    farhadmoradikOct 27, 2021

    Very useful tips. I have used some of these which building my best online marketing tools website. Thank you!

  • melissalevi
    melissaleviSep 7, 2022

    While you are working with the programming language one of the embroidery machine was having some issue for quality digitizing usa

  • alaxander7
    alaxander7Apr 18, 2023

    As truck breakdown services in Tallaght, we can help you out with 24 hours emergency call out for any transport vehicle repairs, we also offer logicfleet commercial fleet maintenance services and breakdown recovery to ensure you are 100% ready for your next job. truck breakdown services Tallaght

  • David
    DavidSep 25, 2023

    Javascript is a top programming language. Java is one of the most useful programming languages and the way you explain is very easy to understand. law essay writing service

  • Leon Wyatt
    Leon WyattSep 25, 2023

    Love coding with programming languages and solving real world problems! law dissertation writing services

  • ethan
    ethanOct 4, 2023

    Javascript is a top programming language. Java is one of the most useful programming languages and the way you explain is very easy to understand. best event companies in Dubai

  • Grace Amelia
    Grace AmeliaMay 29, 2024

    Computer programming languages utilize string functions to manipulate strings or query string-related data (some functions will be executed simultaneously). Source: UK-based professional writer for nursing essay help. Authors There are string functions in most computer languages that support string data formats.

  • Aliya Habib
    Aliya HabibAug 23, 2024

    Great blog post on JavaScript basic string functions! It was very informative and easy to follow, especially for beginners. I’m glad I found it while searching for information related to medical weight loss largo md. The clear examples really helped me understand how to use these functions effectively. Thanks for sharing such valuable content!

  • richard omusalala
    richard omusalalaSep 10, 2024

    Its very enjoyable to write the javascript codes especially when looking for assignment help from the experts

  • Absolute Digitizing
    Absolute DigitizingSep 12, 2024

    Absolute Digitizing offers professional embroidery digitizing services in USA, catering to businesses and individuals alike. Whether you need intricate designs for caps, uniforms, or promotional items, their team of skilled digitizers ensures that your design is accurately translated into stitch-ready files. By choosing expert digitizing services, you can guarantee that your embroidery projects will look sharp, professional, and durable.

  • Sarah Ayla
    Sarah AylaOct 13, 2024

    Thanks for this great post on JavaScript basic string functions! It was really helpful in breaking down some complex concepts into simple steps. By the way, if anyone is looking for the best brown leather jacket for men, you should definitely check out some recent trends online. Keep up the awesome work, this blog is a fantastic resource for beginners like me!

  • Botões Elite
    Botões EliteApr 30, 2025

    This is awesome always love to learn more about javascript when i was doing the website for botões elite i tried to learn some things but ended up on wordpress i guess i have to learn it more. thanks for sharing

Add comment