JS String

HTML
CSS
C#
SQL

JavaScript String

A string in JavaScript is a sequence of characters that can include letters, numbers, special characters, or a combination of all. There are two ways to create strings in JavaScript:

  // Creating strings
  var myString1 = ‘Hello World!’; // Single quoted string
  var myString2 = “Hello World!”; // Double quoted string

2. Using String Object (new keyword):

Strings can also be created using the new keyword to create an instance of the String object.

var stringObject = new String(“Hello, JavaScript string”);
  document.write(stringObject);

It’s important to note that while both methods achieve the same result, using string literals is more common and simpler. The String object is rarely used in practice for creating strings.

Including Quotes Inside a String

You can use quotes inside a string, but they should not match the quotes surrounding the string. If they do, you can use escape characters (backslash `\`) to include them:

  var str1 = “it’s okay”;
  var str2 = ‘He said “Goodbye”‘;
  var str3 = “She replied ‘Calm down, please'”;
  // var str4 = ‘Hi, there!”; // Syntax error – quotes must match

Using escape sequences:

  var str1 = ‘it\’s okay’;
  var str2 = “He said \”Goodbye\””;
  var str3 = ‘She replied \’Calm down, please\”;

Escape Sequences

Escape sequences are also useful for handling characters that can’t be typed using a keyboard. Common escape sequences include:

– `\n` for a newline

– `\t` for a tab

– `\r` for a carriage return

– `\\` for a single backslash

Example :

var str1 = “The quick brown fox \n jumps over the lazy dog.”;
  document.write(“<pre>” + str1 + “</pre>”); // Create a line break

var str2 = “C:\Users\Downloads”;
  document.write(str2); // Prints C:UsersDownloads

var str3 = “C:\\Users\\Downloads”;
  document.write(str3); // Prints C:\Users\Downloads

String Concatenation and Arithmetic

When using the `+` operator, be aware that it is used for both addition and string concatenation. Mixing numbers and strings may produce different results:

  var x = 10;
  var y = 20;
  var z = “30”;

  console.log(x + y); // 30 (addition)
  console.log(z + z); // ‘3030’ (string concatenation)
  console.log(x + z); // ‘1030’ (string concatenation)
  console.log(z + x); // ‘3010’ (string concatenation)
  console.log(“The result is: ” + x + y); // ‘The result is: 1020’ (string concatenation)
  console.log(x + y + z); // ‘The result is: 3030’ (string concatenation)

Course Video

Examples for Practice

You have to solve all the questions given below in the editor without copy-pasting.

1. Write a program that takes five names (Alice, Bob, Charlie, David, Eve) using prompts and displays them separately. display on the document for each name in a new line.

Used the prompt() method five times to get input for each name, and then displayed each name on the document using document.write() with <p> tags, creating a new line for each name.

Here’s a JavaScript program that takes five names using prompts and displays them separately on the document:

// Prompt user to input five names
let name1 = prompt(“Enter the first name:”);
let name2 = prompt(“Enter the second name:”);
let name3 = prompt(“Enter the third name:”);
let name4 = prompt(“Enter the fourth name:”);
let name5 = prompt(“Enter the fifth name:”);

// Display names on the document
document.write(“<p>” + name1 + “</p>”);
document.write(“<p>” + name2 + “</p>”);
document.write(“<p>” + name3 + “</p>”);
document.write(“<p>” + name4 + “</p>”);
document.write(“<p>” + name5 + “</p>”);

Output

2. Create a script that prompts the user to input a value and then checks whether the value is a number or not using the isNaN() function.

The task is to create a JavaScript script that prompts the user to input a value and then checks whether the input is a number or not using isNaN(), and then logs a corresponding message to the console based on the result of the check.

Here’s a simple JavaScript program that prompts the user to input a value, and then checks whether the value is a number using the isNaN() function:

// Prompt user to input a value
let userInput = prompt(“Enter a value:”);

// Check if the input is a number using isNaN()
if (!isNaN(userInput)) {
console.log(“The input is a number.”);
} else {
console.log(“The input is not a number.”);
}

Output

3. Write a javascript program that prompts the user to input a string, then convert it to a number using the parseFloat() function.

The task requires you to write a JavaScript program that prompts the user to input a string, then converts that string to a number using the parseFloat(), and then provides feedback based on whether the conversion was successful or not.

The parseFloat() function is used to convert the string to a floating-point number. This ensures that if the user enters a decimal number, it is correctly converted.

Here’s a simple JavaScript program that prompts the user to input a string, then converts it to a number using the parseFloat() function:

// Prompt user to input a string
let userInput = prompt(“Enter a number:”);

// Convert the string to a number using parseFloat
let convertedNumber = parseFloat(userInput);

// Check if the conversion was successful
if (!isNaN(convertedNumber)) {
console.log(“Converted number:”, convertedNumber);
} else {
console.log(“Invalid input. Please enter a valid number.”);
}

Output