How Javascript get last character of string? | 6 Methods

In the world of web development, JavaScript is an indispensable programming language. One common task developers encounter is extracting the last character from a string.

While it may seem like a simple operation, there are multiple approaches to achieve this, each with its advantages and use cases.

In this article, we’ll explore various methods to get the last character of a string in JavaScript, backed by real-world insights and examples.

How to get get last character of string?

Let’s dive right into different methods to retrieve the last character of a string in JavaScript.

Method 1: Using the charAt() function

The charAt() function allows us to access a specific character in a string based on its index. To get the last character, we need to find the index of the last character and use it with charAt().

Here’s how you can do it:

const str = "Hello, World!";
const lastChar = str.charAt(str.length - 1);

Method 2: Using string.slice()

The slice() method is commonly used to extract substrings from a larger string. To get the last character, we can use the negative index with slice(), which counts from the end of the string.

Here’s an example:

const str = "JavaScript is awesome!";
const lastChar = str.slice(-1);

Method 3: Using string.substr()

The substr() method allows us to extract a portion of a string, starting from a specified index. To get the last character, we can pass the starting index as str.length – 1 to substr().

Here’s how it works:

const str = "Web development is fun!";
const lastChar = str.substr(str.length - 1);

Method 4: Using string.substring()

Similar to substr(), the substring() method also extracts parts of a string based on indices. To get the last character, we can pass str.length – 1 as the second argument to substring().

Here’s an example:

const str = "Coding is art!";
const lastChar = str.substring(str.length - 1);

Method 5: Using ES6 Destructuring

With ES6, we can leverage array destructuring to get the last character easily. By spreading the string into an array, we can extract the last character using the […str] syntax.

Here’s how:

const str = "JavaScript rocks!";
const [, lastChar] = [...str].reverse();

Method 6: Using Regular Expression

For more complex scenarios, regular expressions can come in handy. We can use a regular expression to match the last character and extract it from the string.

Here’s an example:

const str = "This is a test string";

Practical Applications of JavaScript Get Last Character of String

Knowing how to extract the last character from a string is useful in various scenarios. Let’s explore some practical applications:

1. Input Validation

When users enter data into a form, we often need to validate their input. For instance, if they enter a single character, we might want to check if it’s an uppercase letter or a lowercase letter. Knowing how to get the last character allows us to perform such validations easily.

2. File Extensions

In web applications, we often deal with file uploads. Extracting the last character of a filename can help us determine the file extension, which is crucial for server-side processing.

3. String Trimming

Sometimes, strings may have leading or trailing whitespace that we want to remove. By getting the last character, we can check if it’s a space and then trim the string accordingly.

4. Password Strength Checking

When users create passwords, we might want to enforce certain rules, such as requiring at least one special character at the end. Extracting the last character helps us perform such checks.

5. Formatting and Display

In certain cases, we might need to format or display strings based on the last character. For example, if we have a list of items, we can choose to display them in a specific way depending on the last character.

Nevertheless, here are other functions you can learn to enhance your JavaScript skills.

Conclusion

In conclusion, understanding how to get the last character of a string in JavaScript is a valuable skill for web developers. By employing various methods like charAt(), slice(), substr(), substring(), ES6 destructuring, and regular expressions, you can confidently manipulate strings to suit your needs. Whether you’re performing input validation, handling file uploads, or formatting strings, knowing these techniques will make your code more efficient and effective.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Glay Eliver


Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame
 · View all posts by Glay Eliver →

Leave a Comment