1671518766
JavaScript provides many built-in methods for working with strings. Here is a list of some common string methods:
charAt()
: Returns the character at the specified index in a string.
concat()
: Combines two or more strings and returns a new string.
indexOf()
: Returns the index of the first occurrence of a specified value in a string.
lastIndexOf()
: Returns the index of the last occurrence of a specified value in a string.
length
: Returns the length of a string.
slice()
: Extracts a part of a string and returns a new string.
split()
: Splits a string into an array of substrings.
substring()
: Extracts the characters from a string between two specified indices and returns a new string.
toLowerCase()
: Converts a string to lowercase letters.
toUpperCase()
: Converts a string to uppercase letters.
trim()
: Removes whitespace from the beginning and end of a string.
Here is an example of how you might use some of these methods:
let str = "Hello World!";
// Returns the character at the specified index
console.log(str.charAt(0)); // "H"
// Combines two strings and returns a new string
console.log(str.concat(" How are you?")); // "Hello World! How are you?"
// Returns the index of the first occurrence of a specified value
console.log(str.indexOf("o")); // 4
// Returns the index of the last occurrence of a specified value
console.log(str.lastIndexOf("o")); // 7
// Returns the length of a string
console.log(str.length); // 12
// Extracts a part of a string and returns a new string
console.log(str.slice(6, 11)); // "World"
// Splits a string into an array of substrings
console.log(str.split(" ")); // ["Hello", "World!"]
// Extracts the characters from a string between two specified indices and returns a new string
console.log(str.substring(6, 11)); // "World"
// Converts a string to lowercase letters
console.log(str.toLowerCase()); // "hello world!"
// Converts a string to uppercase letters
console.log(str.toUpperCase()); // "HELLO WORLD!"
// Removes whitespace from the beginning and end of a string
console.log(" Hello World! ".trim()); // "Hello World!"
I hope this helps! Let me know if you have any questions. Strings and string methods are complex, therefore practising them is the best way to learn. String applications and method uses are limitless. Lastly you can always hire Javascript Developers for in depth knowledge of String methods.
1624399200
JavaScript Strings
đș The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=09BwruU4kiY&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=6
đ„ If youâre a beginner. I believe the article below will be useful to you â What You Should Know Before Investing in Cryptocurrency - For Beginner
â â âThe project is of interest to the community. Join to Get free âGEEK coinâ (GEEKCASH coin)!
â **-----CLICK HERE-----**â â â
Thanks for visiting and watching! Please donât forget to leave a like, comment and share!
#javascript #strings #javascript strings #javascript strings tutorial
1602900514
String methods in JavaScript help you to work with strings, mastering those methods is a good idea, because a lot of times you will find yourself working with strings in your JavaScript program or application, So you will need to know those methods. Thatâs why in this article I decided to show you 10 useful string methods that maybe you didnât know some of them.
Photo by Irvan Smith on Unsplash
The **length**
property returns the length of a string. Take a look at the example below:
The Length method.
You can search for a string inside another string using the search method, It will return the position of that string. Have a look at the example below:
The Search method.
#javascript #web-development #javascript-string #javascript-tips
1590478483
The easiest approach to use javascript built-in method String.split().
#javascript #javascript string #string to array #morioh #array
1669207080
JavaScript lastIndexOf() method explained with examples
The JavaScript lastIndexOf()
method is a method of JavaScript String
and Array
prototype objects.
The function is used to return the index (position) of the last occurrence of the given argument.
The index of both JavaScript strings and arrays start from 0
, and here are some examples of using the lastIndexOf()
method:
// A string
let aString = "Good morning! It's a great morning to learn programming.";
let index = aString.lastIndexOf("morning");
console.log(index); // 27
// An array
let anArray = ["day", "night", "dawn", "day"];
let valueIndex = anArray.lastIndexOf("day");
console.log(valueIndex); // 3
As you can see from the examples above, the lastIndexOf()
method is available for string
and array
type values in JavaScript.
When the value you search for is not found in the string or array, the method will return -1
:
let aString = "Nathan Sebhastian";
let index = aString.lastIndexOf("morning");
console.log(index); // -1
let anArray = ["day", "night", "dawn", "day"];
let valueIndex = anArray.lastIndexOf("dusk");
console.log(valueIndex); // -1
The lastIndexOf()
method is also case sensitive, so you need to have the letter case in the argument match the actual string.
The above example returns -1
because Nathan
is different from nathan
:
let aString = "Nathan Sebhastian";
let index = aString.lastIndexOf("nathan");
console.log(index); // -1
The lastIndexOf()
method will search your string or array backward.
This means the method will look for matching values from the last element and ends with the first element.
Finally, the method also accepts a second parameter to define the start position of the search.
The following example shows how to start the lastIndexOf()
search at index 4
:
let aString = "Hello World!";
let index = aString.lastIndexOf("World", 4);
console.log(index); // -1
Because a second argument 4
is passed to the lastIndexOf()
method, JavaScript will search the value from index 4
to index 0
only.
While the World
string is available inside the aString
variable, the index starts at 6
. Since itâs outside the search range of the lastIndexOf()
method, the value is not found and -1
is returned.
The same rule applies when you search for a value in an array:
let anArray = [8, 3, 4, 8, 2];
let index = anArray.lastIndexOf(8, 2);
console.log(index); // 0
In the abov example, the lastIndexOf()
method returns 0
for the index of the value 8
.
This is because the second argument 2
makes the method search from index 2
only. Since the last index of the value(8
) is 3
, it stays outside of the method search range.
And thatâs how the lastIndexOf()
method works in JavaScript. đ
Original article source at: https://sebhastian.com/
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, âdocument.writeâ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript