CINXE.COM
<!DOCTYPE html><html lang="en" data-theme-enabled="1"><head><script>window.currentUser = null;</script><script>window.shopCurrency = "EUR";</script><script>window.localCurrency = "EUR";</script><script>window.countryCode = "kh";</script><script>window.rateShopTo = {"EUR":1,"USD":1.0784426856242513,"AMD":422.9305849580972};</script><title itemprop="name">Data types</title><link href="/pack/styles.c582d23a0695e653a6d3.css" rel="stylesheet"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1.0"><meta name="apple-mobile-web-app-capable" content="yes"><script>if (window.devicePixelRatio > 1) document.cookie = 'pixelRatio=' + window.devicePixelRatio + ';path=/;expires=Tue, 19 Jan 2038 03:14:07 GMT';</script><link href="//fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic" rel="stylesheet"><link rel="apple-touch-icon-precomposed" href="/img/favicon/apple-touch-icon-precomposed.png"><link rel="canonical" href="https://javascript.info/types"><meta name="msapplication-TileColor" content="#222A2C"><meta name="msapplication-TileImage" content="/img/favicon/tileicon.png"><link rel="icon" href="/img/favicon/favicon.png"><meta itemprop="image" content="https://javascript.info/img/site_preview_en_512x512.png"><meta property="og:title" content="Data types"><meta property="og:image" content="https://javascript.info/img/site_preview_en_1200x630.png"><meta property="og:image:type" content="image/png"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="630"><meta property="fb:admins" content="100001562528165"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Data types"><meta name="twitter:site" content="@iliakan"><meta name="twitter:creator" content="@iliakan"><meta name="twitter:image" content="https://javascript.info/img/site_preview_en_512x512.png"><meta name="google-adsense-account" content="ca-pub-6204518652652613"><link rel="prev" href="/variables"><link rel="next" href="/alert-prompt-confirm"><script data-collect-dnt="true" async src="https://scripts.simpleanalyticscdn.com/latest.js"></script><script>window.GA_ID = "UA-2056213-15";</script><script>window.YANDEX_METRIKA_ID = 32184394;</script><script>{function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-2LWB61WGYJ")}</script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-2LWB61WGYJ"></script><script>window.metrika={reachGoal:function(){}},window.yandex_metrika_callbacks=[function(){try{window.metrika=new Ya.Metrika({id:YANDEX_METRIKA_ID,webvisor:!0,clickmap:!0,params:{user:window.currentUser&&window.currentUser.id}}),metrika.trackLinks({delay:150}),window.addEventListener("error",function(r){window.metrika.reachGoal("JSERROR",{src:(r.filename||r.errorUrl)+": "+(r.lineno||r.errorLine),stack:r.stack||r.error&&r.error.stack,message:r.message})})}catch(r){}}];</script><script src="//mc.yandex.ru/metrika/watch.js" async></script><script>window.RECAPTCHA_ID = "6LfmLAEVAAAAAJMykMnf7aY8nkyTRmYi2ynx51R1";</script><script src="/pack/init.c620e89b5f96f3cade19.js"></script><script src="/pack/head.7de24b11bdf2e7982166.js" defer></script><meta property="og:title" content="Data types"><meta property="og:type" content="article"><script src="/pack/tutorial.aa68ac65c8ad26a37344.js" defer></script><script src="/pack/footer.818a809d6860f4026867.js" defer></script></head><body class="no-icons"><script>window.fontTest();</script><div class="page-wrapper page-wrapper_sidebar_on"><!--[if IE]><div style="color:red;text-align:center">Sorry, Internet Explorer is not supported, please use a newer browser.</div><![endif]--><div class="sitetoolbar sitetoolbar_tutorial"><script>window.langs = [{"code":"ar","name":"Arabic"},{"code":"az","name":"Azerbaijani"},{"code":"bg","name":"Bulgarian"},{"code":"bn","name":"Bengali"},{"code":"bs","name":"Bosnian"},{"code":"ca","name":"Catalan"},{"code":"cs","name":"Czech"},{"code":"da","name":"Danish"},{"code":"de","name":"German"},{"code":"el","name":"Greek"},{"code":"en","name":"English"},{"code":"es","name":"Spanish"},{"code":"fa","name":"Persian (Farsi)"},{"code":"fi","name":"Finnish"},{"code":"fr","name":"French"},{"code":"he","name":"Hebrew"},{"code":"hi","name":"Hindi"},{"code":"hr","name":"Croatian"},{"code":"hu","name":"Hungarian"},{"code":"hy","name":"Armenian"},{"code":"id","name":"Indonesian"},{"code":"it","name":"Italian"},{"code":"ja","name":"Japanese"},{"code":"ka","name":"Georgian"},{"code":"kk","name":"Kazakh"},{"code":"km","name":"Central Khmer"},{"code":"ko","name":"Korean"},{"code":"ku","name":"Kurdish"},{"code":"ky","name":"Kyrgyz"},{"code":"lt","name":"Lithuanian"},{"code":"me","name":"Montenegrin"},{"code":"ml","name":"Malayalam"},{"code":"ms","name":"Malay"},{"code":"my","name":"Burmese"},{"code":"nl","name":"Dutch"},{"code":"no","name":"Norvegian"},{"code":"pa","name":"Punjabi"},{"code":"pl","name":"Polish"},{"code":"pt","name":"Portuguese"},{"code":"ro","name":"Romanian"},{"code":"ru","name":"Russian"},{"code":"si","name":"Sinhala"},{"code":"sk","name":"Slovak"},{"code":"sl","name":"Slovenian"},{"code":"sq","name":"Albanian"},{"code":"sr","name":"Serbian"},{"code":"ta","name":"Tamil"},{"code":"te","name":"Telugu"},{"code":"test","name":"Test"},{"code":"th","name":"Thai"},{"code":"tk","name":"Turkmen"},{"code":"tr","name":"Turkish"},{"code":"ug","name":"Uyghur"},{"code":"uk","name":"Ukrainian"},{"code":"ur","name":"Urdu"},{"code":"uz","name":"Uzbek"},{"code":"v2","name":"v2"},{"code":"vi","name":"Vietnamese"},{"code":"zh-hant","name":"Chinese Traditional"},{"code":"zh","name":"Chinese"}];</script><script>window.lang = "en";</script><div class="sitetoolbar__content"><div class="sitetoolbar__lang-switcher"><button class="sitetoolbar__dropdown-button" data-dropdown-toggler>EN</button><div class="sitetoolbar__dropdown-wrap"><div class="sitetoolbar__dropdown-body"><div class="sitetoolbar__lang-switcher-body"><div class="supported-langs supported-langs_toolbar"><div class="supported-langs__container"><ul class="supported-langs__list" style="height:200px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://ar.javascript.info/types"><span class="supported-langs__brief">AR</span><span class="supported-langs__title">عربي</span></a></li><li class="supported-langs__item supported-langs__item_current"><a class="supported-langs__link" href="https://javascript.info/types"><span class="supported-langs__brief">EN</span><span class="supported-langs__title">English</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://es.javascript.info/types"><span class="supported-langs__brief">ES</span><span class="supported-langs__title">Español</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://fa.javascript.info/types"><span class="supported-langs__brief">FA</span><span class="supported-langs__title">فارسی</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://fr.javascript.info/types"><span class="supported-langs__brief">FR</span><span class="supported-langs__title">Français</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://id.javascript.info/types"><span class="supported-langs__brief">ID</span><span class="supported-langs__title">Indonesia</span></a></li></ul><ul class="supported-langs__list" style="height:200px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://it.javascript.info/types"><span class="supported-langs__brief">IT</span><span class="supported-langs__title">Italiano</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://ja.javascript.info/types"><span class="supported-langs__brief">JA</span><span class="supported-langs__title">日本語</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://ko.javascript.info/types"><span class="supported-langs__brief">KO</span><span class="supported-langs__title">한국어</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://learn.javascript.ru/types"><span class="supported-langs__brief">RU</span><span class="supported-langs__title">Русский</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://tr.javascript.info/types"><span class="supported-langs__brief">TR</span><span class="supported-langs__title">Türkçe</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://uk.javascript.info/types"><span class="supported-langs__brief">UK</span><span class="supported-langs__title">Українська</span></a></li></ul><ul class="supported-langs__list" style="height:20px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://zh.javascript.info/types"><span class="supported-langs__brief">ZH</span><span class="supported-langs__title">简体中文</span></a></li></ul></div><div class="supported-langs__text"><p>We want to make this open-source project available for people all around the world.</p> <p><a href="https://javascript.info/translate">Help to translate</a> the content of this tutorial to your language!</p> </div></div></div></div></div></div><div class="sitetoolbar__logo-wrap"><a class="sitetoolbar__link sitetoolbar__link_logo" href="/"><img class="sitetoolbar__logo sitetoolbar__logo_normal" src="/img/sitetoolbar__logo_en.svg" width="200" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_normal sitetoolbar__logo_dark" src="/img/sitetoolbar__logo_en-white.svg" width="200" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_small" src="/img/sitetoolbar__logo_small_en.svg" width="70" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_small sitetoolbar__logo_dark" src="/img/sitetoolbar__logo_small_en-white.svg" width="70" alt="" role="presentation"/><script>Array.prototype.forEach.call(document.querySelectorAll("img.sitetoolbar__logo"),function(e){let t=document.createElement("object");t.type="image/svg+xml",t.className=e.className,t.style.cssText="left:0;top:0;position:absolute",t.onload=function(){t.onload=null,e.style.visibility="hidden"},t.data=e.src,e.parentNode.insertBefore(t,e)});</script></a></div><div class="sitetoolbar__nav-toggle-wrap"><button class="sitetoolbar__nav-toggle" type="button"></button></div><nav class="sitetoolbar__sections"><ul class="sitetoolbar__sections-list"></ul></nav><div class="sitetoolbar__right-button-wrap"><a class="sitetoolbar-right-button sitetoolbar-right-button_courses" href="/ebook"><span class="sitetoolbar-right-button__extra-text">Buy</span>EPUB/PDF</a></div><div class="sitetoolbar__login-wrap"><button class="sitetoolbar__login sitetoolbar__login_unready" data-action-login></button></div><div class="sitetoolbar__theme-switcher"><div class="theme-changer"><label class="theme-changer__label" for="theme-changer-input" data-tooltip="Change theme"><input class="theme-changer__input" type="checkbox" id="theme-changer-input" data-theme-changer="data-theme-changer"/><span class="theme-changer__icon theme-changer__icon_light-theme"></span><span class="theme-changer__icon theme-changer__icon_dark-theme"></span></label></div></div><div class="sitetoolbar__search-wrap"><div class="sitetoolbar__search-content"><button class="sitetoolbar__search-toggle" type="button"></button><form class="sitetoolbar__search" method="GET" action="/search"><div class="sitetoolbar__search-input"><div class="text-input"><input class="text-input__control" name="query" placeholder="Search on Javascript.info" required="required" type="text"/></div><button class="sitetoolbar__find" type="submit">Search</button></div></form></div></div></div><div class="tablet-menu"><div class="tablet-menu__line"><div class="tablet-menu__content"><form class="tablet-menu-search" action="/search/"><input class="tablet-menu-search__input" type="search" name="query" placeholder="Search in the tutorial" required="required"/><button class="tablet-menu-search__button" type="submit" name="type" value="articles">Search</button></form></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><a class="map" href="/tutorial/map" data-action="tutorial-map"><span class="map__text">Tutorial map</span></a></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><div class="theme-changer theme-changer_tablet-menu theme-changer_has-label"><label class="theme-changer__label" for="theme-changer-input-tablet" data-tooltip="Change theme"><input class="theme-changer__input" type="checkbox" id="theme-changer-input-tablet" data-theme-changer="data-theme-changer"/><span class="theme-changer__icon theme-changer__icon_light-theme"></span><span class="theme-changer__icon theme-changer__icon_dark-theme"></span><span class="theme-changer__label-text theme-changer__label-text_light-theme">Light theme</span><span class="theme-changer__label-text theme-changer__label-text_dark-theme">Dark theme</span></label></div></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><div class="share-icons"><span class="share-icons__title">Share</span><a class="share share_tw" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a><a class="share share_fb" href="https://www.facebook.com/sharer/sharer.php?s=100&p%5Burl%5D=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a></div></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><select class="tablet-menu__nav input-select input-select input-select_small" onchange="if(this.value) window.location.href=this.value"><option value="https://ar.javascript.info/types">عربي</option><option value="https://javascript.info/types" selected>English</option><option value="https://es.javascript.info/types">Español</option><option value="https://fa.javascript.info/types">فارسی</option><option value="https://fr.javascript.info/types">Français</option><option value="https://id.javascript.info/types">Indonesia</option><option value="https://it.javascript.info/types">Italiano</option><option value="https://ja.javascript.info/types">日本語</option><option value="https://ko.javascript.info/types">한국어</option><option value="https://learn.javascript.ru/types">Русский</option><option value="https://tr.javascript.info/types">Türkçe</option><option value="https://uk.javascript.info/types">Українська</option><option value="https://zh.javascript.info/types">简体中文</option></select></div></div></div><progress class="tutorial-progress" data-sticky value="9" max="94" data-tooltip="Lesson 9 of 94"></progress></div><div class="page page_sidebar_on page_inner_padding"><script>if(localStorage.noSidebar){document.querySelector(".page").classList.remove("page_sidebar_on");let e=document.querySelector(".page-wrapper");e&&e.classList.remove("page-wrapper_sidebar_on")}setTimeout(function(){document.querySelector(".page").classList.add("page_sidebar-animation-on")});</script><div class="page__inner"><main class="main main_width-limit"><header class="main__header"><div class="main__header-inner"><div class="main__header-group"><ol class="breadcrumbs"><li class="breadcrumbs__item breadcrumbs__item_home"><a class="breadcrumbs__link" href="/"><span class="breadcrumbs__hidden-text">Tutorial</span></a></li><li class="breadcrumbs__item" id="breadcrumb-1"><a class="breadcrumbs__link" href="/js"><span>The JavaScript language</span></a></li><li class="breadcrumbs__item" id="breadcrumb-2"><a class="breadcrumbs__link" href="/first-steps"><span>JavaScript Fundamentals</span></a></li><script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorial","item":"https://javascript.info/"},{"@type":"ListItem","position":2,"name":"The JavaScript language","item":"https://javascript.info/js"},{"@type":"ListItem","position":3,"name":"JavaScript Fundamentals","item":"https://javascript.info/first-steps"}]}</script></ol><div class="updated-at" data-tooltip="Last updated on July 9, 2024"><div class="updated-at__content">July 9, 2024</div></div></div><h1 class="main__header-title">Data types</h1></div></header><div class="content"><article class="formatted" itemscope itemtype="http://schema.org/TechArticle"><meta itemprop="name" content="Data types"><div itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="email" content="iliakan@gmail.com"><meta itemprop="name" content="Ilya Kantor"></div><div itemprop="articleBody"><p>A value in JavaScript is always of a certain type. For example, a string or a number.</p> <p>There are eight basic data types in JavaScript. Here, we’ll cover them in general and in the next chapters we’ll talk about each of them in detail.</p> <p>We can put any type in a variable. For example, a variable can at one moment be a string and then store a number:</p> <div id="j5390hoyji" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>// no error let message = "hello"; message = 123456;</code></pre> </div> </div> </div><p>Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them.</p> <h2><a class="main__anchor" name="number" href="#number">Number</a></h2><div id="wswrfvhhn4" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let n = 123; n = 12.345;</code></pre> </div> </div> </div><p>The <em>number</em> type represents both integer and floating point numbers.</p> <p>There are many operations for numbers, e.g. multiplication <code>*</code>, division <code>/</code>, addition <code>+</code>, subtraction <code>-</code>, and so on.</p> <p>Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: <code>Infinity</code>, <code>-Infinity</code> and <code>NaN</code>.</p> <ul> <li> <p><code>Infinity</code> represents the mathematical <a href="https://en.wikipedia.org/wiki/Infinity">Infinity</a> ∞. It is a special value that’s greater than any number.</p> <p>We can get it as a result of division by zero:</p> <div id="oy2bw0nx9w" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( 1 / 0 ); // Infinity</code></pre> </div> </div> </div><p>Or just reference it directly:</p> <div id="xfgdv6w4aj" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( Infinity ); // Infinity</code></pre> </div> </div> </div></li> <li> <p><code>NaN</code> represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance:</p> <div id="tjd0ggky85" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( "not a number" / 2 ); // NaN, such division is erroneous</code></pre> </div> </div> </div><p><code>NaN</code> is sticky. Any further mathematical operation on <code>NaN</code> returns <code>NaN</code>:</p> <div id="8klah9awxt" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( NaN + 1 ); // NaN alert( 3 * NaN ); // NaN alert( "not a number" / 2 - 1 ); // NaN</code></pre> </div> </div> </div><p>So, if there’s a <code>NaN</code> somewhere in a mathematical expression, it propagates to the whole result (there’s only one exception to that: <code>NaN ** 0</code> is <code>1</code>).</p> </li> </ul> <div class="important important_smart"> <div class="important__header"><span class="important__type">Mathematical operations are safe</span></div> <div class="important__content"><p>Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.</p> <p>The script will never stop with a fatal error (“die”). At worst, we’ll get <code>NaN</code> as the result.</p> </div></div> <p>Special numeric values formally belong to the “number” type. Of course they are not numbers in the common sense of this word.</p> <p>We’ll see more about working with numbers in the chapter <a href="/number">Numbers</a>.</p> <h2><a class="main__anchor" name="bigint-type" href="#bigint-type">BigInt</a></h2><p>In JavaScript, the “number” type cannot safely represent integer values larger than <code>(2<sup>53</sup>-1)</code> (that’s <code>9007199254740991</code>), or less than <code>-(2<sup>53</sup>-1)</code> for negatives.</p> <p>To be really precise, the “number” type can store larger integers (up to <code>1.7976931348623157 * 10<sup>308</sup></code>), but outside of the safe integer range <code>±(2<sup>53</sup>-1)</code> there’ll be a precision error, because not all digits fit into the fixed 64-bit storage. So an “approximate” value may be stored.</p> <p>For example, these two numbers (right above the safe range) are the same:</p> <div id="idghr8t41d" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>console.log(9007199254740991 + 1); // 9007199254740992 console.log(9007199254740991 + 2); // 9007199254740992</code></pre> </div> </div> </div><p>So to say, all odd integers greater than <code>(2<sup>53</sup>-1)</code> can’t be stored at all in the “number” type.</p> <p>For most purposes <code>±(2<sup>53</sup>-1)</code> range is quite enough, but sometimes we need the entire range of really big integers, e.g. for cryptography or microsecond-precision timestamps.</p> <p><code>BigInt</code> type was recently added to the language to represent integers of arbitrary length.</p> <p>A <code>BigInt</code> value is created by appending <code>n</code> to the end of an integer:</p> <div id="46uz8te7tz" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>// the "n" at the end means it's a BigInt const bigInt = 1234567890123456789012345678901234567890n;</code></pre> </div> </div> </div><p>As <code>BigInt</code> numbers are rarely needed, we don’t cover them here, but devoted them a separate chapter <a href="/bigint">BigInt</a>. Read it when you need such big numbers.</p> <h2><a class="main__anchor" name="string" href="#string">String</a></h2><p>A string in JavaScript must be surrounded by quotes.</p> <div id="ye4lagiq1u" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let str = "Hello"; let str2 = 'Single quotes are ok too'; let phrase = `can embed another ${str}`;</code></pre> </div> </div> </div><p>In JavaScript, there are 3 types of quotes.</p> <ol> <li>Double quotes: <code>"Hello"</code>.</li> <li>Single quotes: <code>'Hello'</code>.</li> <li>Backticks: <code>`Hello`</code>.</li> </ol> <p>Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.</p> <p>Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in <code>${…}</code>, for example:</p> <div id="3k2bs75ozs" data-trusted="1" class="code-example" data-highlight="[{"start":6,"cols":[{"start":22,"end":30}]},{"start":3,"cols":[{"start":15,"end":22}]}]"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let name = "John"; // embed a variable alert( `Hello, ${name}!` ); // Hello, John! // embed an expression alert( `the result is ${1 + 2}` ); // the result is 3</code></pre> </div> </div> </div><p>The expression inside <code>${…}</code> is evaluated and the result becomes a part of the string. We can put anything in there: a variable like <code>name</code> or an arithmetical expression like <code>1 + 2</code> or something more complex.</p> <p>Please note that this can only be done in backticks. Other quotes don’t have this embedding functionality!</p> <div id="r0sbsatebw" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} (double quotes do nothing)</code></pre> </div> </div> </div><p>We’ll cover strings more thoroughly in the chapter <a href="/string">Strings</a>.</p> <div class="important important_smart"> <div class="important__header"><span class="important__type">There is no <em>character</em> type.</span></div> <div class="important__content"><p>In some languages, there is a special “character” type for a single character. For example, in the C language and in Java it is called “char”.</p> <p>In JavaScript, there is no such type. There’s only one type: <code>string</code>. A string may consist of zero characters (be empty), one character or many of them.</p> </div></div> <h2><a class="main__anchor" name="boolean-logical-type" href="#boolean-logical-type">Boolean (logical type)</a></h2><p>The boolean type has only two values: <code>true</code> and <code>false</code>.</p> <p>This type is commonly used to store yes/no values: <code>true</code> means “yes, correct”, and <code>false</code> means “no, incorrect”.</p> <p>For instance:</p> <div id="xlvir3rcmy" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let nameFieldChecked = true; // yes, name field is checked let ageFieldChecked = false; // no, age field is not checked</code></pre> </div> </div> </div><p>Boolean values also come as a result of comparisons:</p> <div id="b421g81uu2" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let isGreater = 4 > 1; alert( isGreater ); // true (the comparison result is "yes")</code></pre> </div> </div> </div><p>We’ll cover booleans more deeply in the chapter <a href="/logical-operators">Logical operators</a>.</p> <h2><a class="main__anchor" name="the-null-value" href="#the-null-value">The “null” value</a></h2><p>The special <code>null</code> value does not belong to any of the types described above.</p> <p>It forms a separate type of its own which contains only the <code>null</code> value:</p> <div id="u7qiv9zw6b" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let age = null;</code></pre> </div> </div> </div><p>In JavaScript, <code>null</code> is not a “reference to a non-existing object” or a “null pointer” like in some other languages.</p> <p>It’s just a special value which represents “nothing”, “empty” or “value unknown”.</p> <p>The code above states that <code>age</code> is unknown.</p> <h2><a class="main__anchor" name="the-undefined-value" href="#the-undefined-value">The “undefined” value</a></h2><p>The special value <code>undefined</code> also stands apart. It makes a type of its own, just like <code>null</code>.</p> <p>The meaning of <code>undefined</code> is “value is not assigned”.</p> <p>If a variable is declared, but not assigned, then its value is <code>undefined</code>:</p> <div id="t23h6dxs0c" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let age; alert(age); // shows "undefined"</code></pre> </div> </div> </div><p>Technically, it is possible to explicitly assign <code>undefined</code> to a variable:</p> <div id="ahmxo18j7k" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let age = 100; // change the value to undefined age = undefined; alert(age); // "undefined"</code></pre> </div> </div> </div><p>…But we don’t recommend doing that. Normally, one uses <code>null</code> to assign an “empty” or “unknown” value to a variable, while <code>undefined</code> is reserved as a default initial value for unassigned things.</p> <h2><a class="main__anchor" name="objects-and-symbols" href="#objects-and-symbols">Objects and Symbols</a></h2><p>The <code>object</code> type is special.</p> <p>All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.</p> <p>Being that important, objects deserve a special treatment. We’ll deal with them later in the chapter <a href="/object">Objects</a>, after we learn more about primitives.</p> <p>The <code>symbol</code> type is used to create unique identifiers for objects. We have to mention it here for the sake of completeness, but also postpone the details till we know objects.</p> <h2><a class="main__anchor" name="type-typeof" href="#type-typeof">The typeof operator</a></h2><p>The <code>typeof</code> operator returns the type of the operand. It’s useful when we want to process values of different types differently or just want to do a quick check.</p> <p>A call to <code>typeof x</code> returns a string with the type name:</p> <div id="ae9ftwcr1y" data-trusted="1" class="code-example" data-highlight="[{"start":16,"end":16},{"start":14,"end":14},{"start":12,"end":12}]"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>typeof undefined // "undefined" typeof 0 // "number" typeof 10n // "bigint" typeof true // "boolean" typeof "foo" // "string" typeof Symbol("id") // "symbol" typeof Math // "object" (1) typeof null // "object" (2) typeof alert // "function" (3)</code></pre> </div> </div> </div><p>The last three lines may need additional explanation:</p> <ol> <li><code>Math</code> is a built-in object that provides mathematical operations. We will learn it in the chapter <a href="/number">Numbers</a>. Here, it serves just as an example of an object.</li> <li>The result of <code>typeof null</code> is <code>"object"</code>. That’s an officially recognized error in <code>typeof</code>, coming from very early days of JavaScript and kept for compatibility. Definitely, <code>null</code> is not an object. It is a special value with a separate type of its own. The behavior of <code>typeof</code> is wrong here.</li> <li>The result of <code>typeof alert</code> is <code>"function"</code>, because <code>alert</code> is a function. We’ll study functions in the next chapters where we’ll also see that there’s no special “function” type in JavaScript. Functions belong to the object type. But <code>typeof</code> treats them differently, returning <code>"function"</code>. That also comes from the early days of JavaScript. Technically, such behavior isn’t correct, but can be convenient in practice.</li> </ol> <div class="important important_smart"> <div class="important__header"><span class="important__type">The <code>typeof(x)</code> syntax</span></div> <div class="important__content"><p>You may also come across another syntax: <code>typeof(x)</code>. It’s the same as <code>typeof x</code>.</p> <p>To put it clear: <code>typeof</code> is an operator, not a function. The parentheses here aren’t a part of <code>typeof</code>. It’s the kind of parentheses used for mathematical grouping.</p> <p>Usually, such parentheses contain a mathematical expression, such as <code>(2 + 2)</code>, but here they contain only one argument <code>(x)</code>. Syntactically, they allow to avoid a space between the <code>typeof</code> operator and its argument, and some people like it.</p> <p>Some people prefer <code>typeof(x)</code>, although the <code>typeof x</code> syntax is much more common.</p> </div></div> <h2><a class="main__anchor" name="summary" href="#summary">Summary</a></h2><p>There are 8 basic data types in JavaScript.</p> <ul> <li>Seven primitive data types: <ul> <li><code>number</code> for numbers of any kind: integer or floating-point, integers are limited by <code>±(2<sup>53</sup>-1)</code>.</li> <li><code>bigint</code> for integer numbers of arbitrary length.</li> <li><code>string</code> for strings. A string may have zero or more characters, there’s no separate single-character type.</li> <li><code>boolean</code> for <code>true</code>/<code>false</code>.</li> <li><code>null</code> for unknown values – a standalone type that has a single value <code>null</code>.</li> <li><code>undefined</code> for unassigned values – a standalone type that has a single value <code>undefined</code>.</li> <li><code>symbol</code> for unique identifiers.</li> </ul> </li> <li>And one non-primitive data type: <ul> <li><code>object</code> for more complex data structures.</li> </ul> </li> </ul> <p>The <code>typeof</code> operator allows us to see which type is stored in a variable.</p> <ul> <li>Usually used as <code>typeof x</code>, but <code>typeof(x)</code> is also possible.</li> <li>Returns a string with the name of the type, like <code>"string"</code>.</li> <li>For <code>null</code> returns <code>"object"</code> – this is an error in the language, it’s not actually an object.</li> </ul> <p>In the next chapters, we’ll concentrate on primitive values and once we’re familiar with them, we’ll move on to objects.</p> </div></article><div class="tasks formatted"><h2 class="tasks__title" id="tasks"><a class="tasks__title-anchor main__anchor main__anchor main__anchor_noicon" href="#tasks">Tasks</a></h2><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#string-quotes" name="string-quotes">String quotes</a></h3><a class="task__open-link" href="/task/string-quotes" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 5</span></div><div class="task__content"><div class="task__formatted"><p>What is the output of the script?</p> <div id="c6w4lwoowi" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let name = "Ilya"; alert( `hello ${1}` ); // ? alert( `hello ${"name"}` ); // ? alert( `hello ${name}` ); // ?</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>Backticks embed the expression inside <code>${...}</code> into the string.</p> <div id="38vu9cpt17" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let name = "Ilya"; // the expression is a number 1 alert( `hello ${1}` ); // hello 1 // the expression is a string "name" alert( `hello ${"name"}` ); // hello name // the expression is a variable, embed it alert( `hello ${name}` ); // hello Ilya</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div></div></div><div class="page__nav-wrap"><a class="page__nav page__nav_prev" href="/variables" data-tooltip="Variables"><span class="page__nav-text"><span class="page__nav-text-shortcut"></span></span><span class="page__nav-text-alternate">Previous lesson</span></a><a class="page__nav page__nav_next" href="/alert-prompt-confirm" data-tooltip="Interaction: alert, prompt, confirm"><span class="page__nav-text"><span class="page__nav-text-shortcut"></span></span><span class="page__nav-text-alternate">Next lesson</span></a></div><div class="article-tablet-foot tablet-only"><div class="article-tablet-foot__layout"><div class="share-icons"><span class="share-icons__title">Share</span><a class="share share_tw" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a><a class="share share_fb" href="https://www.facebook.com/sharer/sharer.php?s=100&p%5Burl%5D=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a></div><div class="article-tablet-foot__map"><a class="map" href="/tutorial/map" data-action="tutorial-map"><span class="map__text">Tutorial map</span></a></div></div></div><div class="banner-bottom-carbon"><div id="javascriptinfo_iconbar"></div></div><script src="//m.servedby-buysellads.com/monetization.custom.js" id="javascriptinfo_script" async></script><script>document.getElementById("javascriptinfo_script").onload=function(){_bsa.init("custom","CKYDEK3U","placement:javascriptinfo_iconbar",{target:"#javascriptinfo_iconbar",template:'\n <div class="iconBarFlex">\n <a href="##statlink##" class="iconBarLink" rel="sponsored noopener" target="_blank" title="##company## — ##tagline##">\n <div class="iconBarImage" style="background-color: ##backgroundColor##;">\n <img height="30" width="30" src="##image##" alt="##company## logo">\n </div>\n <div class="iconBarText">\n <div class="iconBarTagline">##companyTagline##</div>\n <div class="iconBarDescription">##description##</div>\n </div>\n </a>\n <a href="##ad_via_link##" class="iconBarVia" rel="sponsored noopener" target="_blank">ads via BuySellAds</a>\n </div>\n '})};</script><div class="comments formatted" id="comments"><div class="comments__header"><h2 class="comments__header-title"><a href="#comments" name="comments">Comments</a></h2><div class="comments__read-before"><span class="comments__read-before-link">read this before commenting…</span><div class="comments__read-before-popup"><div class="comments__read-before-popup-i"><ul><li>If you have suggestions what to improve - please <a href="https://github.com/javascript-tutorial/en.javascript.info/issues/new">submit a GitHub issue</a> or a pull request instead of commenting.</li><li>If you can't understand something in the article – please elaborate.</li><li>To insert few words of code, use the <code><code></code> tag, for several lines – wrap them in <code><pre></code> tag, for more than 10 lines – use a sandbox (<a href='https://plnkr.co/edit/?p=preview'>plnkr</a>, <a href='https://jsbin.com'>jsbin</a>, <a href='http://codepen.io'>codepen</a>…)</li></ul></div></div></div></div><div id="disqus_thread"></div><script>var disqus_config = function() { if (!this.page) this.page = {}; Object.assign(this.page, {"url":"https:\/\/javascript.info\/types","identifier":"\/types"}); };</script><script>var disqus_shortname = "javascriptinfo";</script><script>var disqus_enabled = true;</script></div></script></main></div><div class="sidebar page__sidebar sidebar sidebar_sticky-footer"><button class="sidebar__toggle" data-sidebar-toggle></button><a class="map" href="/tutorial/map" data-action="tutorial-map" data-tooltip="Tutorial map"></a><div class="sidebar__inner"><div class="sidebar__content"><div class="sidebar__section"><h4 class="sidebar__section-title">Chapter</h4><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="/first-steps">JavaScript Fundamentals</a></li></ul></nav></div><div class="sidebar__section"><h4 class="sidebar__section-title">Lesson navigation</h4><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="#number">Number</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#bigint-type">BigInt</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#string">String</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#boolean-logical-type">Boolean (logical type)</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#the-null-value">The “null” value</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#the-undefined-value">The “undefined” value</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#objects-and-symbols">Objects and Symbols</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#type-typeof">The typeof operator</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#summary">Summary</a></li></ul></nav></div><div class="sidebar__section"><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="#tasks">Tasks (1)</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#comments">Comments</a></li></ul></nav></div><div class="sidebar__section"><div class="sidebar__section-title">Share</div><a class="share share_tw sidebar__share" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a><a class="share share_fb sidebar__share" href="https://www.facebook.com/sharer/sharer.php?s=100&p[url]=https%3A%2F%2Fjavascript.info%2Ftypes" rel="nofollow"></a></div><div class="sidebar__section"><a class="sidebar__link" href="https://github.com/javascript-tutorial/en.javascript.info/blob/master/1-js/02-first-steps/05-types" rel="nofollow">Edit on GitHub</a></div><div class="sidebar__section" id="sponsorBar"><div class="sidebar__section-title" id="sponsorBarTitle"></div><div id="sponsorBarContent"></div></div><script>window.initSponsorBar()</script></div></div></div></div></div><div class="page-footer"><ul class="page-footer__list"><li class="page-footer__item page-footer__item_copy">© 2007—2025 Ilya Kantor</li><li class="page-footer__item page-footer__item_about"><a class="page-footer__link" href="/about">about the project</a></li><li class="page-footer__item page-footer__item_contact"><a class="page-footer__link" href="/about#contact-us">contact us</a></li><li class="page-footer__item page-footer__item_terms"><a class="page-footer__link" href="/terms">terms of usage</a></li><li class="page-footer__item page-footer__item_privacy"><a class="page-footer__link" href="/privacy">privacy policy</a></li></ul></div></body></html>