Isomorphic JavaScript strings
A string is a fundamental data type used to handle text in JavaScript. For instance, when we initialize a string in JavaScript it looks like this: const name = ‘John’;
Of course, that won’t make much sense unless you have a good understanding of isomorphic Javascript strings. Keep reading for further explanation of what JavaScript strings are and code examples to help you visualize them.
Creating JavaScript strings
Strings are constructed in JavaScript by enclosing characters with quotes. You can utilize quotes in one of three different ways:
- Single quotes: ‘Hello’
- Double quotes: “Hello”
- Backticks: `Hello`
// strings example:
const name = ‘Peter’;
const name1 = “Jack”;
const result = `The names are ${name} and ${name1}`;
The single or double quotation format can be used interchangeably in most cases. You can also add variables or expressions in a string with the help of backticks. As demonstrated above, this is accomplished by enclosing the relevant variables or expressions in ${variable or expression}.
Accessing string characters
After creating JavaScript strings you may need to access them. There are two methods for accessing a string’s characters:
Strings as an array method
Considering strings as an array is one option. Take the following as an illustration:
const a = ‘hello’;
console.log(a[1]); // “e”
The charAt() method
You can also use the method charAt() to access strings. Using the charAt() method would look like:
const a = ‘hello’;
console.log(a.charAt(1)); // “e”
JavaScript strings that are immutable
In JavaScript, string values cannot be changed in any way, meaning they are immutable. This indicates there’s no way to alter the characters that make up a string.
For example:
let a = ‘hello’;
a[0] = ‘H’;
console.log(a); // “hello”
Alternatively, you have the option of assigning the name of the variable to a new string.
For example:
let a = ‘hello’;
a = ‘Hello’;
console.log(a); // “Hello”
JavaScript multiline strings
There are also JavaScript multiline strings. You can use either the + operator or the \ operator when working with multiline strings.
For example:
// using the + operator
const message1 = ‘This is a long message ‘ +
‘that spans across multiple lines’ +
‘in the code.’
// using the \ operator
const message2 = ‘This is a long message \
that spans across multiple lines \
in the code.’
Case-sensitive JavaScript
JavaScript is a case-sensitive language. Therefore, lowercase and uppercase letters are treated differently in JavaScript.
For example:
const a = ‘a’;
const b = ‘A’
console.log(a === b); // false
In this above example, ‘a’ and ‘A’ are considered to be different.
Final word on isomorphic JavaScript strings
The explanations and examples should hopefully help you understand the concept of strings in isomorphic JavaScript. One thing to always keep in mind with isomorphic JavaScript is that the code is going to be the same on the client side and server side.
If you’re looking for SEO project management software to better manage your workflow, clients, and business – evisio.co is your solution. Try evisio.co for free here!