The Wayback Machine - https://web.archive.org/web/20240824203734/https://www.geeksforgeeks.org/how-to-print-a-string-in-javascript/
Open In App

How to Print a String in JavaScript ?

Last Updated : 05 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single (”) or double (“”) quotes.

Print Strings on Console

It is commonly used for debugging and logging messages to the browser console. You can check the printed string by going to the inspect element and then selecting the console tab.

Syntax:

console.log("Geeksforgeeks");

Example: The below code will explain how you can print a string in console.

JavaScript
console.log("Geeksforgeeks");
const desc = "A Computer Science Portal";
console.log(desc);

Output
Geeksforgeeks
A Computer Science Portal

We can use either the document.write() method or the innerHTML property to print the string on web page.

Example: The below code uses document.write() to print string on web page.

HTML
<!DOCTYPE html>
<html>

<body>
    <h2>Hello, GeeksforGeeks</h2>
</body>
<script>
    document.write("Hello, Geeks");
</script>

</html>

Output:

gees

We can provide strings in pop up dialogue box. It can be shown using any of the alert, prompt or confirm method.

Example: The below code implements alert box to print the string in dialog box.

HTML
<!DOCTYPE html>
<html>

<body>
    <h2>Hello,Geeksforgeeks</h2>
</body>
<script>
    alert("Hello Geeks!");
</script>

</html>

Output:

helo

Alert message



Similar Reads

