String Formatting in JavaScript3 Mar 2025 | 4 min read We will understand string formatting in JavaScript in this article. Let us first understand the JavaScript string. JavaScript stringThe JavaScript string is the series of characters that are kept inside single or double quotes. The string's first character is indexed at 0 and the second character is indexed at 1 and so on. String formattingThe key concept of string formatting is to insert variables inside a string. The inserted variables are known as placeholders. There are many methods which are utilized to format a string:
We will understand each method with the help of demonstrations. Utilizing ${} within BackticksWe will use the ${} within backticks to format a string. The backtick is represented as ( ` ` ). Demo 1: We will declare two strings named subject1 and subject2. After that, we will put the values to these strings. We will utilize ` ` and ${} to format and print a string. Code: Output: Learn Core Java and Advanced Java from Javatpoint. Demo 2: We will declare two strings named mathsMarks and englishMarks. After that, we will add the strings and put the value to the string total. We will utilize ` `, ${} and ternary operator to format a string. Code: Output: Your total marks are 205. Congrats! You are eligible to get admission to the school. You are not a student. Demo 3: We will declare a string named student and give it a Boolean value as false. After that, we will format a string according to the condition. Code: Output: You are not a student. Utilizing concatenation (+ operator)We will use the concatenation to format a string. The concatenation is utilized to combine two or more strings. Demo 1: We will declare three strings named s1, s2 and s3 then store the values. We will utilize the concatenation operator to join the two strings and give it to a new string. Code: Output: Welcome to JTP! Demo 2: We will utilize the concatenation operator with the ternary operator to format a string. We will declare a string named age and store the value then we will print a new string. Code: Output: You are not eligible to vote this time. Demo 3: We will declare two strings named number1 and number2. After that, we will store the difference between these strings in a string named difference. We will then print the formatted string. Code: Output: The difference between 45 and 20 is 25 Utilizing String interpolationWe will use the string interpolation to format a string. This method is utilized to inject variables, arithmetic expressions and function calls into a string. Demo 1: We will declare four strings named firstName, lastName, studentName and message. We will declare another string formatted_message and provide it the value of replaced message. Code: Output: Welcome Ajeet Kumar, submit your assignment. Demo 2: We will declare two strings named employeeName and message. We will declare another string formatted_message and provide it the value of replaced message. Code: Output: Hello Anjali, show your work. Utilizing custom functionWe will use the custom function to format a string. Demo: We will create a custom function and print the formatted string. Code: Output: Congratulations, Shrishti from Chandigarh University! Welcome to Javatpoint. Conclusion:We have gained a comprehensive understanding of string formatting in JavaScript in this article. We have understood various methods with the help of demonstrations and now we are equipped with the knowledge to apply these techniques in future projects. Next TopicNested Ternary Operator in JavaScript |
A relatively simple data-interchange format known as JSON (JavaScript Object Notation) is essential to contemporary web development. It makes data structuring simpler and more readable for humans, which makes it suited for information transfer between a client and a server. Because of its ease of use...
7 min read
JavaScript convert usually refers to the process of converting data from one type to another within a JavaScript program. This could include converting a string to a number, converting an object to JSON format, converting a number to a string, and so on. JavaScript is a versatile...
3 min read
In web development, interactivity plays a pivotal role in creating engaging and dynamic user experiences. JavaScript, being a versatile scripting language, offers a multitude of events to facilitate interaction with web elements. One such event is onfocus. In this article, we'll delve into what the onfocus...
7 min read
The processing of data into some other assembly of symbols or choices is a key characteristic in JavaScript programming that moves with the world. Its function is multifaceted. It is used for a wide range of purposes that span the gamut from displaying money to...
6 min read
JavaScript is a versatile and widely used programming language, particularly in web development. Adding two numbers in JavaScript is one of the most fundamental operations you'll encounter when working with this language. In this article, we'll explore various methods for adding two numbers in JavaScript, catering...
3 min read
What is Form in JavaScript? In JavaScript, a form is an HTML element used to collect user input. Forms consist of one or more input elements and a submit button used to submit the form's data to a web server. If you want to create a...
6 min read
What is an Object? JavaScript uses objects to store data or functionality that is related in some way. An object is a collection of properties, which can include methods as well as simple data values. Essentially, objects are entities that have both state and behavior. For...
3 min read
An array is a collection of items that stores different kinds of data types. We can find specific elements from the array by utilizing many methods. There are various ways that we can utilize to search for an element in an array which are as follows: find()...
6 min read
Introduction JavaScript is an object-oriented programming language generally used for internet enhancement. One thrilling operation of JavaScript is manipulating data structures, which include linked lists. In this educational, we will discover how to set up an unattached linked list in an alternate, atypical, and indeed bump-order...
11 min read
The date is an essential function for user interaction form, table and database. We can work on the date in the format of the user's requirement. The users mostly use the following date format to display the date. dd/mm/yyyy format mm/dd/yyyy format yyyy/dd/mm format dd/month_name /yyyy format Methods to Convert...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India