Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
Javascript guide Strings JavaScript

03. Javascript Guide – Strings

  • July 3, 2021August 1, 2021
  • by Coder Kai

Every programming language has strings that represent a set of characters. Javascript strings have some properties which are unique to the language. This guide covers these properties and important facts when using strings.

1. What are strings?

In javascript, texts which can be assigned to a variable can be defined as strings. A string is

  1. Immutable sequence
  2. Sequence is ordered.
  3. Values of the sequence are 16-bit memory spaces, unicode characters.

The length of the string is how many these 16 bit values are present in the array.
Immutables means that when one string is defined that string cannot be mutated and the next one will be a next string. For example: if you define "hello" and then "hello world", both of those memory locations will be separately defined in the memory space.

"hello ".concat("world"); // output => "hello world"
// this doesnt change the orginal "hello " string and it will define a new string "hello world"

A string is a sequence 16-bit memory allocation and follows the standard of UTF-16. These strings are declared in javascript by enclosing between "",``, ''

Examples

"hello" === 'hello' === `hello`
`hello ${user.firstName}!`
"Hi, Foo"
"two\nlines"
'string "inside string"'
"" // empty string length is 0
"e\u0301" // é

After ECMA5 you can add multiline strings with the backslash(\)

"hello \
how are you" // output => "hello how are you"

"hello
how are you" // output => Uncaught SyntaxError: Invalid or unexpected token

Following is the representation of an ordered sequence of a string

Javascript strings

2. Surrogate Pairs

When we represent a character with 16 memory spaces there is a limit to which we can represent the characters(65,536). The characters which are in the basic multilingual plane can be represented in this 16 bits. This cannot represent all the characters in the language around the world and other special characters.

For that UTF-16 will extend the next set of the sequence and create a string of length 2.
Example

const a = "M"

const b = "e\u0301" // é

a.length // 1

b.length // 2

Hence this allows javascript to support all Unicode characters in when programming with strings.

3. String operations

You can perform many operations when programming with strings. Javascript support almost all of the string operations provided by the other popular languages.

Concatenate

"how " + "are you?";
"how ".concat("old are you?"); // string prototype function concat

Character string

We can use characters in the sequence by directly accessing it as an array variable.

const message = "hello ";
message[0]; // h

Unlike programming languages like java, it won’t represent a single character or special character type to represent a sequence of characters or a similar approach in javascript.

Split strings

You can split an array of string by using the prototype function split.

"John, Foo".split(","); // ['John', ' Foo']

Escape literals

Another important operation is escaping the literals for special characters and literals. Following list show some of the common usages.

\n - new line  
\b - back space 
\v - vertical tab 
\t - horizontal tab 
\" - double quotes
\0 - End of character or nul character
\x - Latin 1 charcters 2 hexadecimal digits
\u - Unicode characters with 4 hexadecimal digits

4. String wrapper object

String is a wrapper object for string literals in Javascript. You can create a string also using String constructor.

let hello = String("Hello world"); // => Hello world
hello.toUpperCase();w

The toUpperCase() the function is a prototype function that is provided by the String object.
When you call a method on a literal, temporary wrapper object of String is creating.

'hello'.toUpperCase();

You can find more information regarding String object in following reference.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

5. Regular Expressions

Regular expressions in javascript are handled with RegExp object. This is not a fundamental type object in javascript. But we can define a regexp using literal syntax and create a wrapper object directly when initiating. A RegExp can be paired in between two slashes as \expression\

Examples

let exp1 = /\w+/
let exp2 = new RegExp('\\w+')

let re = /(\w+)\s(\w+)/
let str = 'John Smith'
let newstr = str.replace(re, '$2, $1') // printes "Smith John"

Regular expressions are very important when doing string-related operations. As a programmer, you will be able to simplify many string operations if you know how to handle regular expressions properly in any language.

This covers most of the important aspects of Strings in javascript.
You can also refer to the previous tutorial on numbers to learn more about javascript language

02. Javascript Guide – Numbers

How to fetch data with useEffect
How to dismiss dropdowns when pressed on background with React.js
Coder Kai
A humble developer
javascript strings javascript tutorial strings Surrogate pairs

Related articles

Multiple refs for an array of React Elements
Using multiple refs for an…
Immutable and Mutable Values in Javascript
07. Immutable and Mutable Values…
wrapper objects in javascript
06. Wrapper objects in Javascript
globals undefined and null values in javascript
05 Global, null and undefined…
Javascript Booleans and Equality
04. Javascript Guide Booleans and…
How to add Chakra UI Animations
Chakra UI Animations
SSL Websocket using Nginx Proxy
SSL Websocket proxy with Nginx…
Change python version correctly
Python is not setting correct…
optimize React.js load time
How to optimize React.js app…
Multiple refs for an array of React Elements
How to use IntersectionObserver to…
Multiple refs for an array of React Elements
How to dismiss dropdowns when…
How to fetch data with useEffect
How to fetch data with…
add styles to stripe elements
How to add styles to…
Typescript
How to use Typescript with…
how to optimize react-native map view
How to optimize react-native map…
debounce with react hooks
Avoid multiple clicks using debounce…
Numbers inJavascript
02. Javascript Guide – Numbers
Introduction to Javascript
01. Javascript Guide – Introduction…
Nginx Load Balancer with docker…
Create React App Using npx
  • Pingback: 04. Javascript Guide Booleans and Equality - Sandny Blog()

  • Pingback: Globals undefined and null values in javascript when programming()

  • Pingback: Wrapper objects in Javascript and access properties of literals()

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance account on Minimum supported Gradle version is 7.0. Current version is 6.5
  • binance us registracija on How to add styles to stripe elements without using CardElement
  • binance Registrera dig on Higher order component wrappers using React.js and reusable components.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress