Favorite String Literals

I was recently listening to a JavaScript tutorial on 2x speed when I caught something that caused me to slow down and rewind. The presenter was talking about strings, concatenation, and ES6 string literals. The statement that caught my attention I summ…


This content originally appeared on DEV Community and was authored by Kristen Kinnear-Ohlmann

I was recently listening to a JavaScript tutorial on 2x speed when I caught something that caused me to slow down and rewind. The presenter was talking about strings, concatenation, and ES6 string literals. The statement that caught my attention I summarized as "Go ahead and start with template literals first so you don't need to change to backticks later when you need to use a variable".

JavaScript

// instead of concatenation
// start with a template literal
let numberOfCats = 5;
console.log(`I have ${numberOfCats} cats.`);
console.log(`Now I have ${numberOfCats+1} cats.`);

This struck me as very sensible! There are many advantages to using a string literal instead of a basic statement or simple concatenation and you would save yourself time in refactoring code if you were already using them. They are more readable than other ways of formatting strings; they look like normal sentences and allow you to more easily identify portions to change or update.

I realized I could apply this kind of strategy to some of the other languages I know as well:

Python

# instead of concatenation
# use an 'f' string in Python 3 right away
number_of_cats = 5
f"I have {number_of_cats} cats"

C#

// instead of String.Format() (ex. Console.WriteLine("Hello {0}", "world"))
// use string interpolation
string numberOfCats = 5;
Console.WriteLine($"I have {numberOfCats} cats.");

By using these techniques right away in your code, you save time and effort and increase the accuracy of your code when changes occur.

Further Reading


This content originally appeared on DEV Community and was authored by Kristen Kinnear-Ohlmann


Print Share Comment Cite Upload Translate Updates
APA

Kristen Kinnear-Ohlmann | Sciencx (2022-07-11T03:09:13+00:00) Favorite String Literals. Retrieved from https://www.scien.cx/2022/07/11/favorite-string-literals/

MLA
" » Favorite String Literals." Kristen Kinnear-Ohlmann | Sciencx - Monday July 11, 2022, https://www.scien.cx/2022/07/11/favorite-string-literals/
HARVARD
Kristen Kinnear-Ohlmann | Sciencx Monday July 11, 2022 » Favorite String Literals., viewed ,<https://www.scien.cx/2022/07/11/favorite-string-literals/>
VANCOUVER
Kristen Kinnear-Ohlmann | Sciencx - » Favorite String Literals. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/11/favorite-string-literals/
CHICAGO
" » Favorite String Literals." Kristen Kinnear-Ohlmann | Sciencx - Accessed . https://www.scien.cx/2022/07/11/favorite-string-literals/
IEEE
" » Favorite String Literals." Kristen Kinnear-Ohlmann | Sciencx [Online]. Available: https://www.scien.cx/2022/07/11/favorite-string-literals/. [Accessed: ]
rf:citation
» Favorite String Literals | Kristen Kinnear-Ohlmann | Sciencx | https://www.scien.cx/2022/07/11/favorite-string-literals/ |

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.