How to Split a String in JavaScript with split
split cuts a string into pieces wherever a separator appears, and gives you the pieces as an array. '2026-11-28'.split('-') turns a date into its year, month and day.
JavaScript Programming Language, in the Browser and in Node.js
View All Tagssplit cuts a string into pieces wherever a separator appears, and gives you the pieces as an array. '2026-11-28'.split('-') turns a date into its year, month and day.
A function is a named piece of code you can run as often as you like. The most direct way to write one in JavaScript is a function declaration: the word function, a name, the parameters in brackets and the body in braces.
Programs make decisions: show a warning when it's hot, skip a step when a file is missing. In JavaScript the if statement does that: it runs a block of code only when its condition is true, and else if and else cover the other cases.
A catch block catches every error, including the ones you didn't plan for. Handle the error you expected and send every other one back up with throw err;, so a real bug still stops the program and shows you where it happened.
A catch block receives every error, whatever went wrong. To react only to the kind you expect, such as invalid JSON, check the error's type with instanceof, which also knows that a specific error type belongs to a more general one.
When an awaited promise fails, await throws its error right there in your function, just like a normal error. Put the await inside a try block and the catch block gets that error, so the program can report it and carry on instead of crashing.
Most variables in a program are set once and never pointed anywhere else. Declaring them with const says so, and JavaScript then stops any line that tries to reassign them, which catches a whole class of mistakes early.
console.log is how a Node.js program talks to you in the terminal: it prints whatever you give it, on its own line. Strings, numbers, arrays and objects all work, and you can pass several at once.
Building a message from variables with + gets messy fast: 'You ordered ' + quantity + ' ' + item + 's'. A template literal writes the whole message once and drops the values in where they belong.
Base64 turns bytes into plain text so they can travel in JSON, URLs or environment variables. Uint8Array.fromBase64 turns that text back into the bytes, as a byte array, using a method built into JavaScript itself.