How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the First Character of a String in JavaScript
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Get the First Character of a String in JavaScript
Web Development

How to Get the First Character of a String in JavaScript

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Get the first character of a JavaScript string
SHARE

To get the first character of a string in JavaScript, use str[0] for the modern style or str.charAt(0) when you want explicit “empty string when there’s nothing there” behavior. str.at(0) works on every modern browser too and supports negative indices.

Contents
  • Every approach in one block
  • Empty-string behavior differs
  • Checking the first character
  • Heads-up: emoji and surrogates
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on modern browsers and Node 20+. Originally published 2022-10-19, rewritten and updated 2026-05-17.

Every approach in one block

const str = "hello world";

str[0];               // "h"   bracket notation (modern, terse)
str.at(0);            // "h"   modern (also supports negative indices)
str.charAt(0);        // "h"   classic, never throws
str.slice(0, 1);      // "h"   returns a 1-char substring
str.substring(0, 1);  // "h"   same as slice for non-negative args
str.substr(0, 1);     // "h"   legacy (deprecated, but works)
JS first character — bracket, at, charAt, slice, substring compared with empty string behavior

Empty-string behavior differs

const empty = "";

empty[0];               // undefined
empty.at(0);            // undefined
empty.charAt(0);        // ""
empty.slice(0, 1);      // ""
empty.substring(0, 1);  // ""
empty.substr(0, 1);     // ""

The bracket-notation and .at() methods return undefined — usually what you want, because falsy-checking undefined is clearer than checking for an empty string. The older methods return "", which both pass the “is it falsy?” test and silently chain through further string operations without raising.

Checking the first character

const v = document.querySelector('input').value;

// Is the first character a digit?
if (v[0] >= '0' && v[0] <= '9') { /* ... */ }

// Or using startsWith for a cleaner test
if (v.startsWith('@')) { /* mention syntax */ }

For “does the string start with X?” prefer str.startsWith('x') — it reads naturally and handles multi-character prefixes for free.

Heads-up: emoji and surrogates

const flag = "🇺🇸 USA";
flag[0];           // "\uD83C"  (broken half of the emoji)

// Iterate code points instead
[...flag][0];      // "🇺" or similar — still one code point, not the whole flag

// Grapheme-aware (recognises 🇺🇸 as one cluster)
const seg = new Intl.Segmenter('en', { granularity: 'grapheme' });
[...seg.segment(flag)][0].segment;   // "🇺🇸"

JavaScript strings are UTF-16, so any single emoji is two code units. str[0] returns one of those halves — a “lone surrogate” that doesn’t render. Use the spread iterator for code-point granularity, or Intl.Segmenter for true user-perceived characters (graphemes).

Frequently asked questions

Which method is fastest?

All of them are O(1) — they look up the first code unit. Performance differences are negligible. Pick by readability: str[0] is the shortest; str.charAt(0) is the most self-documenting; str.at(0) is the modern choice. Don’t optimise for speed here.

Why do str[0] and str.at(0) return undefined on an empty string, but charAt returns ''?

charAt and its older siblings (slice, substring, substr) treat out-of-range indices as the empty string. str[0] is bracket notation that returns undefined for missing keys (same as any object). str.at(0) follows the bracket-notation convention. The undefined behavior is usually more useful — empty-string returns can hide bugs.

What about emoji and non-Latin characters?

All of these methods return one UTF-16 code unit, not necessarily one user-visible character. An emoji like 😀 is two code units, so '😀'[0] returns half of it (a lone surrogate that won’t render). For grapheme-aware first-character extraction, use the Intl Segmenter: new Intl.Segmenter().segment(str)[Symbol.iterator]().next().value.segment — verbose but correct.

What does String.raw have to do with this?

Nothing — but people often confuse it with str.at(). String.raw is a template tag that returns the raw string without processing escape sequences. Different feature entirely; mentioned only because both arrived in modern JS and search results sometimes conflate them.

Related guides

  • How to Get the First Character of a String in PHP
  • How to Check if a Character Is Unicode in JavaScript
  • How to Check if a JavaScript String Is a Valid URL

References

MDN String.prototype.at(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at. MDN String.prototype.charAt(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt. MDN Intl.Segmenter: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter.

TAGGED:JavaScriptstrings

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Remove the Other Favorites button on the Microsoft Edge favorites bar How to Remove the “Other Favorites” Button in Microsoft Edge
Next Article Get the first character of a PHP string with mb_substr for UTF-8 How to Get the First Character of a String in PHP
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

Laravel Eloquent orderBy — code snippet sorting posts by id descending with arrow icons
Web Development

How to Use orderBy in Laravel Eloquent (with Examples)

6 Min Read
Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon
Web Development

How to Install Composer on Ubuntu: Step-by-Step Guide

8 Min Read
WordPress disable revision and autosave — wp-config constants and wp_print_scripts dequeue
Web Development

How to Disable Revisions and Autosave in WordPress

8 Min Read
Open a file selection dialog from a button click
Web Development

How to Open a File Dialog When Clicking a Button

4 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?