$name = "John Doe";
$jsValue = \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSValue($name);This would result in the string `\"John Doe\"`, which can be safely used in a JavaScript string. The `quoteJSValue()` method is included in the TYPO3 CMS core library, which can be found in the `typo3/cms-core` package on Packagist.