Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

How Do You Format a String in JavaScript?

learningcode_x1mckf by learningcode_x1mckf
October 22, 2022
in JavaScript
0
How Do You Format a String in JavaScript?
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


In JavaScript, a string is a gaggle of characters enclosed by both a pair of single or double citation marks. There are a lot of methods to format strings in JavaScript.


You should utilize particular strategies or operators to mix strings. You may even carry out particular operations to determine what string seems the place and when.

Discover ways to format your JavaScript strings utilizing concatenation strategies and template literals.


String Concatenation

JavaScript lets you concatenate strings utilizing a number of approaches. A helpful strategy is the concat() methodology. This methodology makes use of two or extra strings. It makes use of a single calling string and takes a number of strings as arguments.

const firstName = "John";
const lastName = "Doe";

let stringVal;

stringVal = firstName.concat(" ", lastName);
console.log(stringVal);

Right here, concat joins the string arguments (a clean area and lastName) to the calling string (firstName). The code then shops the ensuing new string in a variable (stringVal) and prints the new value to the browser console:

Using concat method

One other method of concatenating a set of strings is by utilizing the plus operator. This methodology lets you mix string variables and string literals to create new strings.

const firstName = "John";
const middleName = "Mike";
const lastName = "Doe";

let stringVal;

stringVal = firstName + " " + middleName + " " + lastName;
console.log(stringVal);

The code above prints the next output to the console:

Plus operator string concatenation

A 3rd strategy to concatenating your JavaScript strings requires the usage of a plus and equal signal. This methodology lets you add a brand new string to the top of an current one.

const firstName = "John";
const lastName = "Doe";

let stringVal;

stringVal = firstName;
stringVal += " ";
stringVal += lastName;

console.log(stringVal);

This code appends a clean area and the worth of the lastName variable to the firstName variable, producing the next output:

Plus and equal operators on string

Template Literals

Template literals are an ES6 function that lets you format JavaScript strings. A template literal makes use of a pair of backticks (`) to show strings. This methodology of string formatting lets you show cleaner multiline strings in JavaScript.

let html;

html = `<ul>
<li> Identify: John Doe </li>
<li> Age: 24 </li>
<li> Job: Software program Engineer </li>
</ul>`;

doc.physique.innerHTML = html;

The JavaScript code above makes use of HTML to print an inventory of three gadgets within the browser:

Template literals output

To realize the identical output with out template literals (or earlier than template literals), you would wish to make use of citation marks. Nevertheless, you wouldn’t have the ability to prolong the code over a number of traces as you’ll be able to with template literals.

let html;

html = "<ul><li>Identify: John Doe</li><li>Age: 24</li><li>Job: Software program Engineer</li></ul>";

doc.physique.innerHTML = html;

String Interpolation

Template literals allow you to use expressions in your JavaScript strings by means of a course of known as interpolation. With string interpolation you’ll be able to embed expressions or variables in your strings utilizing the $expression placeholder. That is the place the worth of JavaScript template literals turns into actually evident.

let userName = "Jane Doe";
let age = 21;
let job = "Internet Developer";
let expertise = 3;

let html;

html = `<ul>
<li> Identify: $userName </li>
<li> Age: $age </li>
<li> Job Title: $job </li>
<li> Years of Expertise: $expertise </li>
<li> Developer Degree: $expertise < 5 ? "Junior to Intermediate" : "Senior" </li>
</ul>`;

doc.physique.innerHTML = html;

The code above produces the next output within the console:

String interpolation output

You might also like

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

Select data value from grandparent div? – JavaScript – SitePoint

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

The primary 4 arguments of the $expression placeholder are string variables, however the fifth is a conditional expression. The expression depends on the worth of one of many variables (expertise), to dictate what it ought to show within the browser.

Formatting Parts on Your Webpage With JavaScript

Other than its useful affiliation with webpage improvement, JavaScript works with HTML to affect the design and structure of a webpage. It could manipulate the textual content that seems on a webpage, as is the case with template literals.

It could even convert HTML to pictures and show them on a webpage.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Toolkit Allows JavaScript Devs to Program Embedded Devices  The New Stack Source link

Read more

Select data value from grandparent div? – JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Select data value from grandparent div? - JavaScript  SitePoint Source link

Read more

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Handle Errors in JavaScript - Programming  MUO - MakeUseOf Source link

Read more

How to Use the Javascript Slice Method – hackernoon.com

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Use the Javascript Slice Method  hackernoon.com Source link

Read more

Clean Code in JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Clean Code in JavaScript  SitePoint Source link

Read more
Next Post
Senior Java Developer at Reverside

Senior Java Developer at Reverside

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

An Introduction to Module Systems in JavaScript

An Introduction to Module Systems in JavaScript

September 25, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

TIOBE: C++ overtakes Java in programming language popularity – Developer News

February 18, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

C++ 23 Language Standard Declared Feature-Complete – Slashdot

March 6, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?