JavaScript xxx.split is not a functionの解決方法

環境
Google Chrome  114.0.5735.199(Official Build) (64 ビット)
Windows 11 Pro 64bit

修正前コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const num =82324;
const result = num.split('2');
const num =82324; const result = num.split('2');
const num =82324;

const result = num.split('2');

エラーメッセージ
Error: num.split is not a function

原因
split() メソッドは、 String を指定した区切り文字列で分割することにより、文字列の配列に分割します。

修正後コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const num =82324;
const result = num.toString().split('3');
console.log( result );
const num =82324; const result = num.toString().split('3'); console.log( result );
const num =82324;

const result = num.toString().split('3');

console.log( result );

実行結果
> Array [“82", “24"]

JavaScript

Posted by arkgame