JavaScript Print shortest Path to Print a String on Screen
Given a screen with the English alphabet (A-Z) and a remote control with navigation keys (left, right, up, down), we have to determine the most efficient way to write a string using the remote. Starting from the top-left corner of the screen, we need to move through the alphabet to spell out the entire input string in the correct order. In this art
3 min read
How to pretty print JSON string in JavaScript ?
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In this article, we are going to learn how to pretty print JSON strings in JavaScript. ApproachDeclare a JSON object and store it in the variable.Use JSON.stringify(obj) method to convert
2 min read
JavaScript Program to Print All Duplicate Characters in a String
In this article, we will learn how to print all duplicate characters in a string in JavaScript. Given a string S, the task is to print all the duplicate characters with their occurrences in the given string. Example: Input: S = “geeksforgeeks”Output:e, count = 4g, count = 2k, count = 2s, count = 2Table of Content Using For Loop in JavaScriptUsing S
5 min read
JavaScript Program to Print all Permutations of Given String
In this article, we will see how to write a program that prints all permutations of a given string. Permutation refers to each of several possible ways in which a set or number of things can be ordered or arranged. There are N! permutations for a string of length N. Examples: Input: S = “XY”Output: “XY”, “YX”Examples of Print all Permutations of Gi
5 min read
JavaScript Program to Print all Subsequences of a String
A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements without changing the order of the remaining elements. Subsequences of a string can be found with different methods here, we are using the Recursion method, Iteration method and Bit manipulation method. Example: The example shows the input string
3 min read
JavaScript Program to Print all Substrings of a Given String
In this article, we will see how we can print all substrings of a given string in JavaScript language. We have provided an input string, from which we need to print all the possible substrings. Below we have stated the example for a better understanding of the problem statement: Example: Input: abcOutput: a ab abc b bc cTo print all substrings of a
4 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a" - Shift 3 times - "d" Character "b" - Shift 2 ti
3 min read
Javascript Program to Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
4 min read
Javascript Program to Check if a string can be obtained by rotating another string 2 places
Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview Recommended: Please solve it on “PRACTICE ” f
2 min read
JavaScript Program to Return Original String of Compressed String
The compressed string follows a format where each character is followed by its count. we need to print a string having the same number of characters present in the compressed string. Below are the approaches to return original string of compressed string using Example: Input: compressedString= "a3b2c1"Output: originalString="aaabbc"These are the fo
2 min read
Smallest Window in a String Containing all the Characters of Another String using JavaScript
Given two strings, "str" (the larger string) and "pattern" (the smaller string), the task is to find the smallest window in "str" that contains all the characters of "pattern" in any order using JavaScript. Examples: Input: str = "ADOBECODEBANC"pattern = "ABC"Output: BANCInput: str = "this is a test string"pattern = "tist";Output: t striTable of Co
3 min read
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We're going to discuss a few methods, these are: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript String substr() Method Method 3: Using JavaScript
4 min read
How to check a given string is an anagram of another string in JavaScript ?
In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram. An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different term or phrase. Some of the examples are given below
3 min read
How to check if a string "StartsWith" another string in JavaScript ?
In JavaScript, the 'startsWith()' method is a built-in method that allows one to check whether a string starts with a specified substring. Syntax: It takes a single argument, which is the substring we want to check whether the string starts with. string.startsWith(searchString[, position])Approach 1: Using startWith methodThe 'searchString' paramet
2 min read
Check if a Given String is Binary String or Not in JavaScript
Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string. Example: Input: "101010"Output: True, binary stringInput: "110211"Output: Falee,Explanati
4 min read
Difference between String.slice and String.substring in JavaScript
These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part. The first character starts with index 0. Syntax
3 min read
Check if a string is a valid JSON string using JavaScript
We will be given a string and we need to validate whether the given string is a valid JSON string or not. JSON string is the same as a javaScript object and it must follow the syntax of that object. If the syntax is correct then we have to return true or we have to return false. Methods to Check if a String is Valid JSON String: Table of Content Us
5 min read
Difference Between String.prototype.replace() and String.prototype.replaceAll() in JavaScript
The String.prototype.replace and String.prototype.replaceAll are frequently used to the modify strings. Understanding the differences between these methods is crucial for the effective string handling. This article explores the characteristics, applications and examples of both the replace and replaceAll methods. These are the following topics that
3 min read
How to count string occurrence in string using JavaScript ?
In JavaScript, we can count the string occurrence in a string by counting the number of times the string is present in the string. This can be done in the following ways: Table of Content Approach 1: Using match() functionApproach 2: Using a loopApproach 3: Using split() functionApproach 4: Using Indexof()Approach 5: Using regular expressionsApproa
4 min read
PHP str_pad to print string patterns
str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input is
1 min read
How to add special characters to text to print in color in the console in JavaScript ?
The purpose of this article is to add special characters to text to print in color in the console in JavaScript. Approach: The ANSI escape codes help change/specify the color of the output in the console. The color of the output of the console can be changed by adding these escape codes just before the actual text. Syntax: /*Codes for different Col
2 min read
Print current day and time using HTML and JavaScript
The task is to print the system's current day and time in the following format(Time in hours, minutes and milliseconds). Approach: Use getDay() function to create an array of days in a week. It returns a number of day like 0 for Sunday, 1 for Monday and so on). Convert it into AM or PM using ternary operator. Use getHours() method to get the hour v
2 min read
How to create a pop-up to print dialog box using JavaScript?
Given an HTML document and the task is to design a button that would pop-up a print dialog box. We are going to use JavaScript to do the assigned task: Approach:: Add a button which links to a JavaScript Function. Inside the JavaScript Function, use the JavaScript default function to call the print dialog box Syntax: window.print Example: &lt;!DOCT
2 min read
How to print a number with commas as thousands separators in JavaScript?
Method 1: Using Intl.NumberFormat() The Intl.NumberFormat() object is used to represent numbers in language-sensitive formatting. It can be used to represent currency or percentages according to the locale specified.The locales parameter of this object is used to specify the format of the number. The 'en-US' locale is used to specify that the local
2 min read
How to print a circular structure in a JSON like format using JavaScript ?
The circular structure is when you try to reference an object which directly or indirectly references itself. Example: A -&gt; B -&gt; A OR A -&gt; A Circular structures are pretty common while developing an application. For Example, suppose you are developing a social media application where each user may have one or more images. Each image may be
4 min read
Javascript Program to Print all possible rotations of a given Array
Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotation arr[] = {3, 4, 1, 2} After third rotation arr[
3 min read
Javascript Program to Print array after it is right rotated K times
Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :   Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: Array[] = {1, 2, 3, 4, 5}, K = 4. Output: 2 3 4 5
2 min read
Javascript Program for Print all triplets in sorted array that form AP
Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples :   Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Input : arr[] = { 3, 5, 6, 7, 8, 10, 12}; Output : 3 5 7 5 6 7 6 7 8 6 8 10 8 10 12   A simple
4 min read
Javascript Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
2 min read
Javascript Program to Print matrix in antispiral form
Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 13, 14, 15, 16, 17, 18}; Output : 11 10 9 8 7 13 1
3 min read
three90RightbarBannerImg