Introduction to JavaScript Strings

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 emp…


This content originally appeared on DEV Community and was authored by Naftali Murgor

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"

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

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
}

Get length of a JavaScript String

const THREAD_NAME = 'Moonspiracy'
console.log(THREAD_NAME.length) // prints number of characters// 11

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.


This content originally appeared on DEV Community and was authored by Naftali Murgor


Print Share Comment Cite Upload Translate Updates
APA

Naftali Murgor | Sciencx (2022-02-15T21:00:53+00:00) Introduction to JavaScript Strings. Retrieved from https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/

MLA
" » Introduction to JavaScript Strings." Naftali Murgor | Sciencx - Tuesday February 15, 2022, https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/
HARVARD
Naftali Murgor | Sciencx Tuesday February 15, 2022 » Introduction to JavaScript Strings., viewed ,<https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/>
VANCOUVER
Naftali Murgor | Sciencx - » Introduction to JavaScript Strings. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/
CHICAGO
" » Introduction to JavaScript Strings." Naftali Murgor | Sciencx - Accessed . https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/
IEEE
" » Introduction to JavaScript Strings." Naftali Murgor | Sciencx [Online]. Available: https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/. [Accessed: ]
rf:citation
» Introduction to JavaScript Strings | Naftali Murgor | Sciencx | https://www.scien.cx/2022/02/15/introduction-to-javascript-strings/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.