Introduction to JavaScript Strings
source link: https://dev.to/naftalimurgor/introduction-to-javascript-strings-fmb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Introduction
JavaScript Strings provide a way of representing and handling characters.
JavaScript Strings
A JavaScript string is a set of character(s) written inside quotes.
// with single quote
let emptyString = '' // an empty string
let name = 'Elon Musk'
// with double quotes
let project = "SpaceX"
Enter fullscreen mode
Exit fullscreen mode
Declaring a String in JavaScript does not restrict usage of single ''
and double quotes ""
Acess character
String objects provide a useful method for accessing a character in a string
let catName = 'Anita'
console.log(catName.charAt(0)) // prints 'A', character at a position 0
Enter fullscreen mode
Exit fullscreen mode
Strings behave like Array-like objects, so above can be:
let catName = 'Anita'
console.log(catName[0])// prints 'A'
// looping throug each character
for (let i = 0; i < catName.length; i ++) {
console.log(catName[i]) // A, n, i, t, a
}
Enter fullscreen mode
Exit fullscreen mode
Get length of a JavaScript String
const THREAD_NAME = 'Moonspiracy'
console.log(THREAD_NAME.length) // prints number of characters// 11
Enter fullscreen mode
Exit fullscreen mode
Summary
JavaScript Strings provide a way of presenting strings using double or single quotes. Both syntaxes are valid and usage is based on project style guide and preferences.
Recommend
-
106
Converting Strings to Number in Javascript: Pitfalls #javascript There are many ways to convert a String to a Number. I can th...
-
9
layout title date categories tags post Building Rails-compatible Query Strings in JavaScript 2020-04-01T13:57 javascrip...
-
13
A few days ago, I was working on building a countdown timer in JavaScript, and I needed to format my seconds and milliseconds. I wanted the seconds to always be a length of 2 digits and the milliseconds to always be a len...
-
10
(Relationally) Comparing Strings in JavaScript, WebMaster edition 26 Nov 2013 Alternate title, Learning The Abstract Relational Comparison Algorithm in 24 minutes or less or your money back guaranteed (with proof of purcha...
-
7
by Jan...
-
5
Calculate the hours between two date / time strings (YYYY-MM-DD HH: MM: SS) using Javascript advertisements I have two Time Stamps in the foll...
-
1
How to Uppercase Strings in JavaScript Sep 17, 2021 The built-in way of capitalizing strings in JavaScript is using the toUpperCase() function, which capitalizes the whole string.
-
12
When dealing with user-generated content, there's a high chance that you have to deal with strings full of Emojis. Emoji rendering can come with challenges, so you may want to detect when strings include Emojis and replace them with images.
-
8
Very common requirement developers encounter while working with javascript arrays is to sort an array of strings. This article demonstrates easy ways to sort array of strings alphabetically in ascending as well as descending order using vario...
-
9
We often use sorting while working with arrays. This article demonstrates easy ways to sort an array of strings by their length using different methods and various example illustrations. The strings within the array will be sorted based on th...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK