STRINGS IN JAVASCRIPT BLOG
Published: 2023-01-27 12:51:55

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. In JavaScript strings are used for storing and manipulating text.
In this blog, I am going to first take a look at how to create strings and we’ll see some concept that apply to strings.

Creating Strings
There are three ways to write a string:
Let’s assign some strings variable with each technique:

with single quotes ‘ ’
              let text = “HELLO WORLD”;
double quotes “ ” 
              let text = ‘HELLO WORLD’;
back-ticks 
             let text = `HELLO WORLD`;
There is no syntactic difference between single or double quotes, you’re free to go with your preference.

Storing a String in a Variable
Variables in JavaScript are named containers that store a value, using the keywords var, const or let. We can assign the value of a string to a named variable.

const newString = "This is a string assigned to a variable.";
Now that the newString variable contains our string, we can reference it and print it to the console.
console.log(newstring);
This will output the string value.
Output
This is a string assigned to a variable.

By using variables to stand in for strings, we do not have to retype a string each time we want to use it, making it simpler to work with and manipulate strings within the programs.




String Concatenation
Concatenation means joining two or more strings together to create a new string. In order to concatenate, we use the concatenation operator, represented by a + symbol. The + symbol is also the addition operator when used with arithmetic operations.

Let’s create a simple instance of concatenation, between "Sea" and "salt".
"Sea" + "horse";

Output
Seahorse

Below is how to join strings and variables containing string values with concatenation.
const poem = "The Wide Ocean";
const author = "Pablo Neruda";
const favePoem = "My favorite poem is " + poem + " by " + author + ".";

Output
My favorite poem is The Wide Ocean by Pablo Neruda.

The Anatomy of a String
Each character in a string is numbered, starting at 0. For example:
“HELLO WORLD”



The first character is H  at index position 0.
The last character is O at index position 4.
The space also has index positions at 5.
We can access any character like so:

   “HELLO WORLD [6]”  //W

Finding the Length of a String

To find the length of a string we can use the length property. It returns the number of characters in a string.
 “HELLO WORLD ”.Length           //10

The length property returns the actual number of characters starting with 1, which in our example is 10, not the final index number, which starts at 0 and ends at 9.

Converting Strings into Numbers

let's take a look at some of the ways we can convert strings into numbers.
parseInt()
To convert a string into an integer (a whole number) I can use parseInt(). It takes two arguments, the first being the value I want to convert, and the second is called the radix. This is the base number used in mathematical systems. For our use, it should always be 10, the decimal system.

parseInt(‘64’, 10);        // 64
parseInt(‘64.9’, 10);     // 64
parseInt(‘64px’, 10);    // 64

parseFloat()
If I wish to convert into a floating-point number (a number with decimal points), I’d use parseFloat().
 
parseFloat(‘64.9’);                          // 64
parseFloat(‘64bit’);                        // 64
console.log(parseFloat (‘64’));      //64

Number ()
You can convert my strings with the Number() method as well, however, it's stricter about which values it can convert. It only works with strings that consist of numbers.

Number(‘64’);              //64
Number(‘64.9’);           //64.9
Number(‘64bit’);          //NaN
Number(‘64%’);           //NaN

Conclusion
In this blog, I went over the basics of working with strings in JavaScript, from creating and displaying string literals using single and double quotes and back-ticks, concatenation, The Anatomy of a String, Finding the Length of a String and Converting Strings into Numbers. I am hoping that this blog will benefit someone.

Thank you!