Hey Welcome, Techies👋🏼
Hello everyone, my name is Aryan Sharma, and I hope you're all doing well.
I'm thrilled to begin this JavaScript blog on Strings in JavaScript for absolute beginners.
You can find my other blogs on JavaScripthere↗️
Javascript Strings
Unlike strings in C, C++; character is stored in a string with quotes ""
The Quotes
A string in JS is the character written inside quotes.
Strings can be enclosed within either single quotes, double quotes, or backticks:
let name = 'Aryan Sharma';
let first_name = "Aryan";
const age = 19;
console.log(`I am ${age} years new 😁`); // Template string
Template Strings
Templates were introduced with ES6. Templates are strings enclosed in backticks.
Why do we use this?
To introduce the mathematical operation in the Javascript statement with dollar sign ${}
.
let marks = 95;
let total_marks = 100;
console.log(`I got ${marks} out of ${total_marks`}
Special characters
There are some other characters like newline operator\n
which denotes a new line or a line break simply.
let Full_name = "A\n * B\n * C\n * D";
alert(guestList);
Output:-
A
B
C
D
There are some more characters which are :
\r
\\
\t
\b
\f
\v
You have homework to study about each of them on the Internet 🔎
Length of a String
We can also find the length of a string using the in-built length
property.
let name = "Aryan Sharma";
let length = name.length;
Output
>> 12
**I am expecting you to know about arrays
If not ...**
Accessing characters
We use [] to access characters in a string.
Such as a string name
"Aryan
"
then, name[0] = A
name[1] = r
name[2] = y
name[3] = a
name[4] = n // 1 can be said as name.length() - 1
We can also say .at(0).at(1).at(2).at(3)
...
Also, .at(-1)
means the last character, and .at(-2)
is the one before it, etc.
⚠ But we can not use a negative index inside [brackets], it returns undefined
Properties and methods of string
Methods
These Javascript methods are predefined to the browser of our system.
.uppercase
console.log(Aryan.toUpperCase()); //ARYAN
.lowercase
console.log(Aryan.toLowerCase()); //aryan
Immutable
Unlike, arrays and other data, we can not change the character of a string.
Let’s try it to show that it doesn’t work:
let str = 'Gujarat';
str[5] = 'o'; // error
console.log(str[5]); // doesn't work // a
Comparing
You might have to compare strings in your program or need to do so.
Let's understand how to do
A lowercase
alphabet is always greater than the upper one.
alert( 'a' > 'Z' ); // true
you might be thinking how can we compare two characters, they are not even in number, and how did a > A
.
Here's a thing called ASCII values: you might have heard about it otherwise read here
So, the uppercase letter 'A' is 65, while the lowercase letter 'a' has an ASCII value of 97.
If you want to know the numbers of other alphabets, 👇🏼
console.log(fromCodePoint(a)) // this function will tell the value of a
And this is to this blog! That's the ending for this one!
Let's meet at another one!
I Hope you loved this blog 💖💖
Do not forget to follow me!