How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the First Character of a String in JavaScript
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • 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.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

WordPress default posts per page — get_option reads the Settings Reading value
Web Development

How to Get the Default Posts Per Page Value in WordPress

5 Min Read
Replace jQuery .each with vanilla JavaScript loops
Web Development

How to Replace jQuery’s .each() with Vanilla JavaScript

4 Min Read
Check if Laravel scheduler is running (cron + php artisan schedule:run)
Web Development

How to Check if Laravel Scheduler Is Running (Cron + Logs)

6 Min Read
CSS print styles shown in a clean print preview layout
Web Development

How to Add CSS Print Styles for Printer and Print Screen

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • 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?