A string is text — a sequence of characters wrapped in quotes. Strings are one of the most-used data types, since almost every program deals with text somewhere: names, messages, file paths, URLs.
let greeting = "Hello, world!";
let single = 'Single quotes work too';<?php
$greeting = "Hello, world!";
$single = 'Single quotes work too';greeting = "Hello, world!"
single = 'Single quotes work too'let first = "Ada";
let last = "Lovelace";
let fullName = first + " " + last;
console.log(fullName); // "Ada Lovelace"<?php
$first = "Ada";
$last = "Lovelace";
$fullName = $first . " " . $last;
echo $fullName; // "Ada Lovelace"first = "Ada"
last = "Lovelace"
full_name = first + " " + last
print(full_name) # "Ada Lovelace"| Method | What it does | Example |
|---|---|---|
.length | Number of characters | "hello".length → 5 |
.toUpperCase() | Converts to uppercase | "hi".toUpperCase() → "HI" |
.includes(x) | Checks if text contains x | "hello".includes("ell") → true |
.slice(a, b) | Extracts part of a string | "hello".slice(0, 3) → "hel" |
Backticks let you embed variables directly inside a string, instead of joining pieces with +.
let name = "Ada";
let age = 28;
console.log(`${name} is ${age} years old.`);
// "Ada is 28 years old."<?php
$name = "Ada";
$age = 28;
echo "$name is $age years old.";
// "Ada is 28 years old."name = "Ada"
age = 28
print(f"{name} is {age} years old.")
# "Ada is 28 years old."