JAVASCRIPT Tutorial

Strings

Strings in programming represent sequences of characters enclosed in single (') or double ("") quotes. They are a fundamental data type used to store and manipulate text.

Key Concepts

  • Concatenation: Combining two or more strings into a single string.
let str1 = "Hello";
let str2 = "World";
let newStr = str1 + str2; // newStr = "HelloWorld"
  • String Methods: Functions that can be performed on strings to manipulate their content or behavior.
let str = "Example String";
str.toUpperCase(); // converts string to uppercase
str.includes("Example"); // checks if string contains substring

Practical Steps

  1. Declare a String: Use single or double quotes to enclose the text.
let greeting = "Hello, world!";
  1. Concatenate Strings: Use the + operator to join strings.
let name = "John";
let lastName = "Smith";
let fullName = name + " " + lastName;
  1. Use String Methods: Invoke methods on strings to perform specific operations.
let sentence = "This is a sentence.";
sentence.toLowerCase(); // "this is a sentence."
sentence.indexOf("sentence"); // 10 (index of first occurrence of "sentence")

Example

let message = "Welcome to JavaScript!";

console.log(message); // Output: Welcome to JavaScript!

let newMessage = message.toUpperCase(); // Convert to uppercase

console.log(newMessage); // Output: WELCOME TO JAVASCRIPT!