Originally published on php-to-js.learnio.dev — part of the PHP to JavaScript course. View this lesson on the original site.

Backticks are PHP double-quotes with superpowers

String work in PHP is mostly functions (strlen, strtoupper, str_contains). In JavaScript the string is an object with methods, and backticks give you interpolation like PHP’s double-quoted strings.

PHP habit: double quotes and str_*

<?php
$name = 'Alice';
$line = "Hello, {$name}!";
$upper = strtoupper('php developer');
$parts = explode(',', 'a,b,c');
$joined = implode('-', $parts);

echo $line, "\n", $upper, "\n";
print_r($parts);

JavaScript equivalent

const name = 'Alice';
const line = `Hello, ${name}!`;
const upper = 'php developer'.toUpperCase();
const parts = 'a,b,c'.split(',');
const joined = parts.join('-');

console.log(line);
console.log(upper);
console.log(parts);
console.log(joined);
PHP JavaScript Notes
"Hello, {$name}" `Hello, ${name}` Template literal
Heredoc / concatenation Multi-line backtick string No extra syntax
strlen($s) s.length Property, not a function
strtoupper($s) s.toUpperCase() Method on the value
trim($s) s.trim()
str_contains($s, 'x') s.includes('x') Boolean
str_starts_with($s, 'p') s.startsWith('p')
explode(',', $s) s.split(',') Returns array
implode('-', $arr) arr.join('-') Called on the array
strpos($s, 'x') s.indexOf('x') -1 when missing (not false)
str_replace('-', '_', $s) s.replaceAll('-', '_') JS .replace alone hits first match only

Replace all occurrences

<?php
echo str_replace('-', '_', 'a-b-c'); // a_b_c
console.log('a-b-c'.replace('-', '_'));      // a_b-c
console.log('a-b-c'.replaceAll('-', '_'));   // a_b_c

Try it

Use the amber Try it panel below. Click Run.

const name = 'Stuart';
const greeting = `Hello, ${name}!`;
const slug = '  Hello World  '.trim().toLowerCase().replaceAll(' ', '-');

console.log(greeting);
console.log(slug);
console.log('course'.includes('our'));

If you can read PHP string functions, you can read JavaScript string methods with this table beside you.

From PHP to JavaScript — JavaScript Syntax Foundations for PHP Devs