Logo

dev-resources.site

for different kinds of informations.

JavaScript: String Template Literals

Published at
8/3/2024
Categories
javascript
strings
stringtemplateliterals
cleancode
Author
meganpaffrath
Author
13 person written this
meganpaffrath
open
JavaScript: String Template Literals

String template literals give us a cleaner way to concatenate our strings and variables.

Let's say we have 5 hens and 3 roosters.

let hens = 5;
let roosters = 3;
Enter fullscreen mode Exit fullscreen mode

We want to print out "We have 8 chickens, 5 of which are hens and 3 of which are roosters."

We could say:

let str = "We have " + (hens+roosters) + " chickens, " + hens + " of which are hens and " + roosters + " of which are roosters.";

// str = 'We have 8 chickens, 5 of which are hens and 3 of which are roosters.'
Enter fullscreen mode Exit fullscreen mode

Or we could say

let str = `We have ${hens + roosters} chickens, ${hens} of which are hens and ${roosters} of which are roosters.`

// str =  'We have 8 chickens, 5 of which are hens and 3 of which are roosters.'
Enter fullscreen mode Exit fullscreen mode

The second way of making this string is easier to both code and read!

strings Article's
30 articles in total
Favicon
trimMiddle() - the missing String trim method
Favicon
Interpolação, Verbatim String Literals, Múltiplas Linhas – Tudo Sobre Strings no C#
Favicon
Leetcode — 3110. Score of a String
Favicon
Tipos Básicos em Kotlin - String Literals
Favicon
Tipos Básicos em Kotlin - Strings
Favicon
Quick Zig and C String Conversion Conundrums
Favicon
Code Smell 261 - DigiCert Underscores
Favicon
JavaScript: Strings, String Methods, and String Properties
Favicon
JavaScript: String Template Literals
Favicon
Top 7 PHP Strings Interview Questions and Answers
Favicon
Strings in java language
Favicon
Strings in java language
Favicon
Unraveling the Power of Strings in Python
Favicon
What to use instead of `@ember/string`
Favicon
JS Challenge: Check if a string is a palindrome
Favicon
5 Interesting Things About Strings in Java
Favicon
Efficient String Splitting in Go: A Solution for Gophers Dealing with API Responses
Favicon
Javascript : 12 méthodes essentielles pour les chaînes de caractères
Favicon
anagram program in java
Favicon
How to convert String to Integer in java
Favicon
JS Blog - String Manipulation
Favicon
Find the Index of the First Occurrence in a String (Naive and KMP Solutions)
Favicon
In search of subsequence
Favicon
383. Ransom Note
Favicon
Split and Join: The Dichotomy of Strings and Arrays
Favicon
How do Bytes live in Solidity and coexist with Strings?
Favicon
"str and String in Rust confuses a lot of people in the beginning"
Favicon
W3Schools Strings Method With Example
Favicon
Javascript Tagalog - Strings
Favicon
Resources(R.string) in viewModel in Android

Featured ones: