CINXE.COM
JavaScript syntax - Wikipedia
<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-toc-available" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>JavaScript syntax - Wikipedia</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-toc-available";var cookie=document.cookie.match(/(?:^|; )enwikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy", "wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"0c478a20-838b-477b-8138-ced2a17c4b82","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"JavaScript_syntax","wgTitle":"JavaScript syntax","wgCurRevisionId":1258701791,"wgRevisionId":1258701791,"wgArticleId":3824186,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Articles with short description","Short description is different from Wikidata","Wikipedia articles in need of updating from November 2020","All Wikipedia articles in need of updating","Use dmy dates from April 2022","All articles with unsourced statements","Articles with unsourced statements from January 2021","Articles with excerpts","Webarchive template wayback links","Articles with example JavaScript code","JavaScript","Programming language syntax"],"wgPageViewLanguage": "en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"JavaScript_syntax","wgRelevantArticleId":3824186,"wgIsProbablyEditable":true,"wgRelevantPageIsProbablyEditable":true,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgNoticeProject":"wikipedia","wgCiteReferencePreviewsActive":false,"wgFlaggedRevsParams":{"tags":{"status":{"levels":1}}},"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":true,"wgPopupsFlags":0,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","pageVariantFallbacks":"en"},"wgMFDisplayWikibaseDescriptions":{"search":true,"watchlist":true,"tagline":false,"nearby":true},"wgWMESchemaEditAttemptStepOversample":false,"wgWMEPageLength":90000,"wgRelatedArticlesCompat":[],"wgEditSubmitButtonLabelPublish":true,"wgULSPosition":"interlanguage","wgULSisCompactLinksEnabled":false,"wgVector2022LanguageInHeader":true,"wgULSisLanguageSelectorEmpty":false,"wgWikibaseItemId":"Q1024422","wgCheckUserClientHintsHeadersJsApi":[ "brands","architecture","bitness","fullVersionList","mobile","model","platform","platformVersion"],"GEHomepageSuggestedEditsEnableTopics":true,"wgGETopicsMatchModeEnabled":false,"wgGEStructuredTaskRejectionReasonTextInputEnabled":false,"wgGELevelingUpEnabledForUser":false};RLSTATE={"ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading","ext.cite.styles":"ready","ext.pygments":"ready","ext.math.styles":"ready","skins.vector.search.codex.styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready","jquery.makeCollapsible.styles":"ready","ext.wikimediamessages.styles":"ready","ext.visualEditor.desktopArticleTarget.noscript":"ready","ext.uls.interlanguage":"ready","wikibase.client.init":"ready","ext.wikimediaBadges":"ready"};RLPAGEMODULES=["ext.cite.ux-enhancements","ext.pygments.view","mediawiki.page.media","site","mediawiki.page.ready","jquery.makeCollapsible","mediawiki.toc", "skins.vector.js","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.gadget.ReferenceTooltips","ext.gadget.switcher","ext.urlShortener.toolbar","ext.centralauth.centralautologin","mmv.bootstrap","ext.popups","ext.visualEditor.desktopArticleTarget.init","ext.visualEditor.targetLoader","ext.echo.centralauth","ext.eventLogging","ext.wikimediaEvents","ext.navigationTiming","ext.uls.interface","ext.cx.eventlogging.campaigns","ext.cx.uls.quick.actions","wikibase.client.vector-2022","ext.checkUser.clientHints","ext.growthExperiments.SuggestedEditSession","wikibase.sidebar.tracking"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=en&modules=ext.cite.styles%7Cext.math.styles%7Cext.pygments%2CwikimediaBadges%7Cext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediamessages.styles%7Cjquery.makeCollapsible.styles%7Cskins.vector.icons%2Cstyles%7Cskins.vector.search.codex.styles%7Cwikibase.client.init&only=styles&skin=vector-2022"> <script async="" src="/w/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=vector-2022"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=en&modules=site.styles&only=styles&skin=vector-2022"> <meta name="generator" content="MediaWiki 1.44.0-wmf.5"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/e/e4/Source_code_in_Javascript.png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="751"> <meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Source_code_in_Javascript.png/800px-Source_code_in_Javascript.png"> <meta property="og:image:width" content="800"> <meta property="og:image:height" content="500"> <meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Source_code_in_Javascript.png/640px-Source_code_in_Javascript.png"> <meta property="og:image:width" content="640"> <meta property="og:image:height" content="400"> <meta name="viewport" content="width=1120"> <meta property="og:title" content="JavaScript syntax - Wikipedia"> <meta property="og:type" content="website"> <link rel="preconnect" href="//upload.wikimedia.org"> <link rel="alternate" media="only screen and (max-width: 640px)" href="//en.m.wikipedia.org/wiki/JavaScript_syntax"> <link rel="alternate" type="application/x-wiki" title="Edit this page" href="/w/index.php?title=JavaScript_syntax&action=edit"> <link rel="apple-touch-icon" href="/static/apple-touch/wikipedia.png"> <link rel="icon" href="/static/favicon/wikipedia.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="Wikipedia (en)"> <link rel="EditURI" type="application/rsd+xml" href="//en.wikipedia.org/w/api.php?action=rsd"> <link rel="canonical" href="https://en.wikipedia.org/wiki/JavaScript_syntax"> <link rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en"> <link rel="alternate" type="application/atom+xml" title="Wikipedia Atom feed" href="/w/index.php?title=Special:RecentChanges&feed=atom"> <link rel="dns-prefetch" href="//meta.wikimedia.org" /> <link rel="dns-prefetch" href="//login.wikimedia.org"> </head> <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject mw-editable page-JavaScript_syntax rootpage-JavaScript_syntax skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">Jump to content</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="Main menu" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">Main menu</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">Main menu</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">hide</button> </div> <div id="p-navigation" class="vector-menu mw-portlet mw-portlet-navigation" > <div class="vector-menu-heading"> Navigation </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-mainpage-description" class="mw-list-item"><a href="/wiki/Main_Page" title="Visit the main page [z]" accesskey="z"><span>Main page</span></a></li><li id="n-contents" class="mw-list-item"><a href="/wiki/Wikipedia:Contents" title="Guides to browsing Wikipedia"><span>Contents</span></a></li><li id="n-currentevents" class="mw-list-item"><a href="/wiki/Portal:Current_events" title="Articles related to current events"><span>Current events</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/wiki/Special:Random" title="Visit a randomly selected article [x]" accesskey="x"><span>Random article</span></a></li><li id="n-aboutsite" class="mw-list-item"><a href="/wiki/Wikipedia:About" title="Learn about Wikipedia and how it works"><span>About Wikipedia</span></a></li><li id="n-contactpage" class="mw-list-item"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us" title="How to contact Wikipedia"><span>Contact us</span></a></li> </ul> </div> </div> <div id="p-interaction" class="vector-menu mw-portlet mw-portlet-interaction" > <div class="vector-menu-heading"> Contribute </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-help" class="mw-list-item"><a href="/wiki/Help:Contents" title="Guidance on how to use and edit Wikipedia"><span>Help</span></a></li><li id="n-introduction" class="mw-list-item"><a href="/wiki/Help:Introduction" title="Learn how to edit Wikipedia"><span>Learn to edit</span></a></li><li id="n-portal" class="mw-list-item"><a href="/wiki/Wikipedia:Community_portal" title="The hub for editors"><span>Community portal</span></a></li><li id="n-recentchanges" class="mw-list-item"><a href="/wiki/Special:RecentChanges" title="A list of recent changes to Wikipedia [r]" accesskey="r"><span>Recent changes</span></a></li><li id="n-upload" class="mw-list-item"><a href="/wiki/Wikipedia:File_upload_wizard" title="Add images or other media for use on Wikipedia"><span>Upload file</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/wiki/Main_Page" class="mw-logo"> <img class="mw-logo-icon" src="/static/images/icons/wikipedia.png" alt="" aria-hidden="true" height="50" width="50"> <span class="mw-logo-container skin-invert"> <img class="mw-logo-wordmark" alt="Wikipedia" src="/static/images/mobile/copyright/wikipedia-wordmark-en.svg" style="width: 7.5em; height: 1.125em;"> <img class="mw-logo-tagline" alt="The Free Encyclopedia" src="/static/images/mobile/copyright/wikipedia-tagline-en.svg" width="117" height="13" style="width: 7.3125em; height: 0.8125em;"> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/wiki/Special:Search" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" title="Search Wikipedia [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/w/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search Wikipedia" aria-label="Search Wikipedia" autocapitalize="sentences" title="Search Wikipedia [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <nav class="vector-user-links vector-user-links-wide" aria-label="Personal tools"> <div class="vector-user-links-main"> <div id="p-vector-user-menu-preferences" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-userpage" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-dropdown" class="vector-dropdown " title="Change the appearance of the page's font size, width, and color" > <input type="checkbox" id="vector-appearance-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-appearance-dropdown" class="vector-dropdown-checkbox " aria-label="Appearance" > <label id="vector-appearance-dropdown-label" for="vector-appearance-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-appearance mw-ui-icon-wikimedia-appearance"></span> <span class="vector-dropdown-label-text">Appearance</span> </label> <div class="vector-dropdown-content"> <div id="vector-appearance-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div id="p-vector-user-menu-notifications" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&utm_medium=sidebar&utm_campaign=C13_en.wikipedia.org&uselang=en" class=""><span>Donate</span></a> </li> <li id="pt-createaccount-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:CreateAccount&returnto=JavaScript+syntax" title="You are encouraged to create an account and log in; however, it is not mandatory" class=""><span>Create account</span></a> </li> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:UserLogin&returnto=JavaScript+syntax" title="You're encouraged to log in; however, it's not mandatory. [o]" accesskey="o" class=""><span>Log in</span></a> </li> </ul> </div> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="Log in and more options" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="Personal tools" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">Personal tools</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal user-links-collapsible-item" title="User menu" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport" class="user-links-collapsible-item mw-list-item"><a href="https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&utm_medium=sidebar&utm_campaign=C13_en.wikipedia.org&uselang=en"><span>Donate</span></a></li><li id="pt-createaccount" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:CreateAccount&returnto=JavaScript+syntax" title="You are encouraged to create an account and log in; however, it is not mandatory"><span class="vector-icon mw-ui-icon-userAdd mw-ui-icon-wikimedia-userAdd"></span> <span>Create account</span></a></li><li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:UserLogin&returnto=JavaScript+syntax" title="You're encouraged to log in; however, it's not mandatory. [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>Log in</span></a></li> </ul> </div> </div> <div id="p-user-menu-anon-editor" class="vector-menu mw-portlet mw-portlet-user-menu-anon-editor" > <div class="vector-menu-heading"> Pages for logged out editors <a href="/wiki/Help:Introduction" aria-label="Learn more about editing"><span>learn more</span></a> </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-anoncontribs" class="mw-list-item"><a href="/wiki/Special:MyContributions" title="A list of edits made from this IP address [y]" accesskey="y"><span>Contributions</span></a></li><li id="pt-anontalk" class="mw-list-item"><a href="/wiki/Special:MyTalk" title="Discussion about edits from this IP address [n]" accesskey="n"><span>Talk</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"><!-- CentralNotice --></div> </div> <div class="vector-column-start"> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <div class="vector-sticky-pinned-container"> <nav id="mw-panel-toc" aria-label="Contents" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">Contents</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">hide</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">(Top)</div> </a> </li> <li id="toc-Origins" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Origins"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span> <span>Origins</span> </div> </a> <ul id="toc-Origins-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Basics" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Basics"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span> <span>Basics</span> </div> </a> <button aria-controls="toc-Basics-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Basics subsection</span> </button> <ul id="toc-Basics-sublist" class="vector-toc-list"> <li id="toc-Case_sensitivity" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Case_sensitivity"> <div class="vector-toc-text"> <span class="vector-toc-numb">2.1</span> <span>Case sensitivity</span> </div> </a> <ul id="toc-Case_sensitivity-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Whitespace_and_semicolons" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Whitespace_and_semicolons"> <div class="vector-toc-text"> <span class="vector-toc-numb">2.2</span> <span>Whitespace and semicolons</span> </div> </a> <ul id="toc-Whitespace_and_semicolons-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Comments" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Comments"> <div class="vector-toc-text"> <span class="vector-toc-numb">2.3</span> <span>Comments</span> </div> </a> <ul id="toc-Comments-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Variables" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Variables"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span> <span>Variables</span> </div> </a> <button aria-controls="toc-Variables-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Variables subsection</span> </button> <ul id="toc-Variables-sublist" class="vector-toc-list"> <li id="toc-Scoping_and_hoisting" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Scoping_and_hoisting"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.1</span> <span>Scoping and hoisting</span> </div> </a> <ul id="toc-Scoping_and_hoisting-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Declaration_and_assignment" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Declaration_and_assignment"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.2</span> <span>Declaration and assignment</span> </div> </a> <ul id="toc-Declaration_and_assignment-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Examples" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Examples"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.3</span> <span>Examples</span> </div> </a> <ul id="toc-Examples-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Primitive_data_types" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Primitive_data_types"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span> <span>Primitive data types</span> </div> </a> <button aria-controls="toc-Primitive_data_types-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Primitive data types subsection</span> </button> <ul id="toc-Primitive_data_types-sublist" class="vector-toc-list"> <li id="toc-Undefined" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Undefined"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.1</span> <span>Undefined</span> </div> </a> <ul id="toc-Undefined-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Number" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Number"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.2</span> <span>Number</span> </div> </a> <ul id="toc-Number-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-BigInt" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#BigInt"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.3</span> <span>BigInt</span> </div> </a> <ul id="toc-BigInt-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-String" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#String"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.4</span> <span>String</span> </div> </a> <ul id="toc-String-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Boolean" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Boolean"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.5</span> <span>Boolean</span> </div> </a> <ul id="toc-Boolean-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Type_conversion" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Type_conversion"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.6</span> <span>Type conversion</span> </div> </a> <ul id="toc-Type_conversion-sublist" class="vector-toc-list"> <li id="toc-Boolean_type_conversion" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Boolean_type_conversion"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.6.1</span> <span>Boolean type conversion</span> </div> </a> <ul id="toc-Boolean_type_conversion-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Symbol" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Symbol"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.7</span> <span>Symbol</span> </div> </a> <ul id="toc-Symbol-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Native_objects" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Native_objects"> <div class="vector-toc-text"> <span class="vector-toc-numb">5</span> <span>Native objects</span> </div> </a> <button aria-controls="toc-Native_objects-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Native objects subsection</span> </button> <ul id="toc-Native_objects-sublist" class="vector-toc-list"> <li id="toc-Array" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Array"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.1</span> <span>Array</span> </div> </a> <ul id="toc-Array-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Date" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Date"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.2</span> <span>Date</span> </div> </a> <ul id="toc-Date-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Error" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Error"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.3</span> <span>Error</span> </div> </a> <ul id="toc-Error-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Math" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Math"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.4</span> <span>Math</span> </div> </a> <ul id="toc-Math-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Regular_expression" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Regular_expression"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5</span> <span>Regular expression</span> </div> </a> <ul id="toc-Regular_expression-sublist" class="vector-toc-list"> <li id="toc-Character_classes" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Character_classes"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.1</span> <span>Character classes</span> </div> </a> <ul id="toc-Character_classes-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Character_matching" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Character_matching"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.2</span> <span>Character matching</span> </div> </a> <ul id="toc-Character_matching-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Repeaters" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Repeaters"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.3</span> <span>Repeaters</span> </div> </a> <ul id="toc-Repeaters-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Anchors" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Anchors"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.4</span> <span>Anchors</span> </div> </a> <ul id="toc-Anchors-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Subexpression" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Subexpression"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.5</span> <span>Subexpression</span> </div> </a> <ul id="toc-Subexpression-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Flags" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Flags"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.6</span> <span>Flags</span> </div> </a> <ul id="toc-Flags-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Advanced_methods" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Advanced_methods"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.7</span> <span>Advanced methods</span> </div> </a> <ul id="toc-Advanced_methods-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Capturing_groups" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Capturing_groups"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.5.8</span> <span>Capturing groups</span> </div> </a> <ul id="toc-Capturing_groups-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Function" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Function"> <div class="vector-toc-text"> <span class="vector-toc-numb">5.6</span> <span>Function</span> </div> </a> <ul id="toc-Function-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Operators" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Operators"> <div class="vector-toc-text"> <span class="vector-toc-numb">6</span> <span>Operators</span> </div> </a> <button aria-controls="toc-Operators-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Operators subsection</span> </button> <ul id="toc-Operators-sublist" class="vector-toc-list"> <li id="toc-Arithmetic" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Arithmetic"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.1</span> <span>Arithmetic</span> </div> </a> <ul id="toc-Arithmetic-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Assignment" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Assignment"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.2</span> <span>Assignment</span> </div> </a> <ul id="toc-Assignment-sublist" class="vector-toc-list"> <li id="toc-Destructuring_assignment" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Destructuring_assignment"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.2.1</span> <span>Destructuring assignment</span> </div> </a> <ul id="toc-Destructuring_assignment-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Spread/rest_operator" class="vector-toc-list-item vector-toc-level-3"> <a class="vector-toc-link" href="#Spread/rest_operator"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.2.2</span> <span>Spread/rest operator</span> </div> </a> <ul id="toc-Spread/rest_operator-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Comparison" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Comparison"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.3</span> <span>Comparison</span> </div> </a> <ul id="toc-Comparison-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Logical" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Logical"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.4</span> <span>Logical</span> </div> </a> <ul id="toc-Logical-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Logical_assignment" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Logical_assignment"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.5</span> <span>Logical assignment</span> </div> </a> <ul id="toc-Logical_assignment-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Bitwise" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Bitwise"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.6</span> <span>Bitwise</span> </div> </a> <ul id="toc-Bitwise-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Bitwise_Assignment" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Bitwise_Assignment"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.7</span> <span>Bitwise Assignment</span> </div> </a> <ul id="toc-Bitwise_Assignment-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-String_2" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#String_2"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.8</span> <span>String</span> </div> </a> <ul id="toc-String_2-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-??" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#??"> <div class="vector-toc-text"> <span class="vector-toc-numb">6.9</span> <span>??</span> </div> </a> <ul id="toc-??-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Control_structures" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Control_structures"> <div class="vector-toc-text"> <span class="vector-toc-numb">7</span> <span>Control structures</span> </div> </a> <button aria-controls="toc-Control_structures-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Control structures subsection</span> </button> <ul id="toc-Control_structures-sublist" class="vector-toc-list"> <li id="toc-Compound_statements" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Compound_statements"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.1</span> <span>Compound statements</span> </div> </a> <ul id="toc-Compound_statements-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-If_..._else" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#If_..._else"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.2</span> <span>If ... else</span> </div> </a> <ul id="toc-If_..._else-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Conditional_(ternary)_operator" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Conditional_(ternary)_operator"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.3</span> <span>Conditional (ternary) operator</span> </div> </a> <ul id="toc-Conditional_(ternary)_operator-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Switch_statement" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Switch_statement"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.4</span> <span>Switch statement</span> </div> </a> <ul id="toc-Switch_statement-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-For_loop" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#For_loop"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.5</span> <span>For loop</span> </div> </a> <ul id="toc-For_loop-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-For_..._in_loop" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#For_..._in_loop"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.6</span> <span>For ... in loop</span> </div> </a> <ul id="toc-For_..._in_loop-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-While_loop" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#While_loop"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.7</span> <span>While loop</span> </div> </a> <ul id="toc-While_loop-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Do_..._while_loop" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Do_..._while_loop"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.8</span> <span>Do ... while loop</span> </div> </a> <ul id="toc-Do_..._while_loop-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-With" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#With"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.9</span> <span>With</span> </div> </a> <ul id="toc-With-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Labels" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Labels"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.10</span> <span>Labels</span> </div> </a> <ul id="toc-Labels-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Functions" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Functions"> <div class="vector-toc-text"> <span class="vector-toc-numb">8</span> <span>Functions</span> </div> </a> <button aria-controls="toc-Functions-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Functions subsection</span> </button> <ul id="toc-Functions-sublist" class="vector-toc-list"> <li id="toc-Async/await" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Async/await"> <div class="vector-toc-text"> <span class="vector-toc-numb">8.1</span> <span>Async/await</span> </div> </a> <ul id="toc-Async/await-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Objects" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Objects"> <div class="vector-toc-text"> <span class="vector-toc-numb">9</span> <span>Objects</span> </div> </a> <button aria-controls="toc-Objects-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Objects subsection</span> </button> <ul id="toc-Objects-sublist" class="vector-toc-list"> <li id="toc-Creating_objects" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Creating_objects"> <div class="vector-toc-text"> <span class="vector-toc-numb">9.1</span> <span>Creating objects</span> </div> </a> <ul id="toc-Creating_objects-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Methods" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Methods"> <div class="vector-toc-text"> <span class="vector-toc-numb">9.2</span> <span>Methods</span> </div> </a> <ul id="toc-Methods-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Constructors" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Constructors"> <div class="vector-toc-text"> <span class="vector-toc-numb">9.3</span> <span>Constructors</span> </div> </a> <ul id="toc-Constructors-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Inheritance" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Inheritance"> <div class="vector-toc-text"> <span class="vector-toc-numb">9.4</span> <span>Inheritance</span> </div> </a> <ul id="toc-Inheritance-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Exception_handling" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Exception_handling"> <div class="vector-toc-text"> <span class="vector-toc-numb">10</span> <span>Exception handling</span> </div> </a> <ul id="toc-Exception_handling-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Native_functions_and_methods" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Native_functions_and_methods"> <div class="vector-toc-text"> <span class="vector-toc-numb">11</span> <span>Native functions and methods</span> </div> </a> <button aria-controls="toc-Native_functions_and_methods-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Native functions and methods subsection</span> </button> <ul id="toc-Native_functions_and_methods-sublist" class="vector-toc-list"> <li id="toc-eval_(expression)" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#eval_(expression)"> <div class="vector-toc-text"> <span class="vector-toc-numb">11.1</span> <span>eval (expression)</span> </div> </a> <ul id="toc-eval_(expression)-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-See_also" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#See_also"> <div class="vector-toc-text"> <span class="vector-toc-numb">12</span> <span>See also</span> </div> </a> <ul id="toc-See_also-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-References" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#References"> <div class="vector-toc-text"> <span class="vector-toc-numb">13</span> <span>References</span> </div> </a> <ul id="toc-References-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Further_reading" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Further_reading"> <div class="vector-toc-text"> <span class="vector-toc-numb">14</span> <span>Further reading</span> </div> </a> <ul id="toc-Further_reading-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-External_links" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#External_links"> <div class="vector-toc-text"> <span class="vector-toc-numb">15</span> <span>External links</span> </div> </a> <ul id="toc-External_links-sublist" class="vector-toc-list"> </ul> </li> </ul> </div> </div> </nav> </div> </div> <div class="mw-content-container"> <main id="content" class="mw-body"> <header class="mw-body-header vector-page-titlebar"> <nav aria-label="Contents" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">JavaScript syntax</span></h1> <div id="p-lang-btn" class="vector-dropdown mw-portlet mw-portlet-lang" > <input type="checkbox" id="p-lang-btn-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-p-lang-btn" class="vector-dropdown-checkbox mw-interlanguage-selector" aria-label="Go to an article in another language. Available in 8 languages" > <label id="p-lang-btn-label" for="p-lang-btn-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--action-progressive mw-portlet-lang-heading-8" aria-hidden="true" ><span class="vector-icon mw-ui-icon-language-progressive mw-ui-icon-wikimedia-language-progressive"></span> <span class="vector-dropdown-label-text">8 languages</span> </label> <div class="vector-dropdown-content"> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li class="interlanguage-link interwiki-ar mw-list-item"><a href="https://ar.wikipedia.org/wiki/%D9%86%D8%AD%D9%88_%D8%A5%D8%AE%D8%B7%D8%A7%D8%B7%D8%A9_%D8%AC%D8%A7%D9%81%D8%A7" title="نحو إخطاطة جافا – Arabic" lang="ar" hreflang="ar" data-title="نحو إخطاطة جافا" data-language-autonym="العربية" data-language-local-name="Arabic" class="interlanguage-link-target"><span>العربية</span></a></li><li class="interlanguage-link interwiki-fa mw-list-item"><a href="https://fa.wikipedia.org/wiki/%D9%82%D9%88%D8%A7%D8%B9%D8%AF_%D8%AC%D8%A7%D9%88%D8%A7%D8%A7%D8%B3%DA%A9%D8%B1%DB%8C%D9%BE%D8%AA" title="قواعد جاوااسکریپت – Persian" lang="fa" hreflang="fa" data-title="قواعد جاوااسکریپت" data-language-autonym="فارسی" data-language-local-name="Persian" class="interlanguage-link-target"><span>فارسی</span></a></li><li class="interlanguage-link interwiki-fr mw-list-item"><a href="https://fr.wikipedia.org/wiki/Syntaxe_JavaScript" title="Syntaxe JavaScript – French" lang="fr" hreflang="fr" data-title="Syntaxe JavaScript" data-language-autonym="Français" data-language-local-name="French" class="interlanguage-link-target"><span>Français</span></a></li><li class="interlanguage-link interwiki-arz mw-list-item"><a href="https://arz.wikipedia.org/wiki/%D8%A8%D9%86%D8%A7%D8%A1_%D8%AC%D8%A7%D9%81%D8%A7_%D8%B3%D9%83%D8%B1%D9%8A%D8%A8%D8%AA" title="بناء جافا سكريبت – Egyptian Arabic" lang="arz" hreflang="arz" data-title="بناء جافا سكريبت" data-language-autonym="مصرى" data-language-local-name="Egyptian Arabic" class="interlanguage-link-target"><span>مصرى</span></a></li><li class="interlanguage-link interwiki-ms mw-list-item"><a href="https://ms.wikipedia.org/wiki/Sintaks_JavaScript" title="Sintaks JavaScript – Malay" lang="ms" hreflang="ms" data-title="Sintaks JavaScript" data-language-autonym="Bahasa Melayu" data-language-local-name="Malay" class="interlanguage-link-target"><span>Bahasa Melayu</span></a></li><li class="interlanguage-link interwiki-uk mw-list-item"><a href="https://uk.wikipedia.org/wiki/%D0%A1%D0%B8%D0%BD%D1%82%D0%B0%D0%BA%D1%81%D0%B8%D1%81_JavaScript" title="Синтаксис JavaScript – Ukrainian" lang="uk" hreflang="uk" data-title="Синтаксис JavaScript" data-language-autonym="Українська" data-language-local-name="Ukrainian" class="interlanguage-link-target"><span>Українська</span></a></li><li class="interlanguage-link interwiki-ur mw-list-item"><a href="https://ur.wikipedia.org/wiki/%D8%AC%D8%A7%D9%88%D8%A7_%D8%A7%D8%B3%DA%A9%D8%B1%D9%BE%D9%B9_%DA%A9%DB%92_%D9%82%D9%88%D8%A7%D8%B9%D8%AF" title="جاوا اسکرپٹ کے قواعد – Urdu" lang="ur" hreflang="ur" data-title="جاوا اسکرپٹ کے قواعد" data-language-autonym="اردو" data-language-local-name="Urdu" class="interlanguage-link-target"><span>اردو</span></a></li><li class="interlanguage-link interwiki-zh mw-list-item"><a href="https://zh.wikipedia.org/wiki/JavaScript%E8%AF%AD%E6%B3%95" title="JavaScript语法 – Chinese" lang="zh" hreflang="zh" data-title="JavaScript语法" data-language-autonym="中文" data-language-local-name="Chinese" class="interlanguage-link-target"><span>中文</span></a></li> </ul> <div class="after-portlet after-portlet-lang"><span class="wb-langlinks-edit wb-langlinks-link"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q1024422#sitelinks-wikipedia" title="Edit interlanguage links" class="wbc-editpage">Edit links</a></span></div> </div> </div> </div> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="Namespaces"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/JavaScript_syntax" title="View the content page [c]" accesskey="c"><span>Article</span></a></li><li id="ca-talk" class="vector-tab-noicon mw-list-item"><a href="/wiki/Talk:JavaScript_syntax" rel="discussion" title="Discuss improvements to the content page [t]" accesskey="t"><span>Talk</span></a></li> </ul> </div> </div> <div id="vector-variants-dropdown" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="vector-variants-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-variants-dropdown" class="vector-dropdown-checkbox " aria-label="Change language variant" > <label id="vector-variants-dropdown-label" for="vector-variants-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">English</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="Views"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/JavaScript_syntax"><span>Read</span></a></li><li id="ca-edit" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&action=history" title="Past revisions of this page [h]" accesskey="h"><span>View history</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="Tools" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">Tools</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">Tools</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">hide</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions emptyPortlet vector-has-collapsible-items" title="More options" > <div class="vector-menu-heading"> Actions </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/wiki/JavaScript_syntax"><span>Read</span></a></li><li id="ca-more-edit" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&action=history"><span>View history</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> General </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/wiki/Special:WhatLinksHere/JavaScript_syntax" title="List of all English Wikipedia pages containing links to this page [j]" accesskey="j"><span>What links here</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/wiki/Special:RecentChangesLinked/JavaScript_syntax" rel="nofollow" title="Recent changes in pages linked from this page [k]" accesskey="k"><span>Related changes</span></a></li><li id="t-upload" class="mw-list-item"><a href="/wiki/Wikipedia:File_Upload_Wizard" title="Upload files [u]" accesskey="u"><span>Upload file</span></a></li><li id="t-specialpages" class="mw-list-item"><a href="/wiki/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q"><span>Special pages</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&oldid=1258701791" title="Permanent link to this revision of this page"><span>Permanent link</span></a></li><li id="t-info" class="mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&action=info" title="More information about this page"><span>Page information</span></a></li><li id="t-cite" class="mw-list-item"><a href="/w/index.php?title=Special:CiteThisPage&page=JavaScript_syntax&id=1258701791&wpFormIdentifier=titleform" title="Information on how to cite this page"><span>Cite this page</span></a></li><li id="t-urlshortener" class="mw-list-item"><a href="/w/index.php?title=Special:UrlShortener&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FJavaScript_syntax"><span>Get shortened URL</span></a></li><li id="t-urlshortener-qrcode" class="mw-list-item"><a href="/w/index.php?title=Special:QrCode&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FJavaScript_syntax"><span>Download QR code</span></a></li> </ul> </div> </div> <div id="p-coll-print_export" class="vector-menu mw-portlet mw-portlet-coll-print_export" > <div class="vector-menu-heading"> Print/export </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="coll-download-as-rl" class="mw-list-item"><a href="/w/index.php?title=Special:DownloadAsPdf&page=JavaScript_syntax&action=show-download-screen" title="Download this page as a PDF file"><span>Download as PDF</span></a></li><li id="t-print" class="mw-list-item"><a href="/w/index.php?title=JavaScript_syntax&printable=yes" title="Printable version of this page [p]" accesskey="p"><span>Printable version</span></a></li> </ul> </div> </div> <div id="p-wikibase-otherprojects" class="vector-menu mw-portlet mw-portlet-wikibase-otherprojects" > <div class="vector-menu-heading"> In other projects </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li class="wb-otherproject-link wb-otherproject-wikibooks mw-list-item"><a href="https://en.wikibooks.org/wiki/JavaScript/Lexical_structure" hreflang="en"><span>Wikibooks</span></a></li><li id="t-wikibase" class="wb-otherproject-link wb-otherproject-wikibase-dataitem mw-list-item"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q1024422" title="Structured data on this page hosted by Wikidata [g]" accesskey="g"><span>Wikidata item</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <div class="vector-sticky-pinned-container"> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-pinned-container" class="vector-pinned-container"> <div id="vector-appearance" class="vector-appearance vector-pinnable-element"> <div class="vector-pinnable-header vector-appearance-pinnable-header vector-pinnable-header-pinned" data-feature-name="appearance-pinned" data-pinnable-element-id="vector-appearance" data-pinned-container-id="vector-appearance-pinned-container" data-unpinned-container-id="vector-appearance-unpinned-container" > <div class="vector-pinnable-header-label">Appearance</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-appearance.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-appearance.unpin">hide</button> </div> </div> </div> </nav> </div> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> </div> <div id="siteSub" class="noprint">From Wikipedia, the free encyclopedia</div> </div> <div id="contentSub"><div id="mw-content-subtitle"></div></div> <div id="mw-content-text" class="mw-body-content"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">Set of rules defining correctly structured programs</div> <style data-mw-deduplicate="TemplateStyles:r1251242444">.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}</style><table class="box-Update plainlinks metadata ambox ambox-content ambox-Update" role="presentation"><tbody><tr><td class="mbox-image"><div class="mbox-image-div"><span typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Ambox_current_red_Asia_Australia.svg/42px-Ambox_current_red_Asia_Australia.svg.png" decoding="async" width="42" height="34" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Ambox_current_red_Asia_Australia.svg/63px-Ambox_current_red_Asia_Australia.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Ambox_current_red_Asia_Australia.svg/84px-Ambox_current_red_Asia_Australia.svg.png 2x" data-file-width="360" data-file-height="290" /></span></span></div></td><td class="mbox-text"><div class="mbox-text-span">This article needs to be <b>updated</b>. The reason given is: New features/versions now in JavaScript.<span class="hide-when-compact"> Please help update this article to reflect recent events or newly available information.</span> <span class="date-container"><i>(<span class="date">November 2020</span>)</i></span></div></td></tr></tbody></table> <p class="mw-empty-elt"> </p> <figure class="mw-default-size" typeof="mw:File/Thumb"><a href="/wiki/File:Source_code_in_Javascript.png" class="mw-file-description"><img src="//upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Source_code_in_Javascript.png/220px-Source_code_in_Javascript.png" decoding="async" width="220" height="138" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Source_code_in_Javascript.png/330px-Source_code_in_Javascript.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Source_code_in_Javascript.png/440px-Source_code_in_Javascript.png 2x" data-file-width="804" data-file-height="503" /></a><figcaption>A snippet of <a href="/wiki/JavaScript" title="JavaScript">JavaScript</a> code with keywords highlighted in different colors</figcaption></figure> <p>The <b><a href="/wiki/Syntax_(programming_languages)" title="Syntax (programming languages)">syntax</a> of <a href="/wiki/JavaScript" title="JavaScript">JavaScript</a></b> is the set of rules that define a correctly structured JavaScript program. </p><p>The examples below make use of the log function of the console object present in most browsers for <a href="/wiki/Standard_streams#Standard_output_.28stdout.29" title="Standard streams">standard text output</a>. </p><p>The JavaScript <a href="/wiki/Standard_library" title="Standard library">standard library</a> lacks an official standard text output function (with the exception of <code>document.write</code>). Given that JavaScript is mainly used for <a href="/wiki/Client-side_scripting" class="mw-redirect" title="Client-side scripting">client-side scripting</a> within modern <a href="/wiki/Web_browser" title="Web browser">web browsers</a>, and that almost all Web browsers provide the alert function, <code>alert</code> can also be used, but is not commonly used. </p> <meta property="mw:PageProp/toc" /> <div class="mw-heading mw-heading2"><h2 id="Origins">Origins</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=1" title="Edit section: Origins"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/Brendan_Eich" title="Brendan Eich">Brendan Eich</a> summarized the ancestry of the syntax in the first paragraph of the JavaScript 1.1 specification<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> as follows: </p> <style data-mw-deduplicate="TemplateStyles:r1244412712">.mw-parser-output .templatequote{overflow:hidden;margin:1em 0;padding:0 32px}.mw-parser-output .templatequotecite{line-height:1.5em;text-align:left;margin-top:0}@media(min-width:500px){.mw-parser-output .templatequotecite{padding-left:1.6em}}</style><blockquote class="templatequote"><p>JavaScript borrows most of its syntax from <a href="/wiki/Java_(programming_language)" title="Java (programming language)">Java</a>, but also inherits from <a href="/wiki/Awk" class="mw-redirect" title="Awk">Awk</a> and <a href="/wiki/Perl" title="Perl">Perl</a>, with some indirect influence from <a href="/wiki/Self_(programming_language)" title="Self (programming language)">Self</a> in its object prototype system.</p></blockquote> <div class="mw-heading mw-heading2"><h2 id="Basics">Basics</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=2" title="Edit section: Basics"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-heading mw-heading3"><h3 id="Case_sensitivity">Case sensitivity</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=3" title="Edit section: Case sensitivity"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript is <a href="/wiki/Case_sensitivity" title="Case sensitivity">case sensitive</a>. It is common to start the name of a <a href="#Constructors">constructor</a> with a <a href="/wiki/CamelCase" class="mw-redirect" title="CamelCase">capitalised</a> letter, and the name of a function or variable with a lower-case letter. </p><p>Example: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">5</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span><span class="w"> </span><span class="c1">// 5</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">A</span><span class="p">);</span><span class="w"> </span><span class="c1">// throws a ReferenceError: A is not defined</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Whitespace_and_semicolons">Whitespace and semicolons</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=4" title="Edit section: Whitespace and semicolons"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Unlike in <a href="/wiki/C_(programming_language)" title="C (programming language)">C</a>, whitespace in JavaScript source can directly impact <a href="/wiki/Semantics_(computer_science)" title="Semantics (computer science)">semantics</a>. <a href="/wiki/Semicolon" title="Semicolon">Semicolons</a> end statements in JavaScript. Because of <a href="/wiki/Lexical_analysis#Semicolon_insertion" title="Lexical analysis">automatic semicolon insertion</a> (ASI), some statements that are well formed when a newline is parsed will be considered complete, as if a semicolon were inserted just prior to the newline. Some authorities advise supplying statement-terminating semicolons explicitly, because it may lessen unintended effects of the automatic semicolon insertion.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> </p><p>There are two issues: five tokens can either begin a statement or be the extension of a complete statement; and five restricted productions, where line breaks are not allowed in certain positions, potentially yielding incorrect parsing. </p><p>The five problematic tokens are the open parenthesis "<code>(</code>", open bracket "<code>[</code>", slash "<code>/</code>", plus "<code>+</code>", and minus "<code>-</code>". Of these, the open parenthesis is common in the <a href="/wiki/Immediately_invoked_function_expression" title="Immediately invoked function expression">immediately invoked function expression</a> pattern, and open bracket occurs sometimes, while others are quite rare. An example: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">c</span> <span class="p">(</span><span class="nx">d</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">e</span><span class="p">).</span><span class="nx">foo</span><span class="p">()</span> <span class="c1">// Treated as:</span> <span class="c1">// a = b + c(d + e).foo();</span> </pre></div> <p>with the suggestion that the preceding statement be terminated with a semicolon. </p><p>Some suggest instead the use of <i>leading</i> semicolons on lines starting with '<code>(</code>' or '<code>[</code>', so the line is not accidentally joined with the previous one. This is known as a <b>defensive semicolon</b>, and is particularly recommended, because code may otherwise become ambiguous when it is rearranged. For example: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">c</span> <span class="p">;(</span><span class="nx">d</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">e</span><span class="p">).</span><span class="nx">foo</span><span class="p">()</span> <span class="c1">// Treated as:</span> <span class="c1">// a = b + c;</span> <span class="c1">// (d + e).foo();</span> </pre></div> <p>Initial semicolons are also sometimes used at the start of JavaScript libraries, in case they are appended to another library that omits a trailing semicolon, as this can result in ambiguity of the initial statement. </p><p>The five restricted productions are <code>return</code>, <code>throw</code>, <code>break</code>, <code>continue</code>, and post-increment/decrement. In all cases, inserting semicolons does not fix the problem, but makes the parsed syntax clear, making the error easier to detect. <code>return</code> and <code>throw</code> take an optional value, while <code>break</code> and <code>continue</code> take an optional label. In all cases, the advice is to keep the value or label on the same line as the statement. This most often shows up in the return statement, where one might return a large object literal, which might be accidentally placed starting on a new line. For post-increment/decrement, there is potential ambiguity with pre-increment/decrement, and again it is recommended to simply keep these on the same line. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">return</span> <span class="nx">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">b</span><span class="p">;</span> <span class="c1">// Returns undefined. Treated as:</span> <span class="c1">// return;</span> <span class="c1">// a + b;</span> <span class="c1">// Should be written as:</span> <span class="c1">// return a + b;</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Comments">Comments</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=5" title="Edit section: Comments"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1236090951">.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Comment_(computer_programming)" title="Comment (computer programming)">Comment (computer programming)</a></div> <p><a href="/wiki/Comment_(computer_programming)" title="Comment (computer programming)">Comment</a> syntax is the same as in <a href="/wiki/C%2B%2B" title="C++">C++</a>, <a href="/wiki/Swift_(programming_language)" title="Swift (programming language)">Swift</a> and many other languages. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// a short, one-line comment</span> <span class="cm">/* this is a long, multi-line comment</span> <span class="cm"> about my script. May it one day</span> <span class="cm"> be great. */</span> <span class="cm">/* Comments /* may not be nested */</span><span class="w"> </span><span class="nx">Syntax</span><span class="w"> </span><span class="nx">error</span><span class="w"> </span><span class="o">*</span><span class="err">/</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Variables">Variables</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=6" title="Edit section: Variables"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Variable_(programming)" class="mw-redirect" title="Variable (programming)">Variable (programming)</a></div> <p><a href="/wiki/Variable_(programming)" class="mw-redirect" title="Variable (programming)">Variables</a> in standard JavaScript have no <a href="/wiki/Type_system" title="Type system">type</a> attached, so any value (each <i>value</i> has a type) can be stored in any variable. Starting with <a href="/wiki/ECMAScript#6th_Edition_–_ECMAScript_2015" title="ECMAScript">ES6</a>, the 6th version of the language, variables could be declared with <code>var</code> for function scoped variables, and <code>let</code> or <code>const</code> which are for <a href="/wiki/Block_scope" class="mw-redirect" title="Block scope">block level</a> variables. Before ES6, variables could only be declared with a <code>var</code> statement. Values assigned to variables declared with <code>const</code> cannot be changed, but their properties can. <code>var</code> should no longer be used since <code>let</code> and <code>const</code> are supported by modern browsers.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> A variable's <a href="/wiki/Identifier_(computer_languages)" title="Identifier (computer languages)">identifier</a> must start with a letter, underscore (<code>_</code>), or dollar sign (<code>$</code>), while subsequent characters can also be digits (<code>0-9</code>). JavaScript is case sensitive, so the uppercase characters "A" through "Z" are different from the lowercase characters "a" through "z". </p><p>Starting with JavaScript 1.5, <a href="/wiki/ISO_8859-1" class="mw-redirect" title="ISO 8859-1">ISO 8859-1</a> or <a href="/wiki/Unicode" title="Unicode">Unicode</a> letters (or <code>\uXXXX</code> Unicode escape sequences) can be used in identifiers.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> In certain JavaScript implementations, the at sign (@) can be used in an identifier, but this is contrary to the specifications and not supported in newer implementations. <sup class="noprint Inline-Template Template-Fact" style="white-space:nowrap;">[<i><a href="/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed"><span title="This claim needs references to reliable sources. (January 2021)">citation needed</span></a></i>]</sup> </p> <div class="mw-heading mw-heading3"><h3 id="Scoping_and_hoisting">Scoping and hoisting</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=7" title="Edit section: Scoping and hoisting"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Variables declared with <code>var</code> are <a href="/wiki/Lexical_scoping" class="mw-redirect" title="Lexical scoping">lexically scoped</a> at a <a href="/wiki/Function_scope" class="mw-redirect" title="Function scope">function level</a>, while ones with <code>let</code> or <code>const</code> have a <a href="/wiki/Block_scope" class="mw-redirect" title="Block scope">block level</a> scope. Since declarations are processed before any code is executed, a variable can be assigned to and used prior to being declared in the code.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> This is referred to as <i><style data-mw-deduplicate="TemplateStyles:r1238216509">.mw-parser-output .vanchor>:target~.vanchor-text{background-color:#b1d2ff}@media screen{html.skin-theme-clientpref-night .mw-parser-output .vanchor>:target~.vanchor-text{background-color:#0f4dc9}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .vanchor>:target~.vanchor-text{background-color:#0f4dc9}}</style><span class="vanchor"><span id="hoisting"></span><span class="vanchor-text">hoisting</span></span></i>, and it is equivalent to variables being <a href="/wiki/Forward_declaration" title="Forward declaration">forward declared</a> at the top of the function or block.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> </p><p>With <code>var</code>, <code>let</code>, and <code>const</code> statements, only the declaration is hoisted; assignments are not hoisted. Thus a <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="kd">var</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span></code> statement in the middle of the function is equivalent to a <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="kd">var</span><span class="w"> </span><span class="nx">x</span></code> declaration statement at the top of the function, and an <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span></code> assignment statement at that point in the middle of the function. This means that values cannot be accessed before they are declared; <a href="/wiki/Forward_reference" class="mw-redirect" title="Forward reference">forward reference</a> is not possible. With <code>var</code> a variable's value is <code>undefined</code> until it is initialized. Variables declared with <code>let</code> or <code>const</code> cannot be accessed until they have been initialized, so referencing such variables before will cause an error. </p><p>Function declarations, which declare a variable and assign a function to it, are similar to variable statements, but in addition to hoisting the declaration, they also hoist the assignment – as if the entire statement appeared at the top of the containing function – and thus forward reference is also possible: the location of a function statement within an enclosing function is irrelevant. This is different from a function expression being assigned to a variable in a <code>var</code>, <code>let</code>, or <code>const</code> statement. </p><p>So, for example, </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="nx">func</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// declaration is hoisted only</span> <span class="kd">function</span><span class="w"> </span><span class="nx">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// declaration and assignment are hoisted</span> </pre></div> <p>Block scoping can be produced by wrapping the entire block in a function and then executing it – this is known as the <a href="/wiki/Immediately-invoked_function_expression" class="mw-redirect" title="Immediately-invoked function expression">immediately-invoked function expression</a> pattern – or by declaring the variable using the <code>let</code> keyword. </p> <div class="mw-heading mw-heading3"><h3 id="Declaration_and_assignment">Declaration and assignment</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=8" title="Edit section: Declaration and assignment"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Variables declared outside a scope are <a href="/wiki/Global_variable" title="Global variable">global</a>. If a variable is declared in a higher scope, it can be accessed by child scopes. </p><p>When JavaScript tries to <b>resolve</b> an identifier, it looks in the local scope. If this identifier is not found, it looks in the next outer scope, and so on along the <i>scope chain</i> until it reaches the <i>global scope</i> where global variables reside. If it is still not found, JavaScript will raise a <code>ReferenceError</code> exception. </p><p>When <b>assigning</b> an identifier, JavaScript goes through exactly the same process to retrieve this identifier, except that if it is not found in the <i>global scope</i>, it will create the "variable" in the scope where it was created.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> As a consequence, a variable never declared will be global, if assigned. Declaring a variable (with the keyword <code>var</code>) in the <i>global scope</i> (i.e. outside of any function body (or block in the case of let/const)), assigning a never declared identifier or adding a property to the <i>global object</i> (usually <i>window</i>) will also create a new global variable. </p><p>Note that JavaScript's <i>strict mode</i> forbids the assignment of an undeclared variable, which avoids global namespace pollution. </p> <div class="mw-heading mw-heading3"><h3 id="Examples">Examples</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=9" title="Edit section: Examples"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Here are some examples of variable declarations and scope: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="nx">x1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// A global variable, because it is not in any function</span> <span class="kd">let</span><span class="w"> </span><span class="nx">x2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// Also global, this time because it is not in any block</span> <span class="kd">function</span><span class="w"> </span><span class="nx">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foxes'</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'birds'</span><span class="p">;</span><span class="w"> </span><span class="c1">// 2 local variables</span> <span class="w"> </span><span class="nx">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'fish'</span><span class="p">;</span><span class="w"> </span><span class="c1">// global, because it wasn't declared anywhere before</span> <span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">child</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'monkeys'</span><span class="p">;</span><span class="w"> </span><span class="c1">// This variable is local and does not affect the "birds" r of the parent function.</span> <span class="w"> </span><span class="nx">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'penguins'</span><span class="p">;</span><span class="w"> </span><span class="c1">// Closure: Child function is able to access the variables of the parent function.</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="nx">twenty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">20</span><span class="p">;</span><span class="w"> </span><span class="c1">// This variable is declared on the next line, but usable anywhere in the function, even before, as here</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">twenty</span><span class="p">;</span> <span class="w"> </span><span class="nx">child</span><span class="p">();</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">x2</span><span class="p">;</span><span class="w"> </span><span class="c1">// We can use x1 and x2 here, because they are global</span> <span class="p">}</span> <span class="nx">f</span><span class="p">();</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">z</span><span class="p">);</span><span class="w"> </span><span class="c1">// This line will raise a ReferenceError exception, because the value of z is no longer available</span> </pre></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mf">10</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span><span class="w"> </span><span class="c1">// throws a ReferenceError: i is not defined</span> </pre></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mf">10</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span><span class="w"> </span><span class="c1">// throws a TypeError: Assignment to constant variable</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="mf">2</span><span class="p">,</span><span class="mf">3</span><span class="p">])</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span><span class="w"> </span><span class="c1">//will not raise an exception. i is not reassigned but recreated in every iteration</span> <span class="kd">const</span><span class="w"> </span><span class="nx">pi</span><span class="p">;</span><span class="w"> </span><span class="c1">// throws a SyntaxError: Missing initializer in const declaration</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Primitive_data_types">Primitive data types</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=10" title="Edit section: Primitive data types"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Primitive_data_type" title="Primitive data type">Primitive data type</a></div> <p>The JavaScript language provides six <a href="/wiki/Primitive_data_type" title="Primitive data type">primitive data types</a>: </p> <ul><li>Undefined</li> <li>Number</li> <li>BigInt</li> <li>String</li> <li>Boolean</li> <li>Symbol</li></ul> <p>Some of the primitive data types also provide a set of named values that represent the extents of the type boundaries. These named values are described within the appropriate sections below. </p> <div class="mw-heading mw-heading3"><h3 id="Undefined">Undefined</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=11" title="Edit section: Undefined"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Undefined_value" title="Undefined value">Undefined value</a></div> <p>The <a href="/wiki/Undefined_value" title="Undefined value">value of "undefined"</a> is assigned to all <a href="/wiki/Uninitialized_variable" title="Uninitialized variable">uninitialized variables</a>, and is also returned when checking for object properties that do not exist. In a Boolean context, the undefined value is considered a false value. </p><p>Note: undefined is considered a genuine primitive type. Unless explicitly converted, the undefined value may behave unexpectedly in comparison to other types that evaluate to false in a logical context. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">test</span><span class="p">;</span><span class="w"> </span><span class="c1">// variable declared, but not defined, ...</span> <span class="w"> </span><span class="c1">// ... set to value of undefined</span> <span class="kd">const</span><span class="w"> </span><span class="nx">testObj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">test</span><span class="p">);</span><span class="w"> </span><span class="c1">// test variable exists, but value not ...</span> <span class="w"> </span><span class="c1">// ... defined, displays undefined</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">testObj</span><span class="p">.</span><span class="nx">myProp</span><span class="p">);</span><span class="w"> </span><span class="c1">// testObj exists, property does not, ...</span> <span class="w"> </span><span class="c1">// ... displays undefined</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">undefined</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="p">);</span><span class="w"> </span><span class="c1">// unenforced type during check, displays true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">undefined</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">null</span><span class="p">);</span><span class="w"> </span><span class="c1">// enforce type during check, displays false</span> </pre></div> <p>Note: There is no built-in language literal for undefined. Thus <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">undefined</span><span class="p">)</span></code> is not a foolproof way to check whether a variable is undefined, because in versions before ECMAScript 5, it is legal for someone to write <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="kd">var</span><span class="w"> </span><span class="kc">undefined</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"I'm defined now"</span><span class="p">;</span></code>. A more robust approach is to compare using <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="p">(</span><span class="ow">typeof</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s1">'undefined'</span><span class="p">)</span></code>. </p><p>Functions like this won't work as expected: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">u</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nx">u</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// like this...</span> <span class="kd">function</span><span class="w"> </span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// ... or that second one</span> <span class="kd">function</span><span class="w"> </span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="ow">typeof</span><span class="w"> </span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s2">"undefined"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// ... or that third one</span> </pre></div> <p>Here, calling <code>isUndefined(my_var)</code> raises a <style data-mw-deduplicate="TemplateStyles:r886049734">.mw-parser-output .monospaced{font-family:monospace,monospace}</style><span class="monospaced">ReferenceError</span> if <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">my_var</span> is an unknown identifier, whereas <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="ow">typeof</span><span class="w"> </span><span class="nx">my_var</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s1">'undefined'</span></code> doesn't. </p> <div class="mw-heading mw-heading3"><h3 id="Number">Number</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=12" title="Edit section: Number"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Numbers are represented in binary as <a href="/wiki/IEEE_754" title="IEEE 754">IEEE 754</a> <a href="/wiki/Floating_point" class="mw-redirect" title="Floating point">floating point</a> doubles. Although this format provides an accuracy of nearly 16 <a href="/wiki/Significant_digits" class="mw-redirect" title="Significant digits">significant digits</a>, it cannot always exactly represent real numbers, including fractions. </p><p>This becomes an issue when comparing or formatting numbers. For example: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">0.2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">0.1</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0.3</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">0.94</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">0.01</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 0.9299999999999999</span> </pre></div> <p>As a result, a routine such as the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">toFixed()</span> method should be used to round numbers whenever they are <a rel="nofollow" class="external text" href="http://www.jibbering.com/faq/#formatNumber">formatted for output</a>. </p><p>Numbers may be specified in any of these notations: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="mf">345</span><span class="p">;</span><span class="w"> </span><span class="c1">// an "integer", although there is only one numeric type in JavaScript</span> <span class="mf">34.5</span><span class="p">;</span><span class="w"> </span><span class="c1">// a floating-point number</span> <span class="mf">3.45e2</span><span class="p">;</span><span class="w"> </span><span class="c1">// another floating-point, equivalent to 345</span> <span class="mb">0b1011</span><span class="p">;</span><span class="w"> </span><span class="c1">// a binary integer equal to 11</span> <span class="mo">0o377</span><span class="p">;</span><span class="w"> </span><span class="c1">// an octal integer equal to 255</span> <span class="mh">0xFF</span><span class="p">;</span><span class="w"> </span><span class="c1">// a hexadecimal integer equal to 255, digits represented by the ...</span> <span class="w"> </span><span class="c1">// ... letters A-F may be upper or lowercase</span> </pre></div> <p>There's also a numeric separator, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">_</span> (the underscore), introduced in ES2021: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Note: Wikipedia syntax doesn't support numeric separators yet</span> <span class="mf">1</span><span class="nx">_000_000_000</span><span class="p">;</span><span class="w"> </span><span class="c1">// Used with big numbers</span> <span class="mf">1</span><span class="nx">_000_000</span><span class="mf">.5</span><span class="p">;</span><span class="w"> </span><span class="c1">// Support with decimals</span> <span class="mf">1</span><span class="nx">_000e1_000</span><span class="p">;</span><span class="w"> </span><span class="c1">// Support with exponents</span> <span class="c1">// Support with binary, octals and hex</span> <span class="mb">0b0000</span><span class="nx">_0000_0101_1011</span><span class="p">;</span> <span class="mo">0o0001</span><span class="nx">_3520_0237_1327</span><span class="p">;</span> <span class="mh">0xFFFF</span><span class="nx">_FFFF_FFFF_FFFE</span><span class="p">;</span> <span class="c1">// But you can't use them next to a non-digit number part, or at the start or end</span> <span class="nx">_12</span><span class="p">;</span><span class="w"> </span><span class="c1">// Variable is not defined (the underscore makes it a variable identifier)</span> <span class="mf">12</span><span class="nx">_</span><span class="p">;</span><span class="w"> </span><span class="c1">// Syntax error (cannot be at the end of numbers)</span> <span class="mf">12</span><span class="nx">_</span><span class="mf">.0</span><span class="p">;</span><span class="w"> </span><span class="c1">// Syntax error (doesn't make sense to put a separator next to the decimal point)</span> <span class="mf">12.</span><span class="nx">_0</span><span class="p">;</span><span class="w"> </span><span class="c1">// Syntax error</span> <span class="mf">12</span><span class="nx">e_6</span><span class="p">;</span><span class="w"> </span><span class="c1">// Syntax error (next to "e", a non-digit. Doesn't make sense to put a separator at the start)</span> <span class="mf">1000</span><span class="nx">____0000</span><span class="p">;</span><span class="w"> </span><span class="c1">// Syntax error (next to "_", a non-digit. Only 1 separator at a time is allowed</span> </pre></div> <p>The extents <a href="/wiki/Extended_real_number_line" title="Extended real number line"><b>+∞</b>, <b>−∞</b></a> and <b><a href="/wiki/NaN" title="NaN">NaN</a></b> (Not a Number) of the number type may be obtained by two program expressions: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kc">Infinity</span><span class="p">;</span><span class="w"> </span><span class="c1">// positive infinity (negative obtained with -Infinity for instance)</span> <span class="kc">NaN</span><span class="p">;</span><span class="w"> </span><span class="c1">// The Not-A-Number value, also returned as a failure in ...</span> <span class="w"> </span><span class="c1">// ... string-to-number conversions</span> </pre></div> <p>Infinity and NaN are numbers: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="ow">typeof</span><span class="w"> </span><span class="kc">Infinity</span><span class="p">;</span><span class="w"> </span><span class="c1">// returns "number"</span> <span class="ow">typeof</span><span class="w"> </span><span class="kc">NaN</span><span class="p">;</span><span class="w"> </span><span class="c1">// returns "number"</span> </pre></div> <p>These three special values correspond and behave as the <a href="/wiki/IEEE-754" class="mw-redirect" title="IEEE-754">IEEE-754</a> describes them. </p><p>The Number constructor (used as a function), or a unary + or -, may be used to perform explicit numeric conversion: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">myString</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"123.456"</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">myNumber1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Number</span><span class="p">(</span><span class="nx">myString</span><span class="p">);</span> <span class="kd">const</span><span class="w"> </span><span class="nx">myNumber2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">+</span><span class="nx">myString</span><span class="p">;</span> </pre></div> <p>When used as a constructor, a numeric <i>wrapper</i> object is created (though it is of little use): </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">myNumericWrapper</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Number</span><span class="p">(</span><span class="mf">123.456</span><span class="p">);</span> </pre></div> <p>However, NaN is not equal to itself: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">nan</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NaN</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">NaN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">NaN</span><span class="p">);</span><span class="w"> </span><span class="c1">// false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">NaN</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">NaN</span><span class="p">);</span><span class="w"> </span><span class="c1">// false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">NaN</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="kc">NaN</span><span class="p">);</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">nan</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="nx">nan</span><span class="p">);</span><span class="w"> </span><span class="c1">// true</span> <span class="c1">// You can use the isNaN methods to check for NaN</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">isNaN</span><span class="p">(</span><span class="s2">"converted to NaN"</span><span class="p">));</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">isNaN</span><span class="p">(</span><span class="kc">NaN</span><span class="p">));</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Number</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="s2">"not converted"</span><span class="p">));</span><span class="w"> </span><span class="c1">// false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Number</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="kc">NaN</span><span class="p">));</span><span class="w"> </span><span class="c1">// true</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="BigInt">BigInt</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=13" title="Edit section: BigInt"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>BigInts can be used for arbitrarily large <a href="/wiki/Integer" title="Integer">integers</a>. Especially whole numbers larger than 2<sup>53</sup> - 1, which is the largest number JavaScript can reliably represent with the Number primitive and represented by the Number.MAX_SAFE_INTEGER constant. </p><p>When dividing BigInts, the results are <a href="/wiki/Truncation" title="Truncation">truncated</a>. </p> <div class="mw-heading mw-heading3"><h3 id="String">String</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=14" title="Edit section: String"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>A <a href="/wiki/String_(computer_science)" title="String (computer science)">string</a> in JavaScript is a sequence of characters. In JavaScript, strings can be created directly (as literals) by placing the series of characters between double (") or single (') quotes. Such strings must be written on a single line, but may include escaped newline characters (such as \n). The JavaScript standard allows the <a href="/wiki/Grave_accent#Use_in_programming" title="Grave accent">backquote</a> character (`, a.k.a. grave accent or backtick) to quote multiline literal strings, as well as template literals, which allow for interpolation of type-coerced evaluated expressions within a string.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">greeting</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Hello, World!"</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">anotherGreeting</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Greetings, people of Earth.'</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">aMultilineGreeting</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sb">`Warm regards,</span> <span class="sb">John Doe.`</span> <span class="c1">// Template literals type-coerce evaluated expressions and interpolate them into the string.</span> <span class="kd">const</span><span class="w"> </span><span class="nx">templateLiteral</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sb">`This is what is stored in anotherGreeting: </span><span class="si">${</span><span class="nx">anotherGreeting</span><span class="si">}</span><span class="sb">.`</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">templateLiteral</span><span class="p">);</span><span class="w"> </span><span class="c1">// 'This is what is stored in anotherGreeting: 'Greetings, people of Earth.''</span> </pre></div> <p>Individual characters within a string can be accessed using the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">charAt</span> method (provided by <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">String.prototype</span>). This is the preferred way when accessing individual characters within a string, because it also works in non-modern browsers: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">greeting</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span> </pre></div> <p>In modern browsers, individual characters within a string can be accessed (as strings with only a single character) through the same notation as arrays: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">greeting</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span> </pre></div> <p>However, JavaScript strings are <a href="/wiki/Immutable_object" title="Immutable object">immutable</a>: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">greeting</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"H"</span><span class="p">;</span><span class="w"> </span><span class="c1">// Fails.</span> </pre></div> <p>Applying the equality operator ("==") to two strings returns true, if the strings have the same contents, which means: of the same length and containing the same sequence of characters (case is significant for alphabets). Thus: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"World"</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">compare1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"Hello, "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Hello, World"</span><span class="p">);</span><span class="w"> </span><span class="c1">// Here compare1 contains true.</span> <span class="kd">const</span><span class="w"> </span><span class="nx">compare2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"Hello, "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"hello, World"</span><span class="p">);</span><span class="w"> </span><span class="c1">// Here compare2 contains ...</span> <span class="w"> </span><span class="c1">// ... false since the ...</span> <span class="w"> </span><span class="c1">// ... first characters ...</span> <span class="w"> </span><span class="c1">// ... of both operands ...</span> <span class="w"> </span><span class="c1">// ... are not of the same case.</span> </pre></div> <p>Quotes of the same type cannot be nested unless they are <a href="/wiki/String_literal#Escape_character" title="String literal">escaped</a>. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'"Hello, World!" he said.'</span><span class="p">;</span><span class="w"> </span><span class="c1">// Just fine.</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="nx">Hello</span><span class="p">,</span><span class="w"> </span><span class="nx">World</span><span class="o">!</span><span class="s2">" he said."</span><span class="p">;</span><span class="w"> </span><span class="c1">// Not good.</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\"Hello, World!\" he said."</span><span class="p">;</span><span class="w"> </span><span class="c1">// Works by escaping " with \"</span> </pre></div> <p>The <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">String</span> constructor creates a string object (an object wrapping a string): </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">greeting</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">String</span><span class="p">(</span><span class="s2">"Hello, World!"</span><span class="p">);</span> </pre></div> <p>These objects have a <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">valueOf</span> method returning the primitive string wrapped within them: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">String</span><span class="p">(</span><span class="s2">"Hello !"</span><span class="p">);</span> <span class="ow">typeof</span><span class="w"> </span><span class="nx">s</span><span class="p">;</span><span class="w"> </span><span class="c1">// Is 'object'.</span> <span class="ow">typeof</span><span class="w"> </span><span class="nx">s</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">();</span><span class="w"> </span><span class="c1">// Is 'string'.</span> </pre></div> <p>Equality between two <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">String</span> objects does not behave as with string primitives: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">s1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">String</span><span class="p">(</span><span class="s2">"Hello !"</span><span class="p">);</span> <span class="kd">const</span><span class="w"> </span><span class="nx">s2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">String</span><span class="p">(</span><span class="s2">"Hello !"</span><span class="p">);</span> <span class="nx">s1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">s2</span><span class="p">;</span><span class="w"> </span><span class="c1">// Is false, because they are two distinct objects.</span> <span class="nx">s1</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">s2</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">();</span><span class="w"> </span><span class="c1">// Is true.</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Boolean">Boolean</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=15" title="Edit section: Boolean"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/JavaScript" title="JavaScript">JavaScript</a> provides a <a href="/wiki/Boolean_data_type" title="Boolean data type">Boolean data type</a> with <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">true</span> and <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">false</span> literals. The <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced"><a href="/wiki/Typeof" title="Typeof">typeof</a></span> operator returns the string <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">"boolean"</span> for these <a href="/wiki/Primitive_types" class="mw-redirect" title="Primitive types">primitive types</a>. When used in a logical context, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">0</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">-0</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">null</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">NaN</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">undefined</span>, and the empty string (<link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">""</span>) evaluate as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">false</span> due to automatic <a href="/wiki/Type_conversion" title="Type conversion">type conversion</a>. All other values (the <a href="/wiki/Complement_(set_theory)" title="Complement (set theory)">complement</a> of the previous list) evaluate as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">true</span>, including the strings <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">"0"</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">"false"</span> and any object. </p> <div class="mw-heading mw-heading3"><h3 id="Type_conversion">Type conversion</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=16" title="Edit section: Type conversion"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Automatic type coercion by the equality comparison operators (<code>==</code> and <code>!=</code>) can be avoided by using the type checked comparison operators (<code>===</code> and <code>!==</code>). </p><p>When type conversion is required, JavaScript converts <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Boolean</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Number</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">String</span>, or <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Object</span> operands as follows:<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> </p> <dl><dt><span style="font-size:85%;">Number and String</span></dt> <dd>The string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.</dd> <dt><span style="font-size:85%;">Boolean</span></dt> <dd>If one of the operands is a Boolean, the Boolean operand is converted to 1 if it is <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">true</span>, or to 0 if it is <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">false</span>.</dd> <dt><span style="font-size:85%;">Object</span></dt> <dd>If an object is compared with a number or string, JavaScript attempts to return the default value for the object. An object is converted to a primitive String or Number value, using the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.valueOf()</span> or <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.toString()</span> methods of the object. If this fails, a runtime error is generated.</dd></dl> <div class="mw-heading mw-heading4"><h4 id="Boolean_type_conversion">Boolean type conversion</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=17" title="Edit section: Boolean type conversion"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><span class="anchor" id="truthy_and_falsy"></span> </p> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Further information: <a href="/wiki/Truthy_(computing)" class="mw-redirect" title="Truthy (computing)">Truthy (computing)</a></div> <p><a href="/wiki/Douglas_Crockford" title="Douglas Crockford">Douglas Crockford</a> advocates the terms "truthy" and "falsy" to describe how values of various types behave when evaluated in a logical context, especially in regard to edge cases.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> The binary logical operators returned a Boolean value in early versions of JavaScript, but now they return one of the operands instead. The left–operand is returned, if it can be evaluated as : <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">false</span>, in the case of <a href="/wiki/Logical_conjunction" title="Logical conjunction">conjunction</a>: (<code>a && b</code>), or <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">true</span>, in the case of <a href="/wiki/Logical_disjunction" title="Logical disjunction">disjunction</a>: (<code>a || b</code>); otherwise the right–operand is returned. Automatic type coercion by the comparison operators may differ for cases of mixed Boolean and number-compatible operands (including strings that can be evaluated as a number, or objects that can be evaluated as such a string), because the Boolean operand will be compared as a numeric value. This may be unexpected. An expression can be explicitly cast to a Boolean primitive by doubling the logical <a href="/wiki/Negation" title="Negation">negation operator</a>: (<link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">!!</span>), using the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Boolean()</span> function, or using the <a href="/wiki/Conditional_(programming)" class="mw-redirect" title="Conditional (programming)">conditional operator</a>: (<code>c ? t : f</code>). </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Automatic type coercion</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="p">);</span><span class="w"> </span><span class="c1">// false... true → 1 !== 2 ← 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="p">);</span><span class="w"> </span><span class="c1">// false... false → 0 !== 2 ← 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">1</span><span class="w"> </span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... true → 1 === 1 ← 1</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... false → 0 === 0 ← 0</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"2"</span><span class="p">);</span><span class="w"> </span><span class="c1">// false... true → 1 !== 2 ← "2"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"2"</span><span class="p">);</span><span class="w"> </span><span class="c1">// false... false → 0 !== 2 ← "2"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"1"</span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... true → 1 === 1 ← "1"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"0"</span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... false → 0 === 0 ← "0"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">""</span><span class="w"> </span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... false → 0 === 0 ← ""</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">false</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">NaN</span><span class="p">);</span><span class="w"> </span><span class="c1">// false... false → 0 !== NaN</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">NaN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">NaN</span><span class="p">);</span><span class="w"> </span><span class="c1">// false...... NaN is not equivalent to anything, including NaN.</span> <span class="c1">// Type checked comparison (no conversion of types and values)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// false...... data types do not match</span> <span class="c1">// Explicit type coercion</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!!</span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... data types and values match</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!!</span><span class="mf">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// false... data types match, but values differ</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="w"> </span><span class="mf">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... only ±0 and NaN are "falsy" numbers</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"0"</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// true.... only the empty string is "falsy"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">({}));</span><span class="w"> </span><span class="c1">// true.... all objects are "truthy"</span> </pre></div> <p>The new operator can be used to create an object wrapper for a Boolean primitive. However, the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">typeof</span> operator does not return <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">boolean</span> for the object wrapper, it returns <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">object</span>. Because all objects evaluate as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">true</span>, a method such as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.valueOf()</span>, or <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.toString()</span>, must be used to retrieve the wrapped value. For explicit coercion to the Boolean type, Mozilla recommends that the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Boolean()</span> function (without <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">new</span>) be used in preference to the Boolean object. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// Object false {}</span> <span class="kd">const</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// Boolean true</span> <span class="kd">const</span><span class="w"> </span><span class="nx">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">());</span><span class="w"> </span><span class="c1">// Boolean false</span> <span class="kd">let</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// Not recommended</span> <span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">());</span><span class="w"> </span><span class="c1">// Preferred</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="mf">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">-</span><span class="mf">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="s2">""</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="kc">undefined</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nx">b</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">()</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="ow">new</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">()</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nx">t</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Never this"</span><span class="p">);</span> <span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">([]</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="ow">typeof</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s2">"object"</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="nx">b</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s2">"false"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Always this"</span><span class="p">);</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Symbol">Symbol</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=18" title="Edit section: Symbol"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>New in ECMAScript6. A <i>Symbol</i> is a unique and immutable identifier. </p><p>Example: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Symbol</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span> <span class="kd">const</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Symbol</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span> <span class="nx">x</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// => false</span> <span class="kd">const</span><span class="w"> </span><span class="nx">symbolObject</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">normalObject</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span> <span class="c1">// since x and y are unique,</span> <span class="c1">// they can be used as unique keys in an object</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">x</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">y</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2</span><span class="p">;</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">x</span><span class="p">];</span><span class="w"> </span><span class="c1">// => 1</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">y</span><span class="p">];</span><span class="w"> </span><span class="c1">// => 2</span> <span class="c1">// as compared to normal numeric keys</span> <span class="nx">normalObject</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span> <span class="nx">normalObject</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// overrides the value of 1</span> <span class="nx">normalObject</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span><span class="w"> </span><span class="c1">// => 2</span> <span class="c1">// changing the value of x does not change the key stored in the object</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Symbol</span><span class="p">(</span><span class="mf">3</span><span class="p">);</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">x</span><span class="p">];</span><span class="w"> </span><span class="c1">// => undefined</span> <span class="c1">// changing x back just creates another unique Symbol</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Symbol</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span> <span class="nx">symbolObject</span><span class="p">[</span><span class="nx">x</span><span class="p">];</span><span class="w"> </span><span class="c1">// => undefined</span> </pre></div> <p>There are also <i>well known symbols</i>. </p><p>One of which is <code>Symbol.iterator</code>; if something implements <code>Symbol.iterator</code>, it's iterable: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">];</span><span class="w"> </span><span class="c1">// x is an Array</span> <span class="nx">x</span><span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">];</span><span class="w"> </span><span class="c1">// and Arrays are iterable</span> <span class="kd">const</span><span class="w"> </span><span class="nx">xIterator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]();</span><span class="w"> </span><span class="c1">// The [Symbol.iterator] function should provide an iterator for x</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: 1, done: false }</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: 2, done: false }</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: 3, done: false }</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: 4, done: false }</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: undefined, done: true }</span> <span class="nx">xIterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span><span class="w"> </span><span class="c1">// { value: undefined, done: true }</span> <span class="c1">// for..of loops automatically iterate values</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span><span class="w"> </span><span class="c1">// 1 2 3 4</span> <span class="p">}</span> <span class="c1">// Sets are also iterable:</span> <span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nb">Set</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span><span class="w"> </span><span class="c1">// true</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">([</span><span class="s1">'apple'</span><span class="p">,</span><span class="w"> </span><span class="s1">'orange'</span><span class="p">]))</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span><span class="w"> </span><span class="c1">// "apple" "orange"</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Native_objects">Native objects</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=19" title="Edit section: Native objects"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The JavaScript language provides a handful of native <a href="/wiki/Object_(computer_science)" title="Object (computer science)">objects</a>. JavaScript native objects are considered part of the JavaScript specification. JavaScript environment notwithstanding, this set of objects should always be available. </p> <div class="mw-heading mw-heading3"><h3 id="Array">Array</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=20" title="Edit section: Array"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Array_data_type" class="mw-redirect" title="Array data type">Array data type</a></div> <p>An <a href="/wiki/Array_data_type" class="mw-redirect" title="Array data type">Array</a> is a JavaScript object prototyped from the <code>Array</code> constructor specifically designed to store data values indexed by integer keys. Arrays, unlike the basic Object type, are prototyped with methods and properties to aid the programmer in routine tasks (for example, <code>join</code>, <code>slice</code>, and <code>push</code>). </p><p>As in the <a href="/wiki/Category:C_programming_language_family" title="Category:C programming language family">C family</a>, arrays use a zero-based indexing scheme: A value that is inserted into an empty array by means of the <code>push</code> method occupies the 0th index of the array. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span><span class="w"> </span><span class="c1">// Point the variable myArray to a newly ...</span> <span class="w"> </span><span class="c1">// ... created, empty Array</span> <span class="nx">myArray</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s2">"hello World"</span><span class="p">);</span><span class="w"> </span><span class="c1">// Fill the next empty index, in this case 0</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myArray</span><span class="p">[</span><span class="mf">0</span><span class="p">]);</span><span class="w"> </span><span class="c1">// Equivalent to console.log("hello World");</span> </pre></div> <p>Arrays have a <code>length</code> property that is guaranteed to always be larger than the largest integer index used in the array. It is automatically updated, if one creates a property with an even larger index. Writing a smaller number to the <code>length</code> property will remove larger indices. </p><p>Elements of <code>Array</code>s may be accessed using normal object property access notation: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">myArray</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span><span class="w"> </span><span class="c1">// the 2nd item in myArray</span> <span class="nx">myArray</span><span class="p">[</span><span class="s2">"1"</span><span class="p">];</span> </pre></div> <p>The above two are equivalent. It's not possible to use the "dot"-notation or strings with alternative representations of the number: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">myArray</span><span class="mf">.1</span><span class="p">;</span><span class="w"> </span><span class="c1">// syntax error</span> <span class="nx">myArray</span><span class="p">[</span><span class="s2">"01"</span><span class="p">];</span><span class="w"> </span><span class="c1">// not the same as myArray[1]</span> </pre></div> <p>Declaration of an array can use either an <code>Array</code> literal or the <code>Array</code> constructor: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">myArray</span><span class="p">;</span> <span class="c1">// Array literals</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">];</span><span class="w"> </span><span class="c1">// length of 2</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,];</span><span class="w"> </span><span class="c1">// same array - You can also have an extra comma at the end</span> <span class="c1">// It's also possible to not fill in parts of the array</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">];</span><span class="w"> </span><span class="c1">// length of 6</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,];</span><span class="w"> </span><span class="c1">// same array</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="cm">/* hole */</span><span class="p">,];</span><span class="w"> </span><span class="c1">// length of 7</span> <span class="c1">// With the constructor</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// length of 6</span> <span class="nx">myArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">365</span><span class="p">);</span><span class="w"> </span><span class="c1">// an empty array with length 365</span> </pre></div> <p><a href="/wiki/Array_data_structure" class="mw-redirect" title="Array data structure">Arrays</a> are implemented so that only the defined elements use memory; they are "<a href="/wiki/Sparse_array" class="mw-redirect" title="Sparse array">sparse arrays</a>". Setting <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="nx">myArray</span><span class="p">[</span><span class="mf">10</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'someThing'</span></code> and <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="nx">myArray</span><span class="p">[</span><span class="mf">57</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'somethingOther'</span></code> only uses space for these two elements, just like any other object. The <code>length</code> of the array will still be reported as 58. The maximum length of an array is 4,294,967,295 which corresponds to 32-bit binary number (11111111111111111111111111111111)<sub>2</sub>. </p><p>One can use the object declaration literal to create objects that behave much like associative arrays in other languages: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">dog</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"brown"</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="s2">"large"</span><span class="p">};</span> <span class="nx">dog</span><span class="p">[</span><span class="s2">"color"</span><span class="p">];</span><span class="w"> </span><span class="c1">// results in "brown"</span> <span class="nx">dog</span><span class="p">.</span><span class="nx">color</span><span class="p">;</span><span class="w"> </span><span class="c1">// also results in "brown"</span> </pre></div> <p>One can use the object and array declaration literals to quickly create arrays that are associative, multidimensional, or both. (Technically, JavaScript does not support multidimensional arrays, but one can mimic them with arrays-of-arrays.) </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">cats</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[{</span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"brown"</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="s2">"large"</span><span class="p">},</span> <span class="w"> </span><span class="p">{</span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="s2">"small"</span><span class="p">}];</span> <span class="nx">cats</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="s2">"size"</span><span class="p">];</span><span class="w"> </span><span class="c1">// results in "large"</span> <span class="kd">const</span><span class="w"> </span><span class="nx">dogs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">rover</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"brown"</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="s2">"large"</span><span class="p">},</span> <span class="w"> </span><span class="nx">spot</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="s2">"small"</span><span class="p">}};</span> <span class="nx">dogs</span><span class="p">[</span><span class="s2">"spot"</span><span class="p">][</span><span class="s2">"size"</span><span class="p">];</span><span class="w"> </span><span class="c1">// results in "small"</span> <span class="nx">dogs</span><span class="p">.</span><span class="nx">rover</span><span class="p">.</span><span class="nx">color</span><span class="p">;</span><span class="w"> </span><span class="c1">// results in "brown"</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Date">Date</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=21" title="Edit section: Date"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>A <code>Date</code> object stores a signed millisecond count with zero representing 1970-01-01 00:00:00 UT and a range of ±10<sup>8</sup> days. There are several ways of providing arguments to the <code>Date</code> constructor. Note that months are zero-based. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">();</span><span class="w"> </span><span class="c1">// create a new Date instance representing the current time/date.</span> <span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">(</span><span class="mf">2010</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// create a new Date instance representing 2010-Mar-01 00:00:00</span> <span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">(</span><span class="mf">2010</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">14</span><span class="p">,</span><span class="w"> </span><span class="mf">25</span><span class="p">,</span><span class="w"> </span><span class="mf">30</span><span class="p">);</span><span class="w"> </span><span class="c1">// create a new Date instance representing 2010-Mar-01 14:25:30</span> <span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">(</span><span class="s2">"2010-3-1 14:25:30"</span><span class="p">);</span><span class="w"> </span><span class="c1">// create a new Date instance from a String.</span> </pre></div> <p>Methods to extract fields are provided, as well as a useful <code>toString</code>: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">(</span><span class="mf">2010</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">14</span><span class="p">,</span><span class="w"> </span><span class="mf">25</span><span class="p">,</span><span class="w"> </span><span class="mf">30</span><span class="p">);</span><span class="w"> </span><span class="c1">// 2010-Mar-01 14:25:30;</span> <span class="c1">// Displays '2010-3-1 14:25:30':</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getFullYear</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'-'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getMonth</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'-'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">d</span><span class="p">.</span><span class="nx">getDate</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">' '</span> <span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">d</span><span class="p">.</span><span class="nx">getHours</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">':'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">d</span><span class="p">.</span><span class="nx">getMinutes</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">':'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">d</span><span class="p">.</span><span class="nx">getSeconds</span><span class="p">());</span> <span class="c1">// Built-in toString returns something like 'Mon Mar 01 2010 14:25:30 GMT-0500 (EST)':</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">d</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Error">Error</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=22" title="Edit section: Error"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Custom error messages can be created using the <code>Error</code> class: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">"Something went wrong."</span><span class="p">);</span> </pre></div> <p>These can be caught by try...catch...finally blocks as described in the section on <a href="#Exception_handling">exception handling</a>. </p> <div class="mw-heading mw-heading3"><h3 id="Math">Math</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=23" title="Edit section: Math"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math</span> object contains various math-related constants (for example, <span class="texhtml mvar" style="font-style:italic;">π</span>) and functions (for example, cosine). (Note that the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math</span> object has no constructor, unlike <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Array</span> or <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Date</span>. All its methods are "static", that is "class" methods.) All the trigonometric functions use angles expressed in <a href="/wiki/Radian" title="Radian">radians</a>, not <a href="/wiki/Degree_(angle)" title="Degree (angle)">degrees</a> or <a href="/wiki/Grad_(angle)" class="mw-redirect" title="Grad (angle)">grads</a>. </p> <table class="wikitable" border="1"> <caption>Some of the constants contained in the Math object </caption> <tbody><tr> <th>Property</th> <th>Returned value<br />rounded to 5 digits</th> <th>Description </th></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.E</span></td> <td>2.7183</td> <td><span class="texhtml mvar" style="font-style:italic;"><a href="/wiki/E_(mathematical_constant)" title="E (mathematical constant)">e</a></span>: Natural logarithm base </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.LN2</span></td> <td>0.69315</td> <td><a href="/wiki/Natural_logarithm" title="Natural logarithm">Natural logarithm</a> of 2 </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.LN10</span></td> <td>2.3026</td> <td>Natural logarithm of 10 </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.LOG2E</span></td> <td>1.4427</td> <td><a href="/wiki/Logarithm" title="Logarithm">Logarithm</a> to the base 2 of <span class="texhtml mvar" style="font-style:italic;">e</span> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.LOG10E</span></td> <td>0.43429</td> <td>Logarithm to the base 10 of <span class="texhtml mvar" style="font-style:italic;">e</span> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.PI</span></td> <td>3.14159</td> <td><a href="/wiki/Pi" title="Pi"><span class="texhtml mvar" style="font-style:italic;">π</span></a>: circumference/diameter of a circle </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.SQRT1_2</span></td> <td>0.70711</td> <td><a href="/wiki/Square_root" title="Square root">Square root</a> of ½ </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.SQRT2</span></td> <td>1.4142</td> <td><a href="/wiki/Square_root_of_2" title="Square root of 2">Square root of 2</a> </td></tr></tbody></table> <table class="wikitable" border="1"> <caption>Methods of the Math object </caption> <tbody><tr> <th>Example</th> <th>Returned value<br />rounded to 5 digits</th> <th>Description </th></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.abs(-2.3)</span></td> <td>2.3</td> <td><a href="/wiki/Absolute_value" title="Absolute value">Absolute value</a> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.acos(Math.SQRT1_2)</span></td> <td><span class="nowrap"><span data-sort-value="6997785400000000000♠"></span>0.78540 <a href="/wiki/Rad_(unit)" class="mw-redirect" title="Rad (unit)">rad</a></span> = 45°</td> <td><a href="/wiki/Arccosine" class="mw-redirect" title="Arccosine">Arccosine</a> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.asin(Math.SQRT1_2)</span></td> <td><span class="nowrap"><span data-sort-value="6997785400000000000♠"></span>0.78540 rad</span> = 45°</td> <td><a href="/wiki/Arcsine" class="mw-redirect" title="Arcsine">Arcsine</a> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.atan(1)</span></td> <td><span class="nowrap"><span data-sort-value="6997785400000000000♠"></span>0.78540 rad</span> = 45°</td> <td>Half circle <a href="/wiki/Arctangent" class="mw-redirect" title="Arctangent">arctangent</a> (<span class="nowrap">⁠<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -\pi /2}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>−<!-- − --></mo> <mi>π<!-- π --></mi> <mrow class="MJX-TeXAtom-ORD"> <mo>/</mo> </mrow> <mn>2</mn> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle -\pi /2}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/4644f935f59f6d1bac8c2ef618246eee6c1f07b3" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.465ex; height:2.843ex;" alt="{\displaystyle -\pi /2}"></span>⁠</span> to <span class="nowrap">⁠<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle +\pi /2}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>+</mo> <mi>π<!-- π --></mi> <mrow class="MJX-TeXAtom-ORD"> <mo>/</mo> </mrow> <mn>2</mn> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle +\pi /2}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/b7fac17a374c4e9eae65df5546ec64b79b0e16ca" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.465ex; height:2.843ex;" alt="{\displaystyle +\pi /2}"></span>⁠</span>) </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.atan2(-3.7, -3.7)</span></td> <td><span class="nowrap"><span data-sort-value="3001764380000000000♠"></span>−2.3562 rad</span> = <span class="nowrap"><span data-sort-value="2999764380550980765♠"></span>−135°</span></td> <td>Whole circle arctangent (<span class="nowrap">⁠<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -\pi }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>−<!-- − --></mo> <mi>π<!-- π --></mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle -\pi }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/f2359073fe90a84a705e02f0c1e63b32df850a60" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:3.14ex; height:2.176ex;" alt="{\displaystyle -\pi }"></span>⁠</span> to <span class="nowrap">⁠<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle +\pi }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>+</mo> <mi>π<!-- π --></mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle +\pi }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/cbae717a544919265768ad63c73919176113fb12" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:3.14ex; height:2.176ex;" alt="{\displaystyle +\pi }"></span>⁠</span>) </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.ceil(1.1)</span></td> <td>2</td> <td>Ceiling: <a href="/wiki/Rounding" title="Rounding">round</a> up to smallest integer ≥ argument </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.cos(Math.PI/4)</span></td> <td>0.70711</td> <td><a href="/wiki/Trigonometric_functions" title="Trigonometric functions">Cosine</a> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.exp(1)</span></td> <td>2.7183</td> <td><a href="/wiki/Exponential_function" title="Exponential function">Exponential function</a>: <span class="texhtml mvar" style="font-style:italic;">e</span> raised to this power </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.floor(1.9)</span></td> <td>1</td> <td>Floor: round down to largest integer ≤ argument </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.log(Math.E)</span></td> <td>1</td> <td>Natural logarithm, base <span class="texhtml mvar" style="font-style:italic;">e</span> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.max(1, -2)</span></td> <td>1</td> <td>Maximum: <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">(x > y) ? x : y</span> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.min(1, -2)</span></td> <td><span class="nowrap"><span data-sort-value="2999800000000000000♠"></span>−2</span></td> <td>Minimum: <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">(x < y) ? x : y</span> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.pow(-3, 2)</span></td> <td>9</td> <td><a href="/wiki/Exponentiation" title="Exponentiation">Exponentiation</a> (raised to the power of): <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.pow(x, y)</span> gives x<sup>y</sup> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.random()</span></td> <td>e.g. 0.17068</td> <td><a href="/wiki/Pseudorandom" class="mw-redirect" title="Pseudorandom">Pseudorandom</a> number between 0 (inclusive) and 1 (exclusive) </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.round(1.5)</span></td> <td>2</td> <td>Round to the nearest integer; half fractions are rounded up (e.g. 1.5 rounds to 2) </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.sin(Math.PI/4)</span></td> <td>0.70711</td> <td><a href="/wiki/Sine" class="mw-redirect" title="Sine">Sine</a> </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.sqrt(49)</span></td> <td>7</td> <td>Square root </td></tr> <tr> <td><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Math.tan(Math.PI/4)</span></td> <td>1</td> <td><a href="/wiki/Trigonometric_functions" title="Trigonometric functions">Tangent</a> </td></tr></tbody></table> <div class="mw-heading mw-heading3"><h3 id="Regular_expression">Regular expression</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=24" title="Edit section: Regular expression"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Regular_expression" title="Regular expression">Regular expression</a></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="sr">/expression/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">);</span><span class="w"> </span><span class="c1">// returns Boolean</span> <span class="s2">"string"</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="sr">/expression/</span><span class="p">);</span><span class="w"> </span><span class="c1">// returns position Number</span> <span class="s2">"string"</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/expression/</span><span class="p">,</span><span class="w"> </span><span class="nx">replacement</span><span class="p">);</span> <span class="c1">// Here are some examples</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/Tom/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"My name is Tom"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Hello Tom!"</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"My name is Tom"</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="sr">/Tom/</span><span class="p">));</span><span class="w"> </span><span class="c1">// == 11 (letters before Tom)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"My name is Tom"</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/Tom/</span><span class="p">,</span><span class="w"> </span><span class="s2">"John"</span><span class="p">));</span><span class="w"> </span><span class="c1">// == "My name is John"</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Character_classes">Character classes</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=25" title="Edit section: Character classes"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// \d - digit</span> <span class="c1">// \D - non digit</span> <span class="c1">// \s - space</span> <span class="c1">// \S - non space</span> <span class="c1">// \w - word char</span> <span class="c1">// \W - non word</span> <span class="c1">// [ ] - one of</span> <span class="c1">// [^] - one not of</span> <span class="c1">// - - range</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/\d/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'0'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Digit'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/[0-9]/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'6'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Digit'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/[13579]/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'1'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Odd number'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/\S\S\s\S\S\S\S/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'My name'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Format OK'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/\w\w\w/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'Tom'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Hello Tom'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/[a-zA-Z]/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'B'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Letter'</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Character_matching">Character matching</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=26" title="Edit section: Character matching"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// A...Z a...z 0...9 - alphanumeric</span> <span class="c1">// \u0000...\uFFFF - Unicode hexadecimal</span> <span class="c1">// \x00...\xFF - ASCII hexadecimal</span> <span class="c1">// \t - tab</span> <span class="c1">// \n - new line</span> <span class="c1">// \r - CR</span> <span class="c1">// . - any character</span> <span class="c1">// | - OR</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/T.m/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s1">'Tom'</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="w"> </span><span class="p">(</span><span class="s1">'Hi Tom, Tam or Tim'</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/A|B/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"A"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="w"> </span><span class="p">(</span><span class="s1">'A or B'</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Repeaters">Repeaters</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=27" title="Edit section: Repeaters"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// ? - 0 or 1 match</span> <span class="c1">// * - 0 or more</span> <span class="c1">// + - 1 or more</span> <span class="c1">// {n} - exactly n</span> <span class="c1">// {n,} - n or more</span> <span class="c1">// {0,n} - n or less</span> <span class="c1">// {n,m} - range n to m</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab?c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"ac"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "ac", "abc"</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab*c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"ac"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "ac", "abc", "abbc", "abbbc" etc.</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab+c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"abc"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "abc", "abbc", "abbbc" etc.</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab{3}c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"abbbc"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "abbbc"</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab{3,}c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"abbbc"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "abbbc", "abbbbc", "abbbbbc" etc.</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/ab{1,3}c/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"abc"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"OK"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "abc", "abbc", "abbbc"</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Anchors">Anchors</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=28" title="Edit section: Anchors"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// ^ - string starts with</span> <span class="c1">// $ - string ends with</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/^My/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"My name is Tom"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="w"> </span><span class="p">(</span><span class="s2">"Hi!"</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/Tom$/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"My name is Tom"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="w"> </span><span class="p">(</span><span class="s2">"Hi Tom!"</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Subexpression">Subexpression</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=29" title="Edit section: Subexpression"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// ( ) - groups characters</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/water(mark)?/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"watermark"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Here is water!"</span><span class="p">);</span><span class="w"> </span><span class="c1">// match: "water", "watermark",</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="sr">/(Tom)|(John)/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="s2">"John"</span><span class="p">))</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Hi Tom or John!"</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Flags">Flags</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=30" title="Edit section: Flags"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// /g - global</span> <span class="c1">// /i - ignore upper/lower case</span> <span class="c1">// /m - allow matches to span multiple lines</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"hi tom!"</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/Tom/i</span><span class="p">,</span><span class="w"> </span><span class="s2">"John"</span><span class="p">));</span><span class="w"> </span><span class="c1">// == "hi John!"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"ratatam"</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/ta/</span><span class="p">,</span><span class="w"> </span><span class="s2">"tu"</span><span class="p">));</span><span class="w"> </span><span class="c1">// == "ratutam"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"ratatam"</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/ta/g</span><span class="p">,</span><span class="w"> </span><span class="s2">"tu"</span><span class="p">));</span><span class="w"> </span><span class="c1">// == "ratutum"</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Advanced_methods">Advanced methods</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=31" title="Edit section: Advanced methods"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">my_array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">my_string</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">my_delimiter</span><span class="p">);</span> <span class="c1">// example</span> <span class="nx">my_array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"dog,cat,cow"</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">","</span><span class="p">);</span><span class="w"> </span><span class="c1">// my_array==["dog","cat","cow"];</span> <span class="nx">my_array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">my_string</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">my_expression</span><span class="p">);</span> <span class="c1">// example</span> <span class="nx">my_array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"We start at 11:30, 12:15 and 16:45"</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/\d\d:\d\d/g</span><span class="p">);</span><span class="w"> </span><span class="c1">// my_array==["11:30","12:15","16:45"];</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Capturing_groups">Capturing groups</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=32" title="Edit section: Capturing groups"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">myRe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sr">/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})/</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">results</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">myRe</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="s2">"The date and time are 2009-09-08 09:37:08."</span><span class="p">);</span> <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">results</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Matched: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">results</span><span class="p">[</span><span class="mf">0</span><span class="p">]);</span><span class="w"> </span><span class="c1">// Entire match</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">my_date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">results</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span><span class="w"> </span><span class="c1">// First group == "2009-09-08"</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">my_time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">results</span><span class="p">[</span><span class="mf">2</span><span class="p">];</span><span class="w"> </span><span class="c1">// Second group == "09:37:08"</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`It is </span><span class="si">${</span><span class="nx">my_time</span><span class="si">}</span><span class="sb"> on </span><span class="si">${</span><span class="nx">my_date</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Did not find a valid date!"</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Function">Function</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=33" title="Edit section: Function"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Every function in JavaScript is an instance of the <code>Function</code> constructor: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// x, y is the argument. 'return x + y' is the function body, which is the last in the argument list.</span> <span class="kd">const</span><span class="w"> </span><span class="nx">add</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Function</span><span class="p">(</span><span class="s1">'x'</span><span class="p">,</span><span class="w"> </span><span class="s1">'y'</span><span class="p">,</span><span class="w"> </span><span class="s1">'return x + y'</span><span class="p">);</span> <span class="nx">add</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 3</span> </pre></div> <p>The add function above may also be defined using a function expression: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">add</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span> <span class="p">};</span> <span class="nx">add</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 3</span> </pre></div> <p>In ES6, arrow function syntax was added, allowing functions that return a value to be more concise. They also retain the <code>this</code> of the global object instead of inheriting it from where it was called / what it was called on, unlike the <code>function() {}</code> expression. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">add</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="p">{</span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;};</span> <span class="c1">// values can also be implicitly returned (i.e. no return statement is needed)</span> <span class="kd">const</span><span class="w"> </span><span class="nx">addImplicit</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span> <span class="nx">add</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 3</span> <span class="nx">addImplicit</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="c1">// => 3</span> </pre></div> <p>For functions that need to be hoisted, there is a separate expression: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">add</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span> <span class="p">}</span> <span class="nx">add</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 3</span> </pre></div> <p>Hoisting allows you to use the function before it is "declared": </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">add</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 3, not a ReferenceError</span> <span class="kd">function</span><span class="w"> </span><span class="nx">add</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p>A function instance has properties and methods. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">subtract</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span> <span class="p">}</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">subtract</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span><span class="w"> </span><span class="c1">// => 2, arity of the function (number of arguments)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">subtract</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span> <span class="cm">/*</span> <span class="cm">"function subtract(x, y) {</span> <span class="cm"> return x - y;</span> <span class="cm">}"</span> <span class="cm">*/</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Operators">Operators</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=34" title="Edit section: Operators"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The '+' operator is <a href="/wiki/Operator_overloading" title="Operator overloading">overloaded</a>: it is used for string concatenation and arithmetic addition. This may cause problems when inadvertently mixing strings and numbers. As a unary operator, it can convert a numeric string to a number. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Concatenate 2 strings</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'He'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'llo'</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays Hello</span> <span class="c1">// Add two numbers</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 8</span> <span class="c1">// Adding a number and a string results in concatenation (from left to right)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'2'</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 22</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'$'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">4</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays $34, but $7 may have been expected</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'$'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="mf">3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">4</span><span class="p">));</span><span class="w"> </span><span class="c1">// displays $7</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">4</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'7'</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 77, numbers stay numbers until a string is added</span> <span class="c1">// Convert a string to a number using the unary plus</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">+</span><span class="s1">'2'</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">+</span><span class="s1">'Hello'</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays NaN</span> </pre></div> <p>Similarly, the '*' operator is overloaded: it can convert a string into a number. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">'6'</span><span class="o">*</span><span class="mf">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 8</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mf">3</span><span class="o">*</span><span class="s1">'7'</span><span class="p">);</span><span class="w"> </span><span class="c1">// 21</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'3'</span><span class="o">*</span><span class="s1">'7'</span><span class="p">);</span><span class="w"> </span><span class="c1">// 21</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'hello'</span><span class="o">*</span><span class="s1">'world'</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays NaN</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Arithmetic">Arithmetic</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=35" title="Edit section: Arithmetic"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript supports the following <b>binary arithmetic operators</b>: </p> <table class="wikitable"> <tbody><tr> <td align="center"><code>+</code></td> <td>addition </td></tr> <tr> <td align="center"><code>-</code></td> <td>subtraction </td></tr> <tr> <td align="center"><code>*</code></td> <td>multiplication </td></tr> <tr> <td align="center"><code>/</code></td> <td>division (returns a floating-point value) </td></tr> <tr> <td align="center"><code>%</code></td> <td>modulo (returns the remainder) </td></tr> <tr> <td align="center"><code>**</code></td> <td>exponentiation </td></tr></tbody></table> <p>JavaScript supports the following <b>unary arithmetic operators</b>: </p> <table class="wikitable"> <tbody><tr> <td align="center"><code>+</code></td> <td>unary conversion of string to number </td></tr> <tr> <td align="center"><code>-</code></td> <td>unary negation (reverses the sign) </td></tr> <tr> <td align="center"><code>++</code></td> <td>increment (can be prefix or postfix) </td></tr> <tr> <td align="center"><code>--</code></td> <td>decrement (can be prefix or postfix) </td></tr></tbody></table> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">++</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// x becomes 2; displays 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="o">++</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 2; x becomes 3</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// x is 3; displays 3</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="o">--</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 3; x becomes 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 2; x is 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">--</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// x becomes 1; displays 1</span> </pre></div> <p>The modulo operator displays the remainder after division by the modulus. If negative numbers are involved, the returned value depends on the operand. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">17</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="o">%</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="o">%</span><span class="mf">6</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 5</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">-</span><span class="nx">x</span><span class="o">%</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays -2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">-</span><span class="nx">x</span><span class="o">%-</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays -2</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="o">%-</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 2</span> </pre></div> <p>To always return a non-negative number, re-add the modulus and apply the modulo operator again: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">17</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">((</span><span class="o">-</span><span class="nx">x</span><span class="o">%</span><span class="mf">5</span><span class="o">+</span><span class="mf">5</span><span class="p">)</span><span class="o">%</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays 3</span> </pre></div> <p>You could also do: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">17</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">abs</span><span class="p">(</span><span class="o">-</span><span class="nx">x</span><span class="o">%</span><span class="mf">5</span><span class="p">));</span><span class="w"> </span><span class="c1">// also 3</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Assignment">Assignment</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=36" title="Edit section: Assignment"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <table class="wikitable"> <tbody><tr> <td align="center"><code>=</code></td> <td>assign </td></tr> <tr> <td align="center"><code>+=</code></td> <td>add and assign </td></tr> <tr> <td align="center"><code>-=</code></td> <td>subtract and assign </td></tr> <tr> <td align="center"><code>*=</code></td> <td>multiply and assign </td></tr> <tr> <td align="center"><code>/=</code></td> <td>divide and assign </td></tr> <tr> <td align="center"><code>%=</code></td> <td>modulo and assign </td></tr> <tr> <td align="center"><code>**=</code></td> <td>exponentiation and assign </td></tr></tbody></table> <p><a href="/wiki/Assignment_(computer_science)" title="Assignment (computer science)">Assignment</a> of <a href="/wiki/Primitive_type" class="mw-redirect" title="Primitive type">primitive types</a> </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">9</span><span class="p">;</span> <span class="nx">x</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span><span class="w"> </span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 10</span> <span class="nx">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mf">30</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 300</span> <span class="nx">x</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="mf">6</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 50</span> <span class="nx">x</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="mf">3</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 47</span> <span class="nx">x</span><span class="w"> </span><span class="o">%=</span><span class="w"> </span><span class="mf">7</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 5</span> </pre></div> <p>Assignment of object types </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="cm">/**</span> <span class="cm"> * To learn JavaScript objects...</span> <span class="cm"> */</span> <span class="kd">const</span><span class="w"> </span><span class="nx">object_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">};</span><span class="w"> </span><span class="c1">// assign reference of newly created object to object_1</span> <span class="kd">let</span><span class="w"> </span><span class="nx">object_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">};</span> <span class="kd">let</span><span class="w"> </span><span class="nx">object_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">object_2</span><span class="p">;</span><span class="w"> </span><span class="c1">// object_3 references the same object as object_2 does</span> <span class="w"> </span> <span class="nx">object_3</span><span class="p">.</span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2</span><span class="p">;</span> <span class="nx">message</span><span class="p">();</span><span class="w"> </span><span class="c1">// displays 1 2 2</span> <span class="w"> </span> <span class="nx">object_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">object_1</span><span class="p">;</span><span class="w"> </span><span class="c1">// object_2 now references the same object as object_1</span> <span class="w"> </span><span class="c1">// object_3 still references what object_2 referenced before</span> <span class="nx">message</span><span class="p">();</span><span class="w"> </span><span class="c1">// displays 1 1 2</span> <span class="w"> </span> <span class="nx">object_2</span><span class="p">.</span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">7</span><span class="p">;</span><span class="w"> </span><span class="c1">// modifies object_1</span> <span class="nx">message</span><span class="p">();</span><span class="w"> </span><span class="c1">// displays 7 7 2</span> <span class="nx">object_3</span><span class="p">.</span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">5</span><span class="p">;</span><span class="w"> </span><span class="c1">// object_3 doesn't change object_2</span> <span class="nx">message</span><span class="p">();</span><span class="w"> </span><span class="c1">// displays 7 7 5</span> <span class="nx">object_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">object_2</span><span class="p">;</span><span class="w"> </span> <span class="nx">object_3</span><span class="p">.</span><span class="nx">a</span><span class="o">=</span><span class="mf">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// object_3 changes object_1 and object_2</span> <span class="nx">message</span><span class="p">();</span><span class="w"> </span><span class="c1">// displays 4 4 4</span> <span class="cm">/**</span> <span class="cm"> * Prints the console.log message</span> <span class="cm"> */</span> <span class="kd">function</span><span class="w"> </span><span class="nx">message</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">object_1</span><span class="p">.</span><span class="nx">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">object_2</span><span class="p">.</span><span class="nx">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">object_3</span><span class="p">.</span><span class="nx">a</span><span class="p">);</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Destructuring_assignment">Destructuring assignment</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=37" title="Edit section: Destructuring assignment"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In Mozilla's JavaScript, since version 1.7, destructuring assignment allows the assignment of parts of data structures to several variables at once. The left hand side of an assignment is a pattern that resembles an arbitrarily nested object/array literal containing l-lvalues at its leaves that are to receive the substructures of the assigned value. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">,</span><span class="w"> </span><span class="nx">d</span><span class="p">,</span><span class="w"> </span><span class="nx">e</span><span class="p">;</span> <span class="p">[</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">];</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">a</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">b</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">c</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 3,4,5</span> <span class="nx">e</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">foo</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="nx">bar</span><span class="o">:</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="nx">baz</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="s1">'Baz'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Content'</span><span class="p">]};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">arr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span> <span class="p">({</span><span class="nx">baz</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">arr</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="nx">arr</span><span class="p">[</span><span class="mf">3</span><span class="p">]],</span><span class="w"> </span><span class="nx">foo</span><span class="o">:</span><span class="w"> </span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">bar</span><span class="o">:</span><span class="w"> </span><span class="nx">b</span><span class="p">}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">e</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">a</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">b</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">arr</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 5,6,Baz,,,Content</span> <span class="p">[</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">];</span><span class="w"> </span><span class="c1">// swap contents of a and b</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">','</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 6,5</span> <span class="p">[</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">];</span><span class="w"> </span><span class="c1">// permutations</span> <span class="p">[</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">];</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">a</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">b</span><span class="si">}</span><span class="sb">,</span><span class="si">${</span><span class="nx">c</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span><span class="w"> </span><span class="c1">// displays: 4,5,3</span> </pre></div> <div class="mw-heading mw-heading4"><h4 id="Spread/rest_operator"><span id="Spread.2Frest_operator"></span>Spread/rest operator</h4><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=38" title="Edit section: Spread/rest operator"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The ECMAScript 2015 standard introduced the "<code>...</code>" array operator, for the related concepts of "spread syntax"<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> and "rest parameters".<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> Object spreading was added in ECMAScript 2018. </p><p><b>Spread syntax</b> provides another way to destructure arrays and objects. For arrays, it indicates that the elements should be used as the parameters in a function call or the items in an array literal. For objects, it can be used for merging objects together or overriding properties. </p><p>In other words, "<code>...</code>" transforms "<code>[...foo]</code>" into "<code>[foo[0], foo[1], foo[2]]</code>", and "<code>this.bar(...foo);</code>" into "<code>this.bar(foo[0], foo[1], foo[2]);</code>", and "<code>{ ...bar }</code>" into <code>{ prop: bar.prop, prop2: bar.prop2 }</code>. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span></span><span class="linenos" data-line="1"></span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">];</span> <span class="linenos" data-line="2"></span> <span class="linenos" data-line="3"></span><span class="c1">// It can be used multiple times in the same expression</span> <span class="linenos" data-line="4"></span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[...</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="nx">a</span><span class="p">];</span><span class="w"> </span><span class="c1">// b = [1, 2, 3, 4, 1, 2, 3, 4];</span> <span class="linenos" data-line="5"></span> <span class="linenos" data-line="6"></span><span class="c1">// It can be combined with non-spread items.</span> <span class="linenos" data-line="7"></span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">9</span><span class="p">];</span><span class="w"> </span><span class="c1">// c = [5, 6, 1, 2, 3, 4, 7, 9];</span> <span class="linenos" data-line="8"></span> <span class="linenos" data-line="9"></span><span class="c1">// For comparison, doing this without the spread operator </span> <span class="linenos" data-line="10"></span><span class="c1">// creates a nested array.</span> <span class="linenos" data-line="11"></span><span class="kd">const</span><span class="w"> </span><span class="nx">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">];</span><span class="w"> </span><span class="c1">// d = [[1, 2, 3, 4], [1, 2, 3, 4]]</span> <span class="linenos" data-line="12"></span> <span class="linenos" data-line="13"></span><span class="c1">// It works the same with function calls</span> <span class="linenos" data-line="14"></span><span class="kd">function</span><span class="w"> </span><span class="nx">foo</span><span class="p">(</span><span class="nx">arg1</span><span class="p">,</span><span class="w"> </span><span class="nx">arg2</span><span class="p">,</span><span class="w"> </span><span class="nx">arg3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="linenos" data-line="15"></span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">arg1</span><span class="si">}</span><span class="sb">:</span><span class="si">${</span><span class="nx">arg2</span><span class="si">}</span><span class="sb">:</span><span class="si">${</span><span class="nx">arg3</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <span class="linenos" data-line="16"></span><span class="p">}</span> <span class="linenos" data-line="17"></span> <span class="linenos" data-line="18"></span><span class="c1">// You can use it even if it passes more parameters than the function will use</span> <span class="linenos" data-line="19"></span><span class="nx">foo</span><span class="p">(...</span><span class="nx">a</span><span class="p">);</span><span class="w"> </span><span class="c1">// "1:2:3" → foo(a[0], a[1], a[2], a[3]);</span> <span class="linenos" data-line="20"></span> <span class="linenos" data-line="21"></span><span class="c1">// You can mix it with non-spread parameters</span> <span class="linenos" data-line="22"></span><span class="nx">foo</span><span class="p">(</span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span><span class="w"> </span><span class="c1">// "5:1:2" → foo(5, a[0], a[1], a[2], a[3], 6);</span> <span class="linenos" data-line="23"></span> <span class="linenos" data-line="24"></span><span class="c1">// For comparison, doing this without the spread operator</span> <span class="linenos" data-line="25"></span><span class="c1">// assigns the array to arg1, and nothing to the other parameters.</span> <span class="linenos" data-line="26"></span><span class="nx">foo</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span><span class="w"> </span><span class="c1">// "1,2,3,4:undefined:undefined"</span> <span class="linenos" data-line="27"></span> <span class="linenos" data-line="28"></span><span class="kd">const</span><span class="w"> </span><span class="nx">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="o">:</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="p">};</span> <span class="linenos" data-line="29"></span> <span class="linenos" data-line="30"></span><span class="c1">// This would copy the object</span> <span class="linenos" data-line="31"></span><span class="kd">const</span><span class="w"> </span><span class="nx">copy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="nx">bar</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// copy = { a: 1, b: 2, c: 3 };</span> <span class="linenos" data-line="32"></span> <span class="linenos" data-line="33"></span><span class="c1">// "b" would be overridden here</span> <span class="linenos" data-line="34"></span><span class="kd">const</span><span class="w"> </span><span class="nx">override</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="nx">bar</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">4</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// override = { a: 1, c: 3, b: 4 }</span> </pre></div> <p>When <code>...</code> is used in a function <i>declaration</i>, it indicates a <b>rest parameter</b>. The rest parameter must be the final named parameter in the function's parameter list. It will be assigned an <code>Array</code> containing any arguments passed to the function in excess of the other named parameters. In other words, it gets "the rest" of the arguments passed to the function (hence the name). </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">foo</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="nx">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span> <span class="p">}</span> <span class="nx">foo</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// "3" → c = [3, 4, 5]</span> <span class="nx">foo</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="s1">'b'</span><span class="p">);</span><span class="w"> </span><span class="c1">// "0" → c = []</span> </pre></div> <p>Rest parameters are similar to Javascript's <code>arguments</code> object, which is an array-like object that contains all of the parameters (named and unnamed) in the current function call. Unlike <code>arguments</code>, however, rest parameters are true <code>Array</code> objects, so methods such as <code>.slice()</code> and <code>.sort()</code> can be used on them directly. </p> <div class="mw-heading mw-heading3"><h3 id="Comparison">Comparison</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=39" title="Edit section: Comparison"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <table class="wikitable"> <tbody><tr> <td align="center"><code>==</code></td> <td>equal </td></tr> <tr> <td align="center"><code>!=</code></td> <td>not equal </td></tr> <tr> <td align="center"><code>></code></td> <td>greater than </td></tr> <tr> <td align="center"><code>>=</code></td> <td>greater than or equal to </td></tr> <tr> <td align="center"><code><</code></td> <td>less than </td></tr> <tr> <td align="center"><code><=</code></td> <td>less than or equal to </td></tr> <tr> <td align="center"><code>===</code></td> <td>identical (equal and of same type) </td></tr> <tr> <td align="center"><code>!==</code></td> <td>not identical </td></tr></tbody></table> <p>Variables referencing objects are equal or identical only if they reference the same object: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">obj1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">obj2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">obj3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">obj1</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">obj2</span><span class="p">);</span><span class="w"> </span><span class="c1">//false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj3</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">obj1</span><span class="p">);</span><span class="w"> </span><span class="c1">//true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj3</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nx">obj1</span><span class="p">);</span><span class="w"> </span><span class="c1">//true</span> </pre></div> <p>See also <a href="#String">String</a>. </p> <div class="mw-heading mw-heading3"><h3 id="Logical">Logical</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=40" title="Edit section: Logical"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript provides four logical operators: </p> <ul><li>unary <a href="/wiki/Negation" title="Negation">negation</a> (<code>NOT = !a</code>)</li> <li>binary <a href="/wiki/Logical_disjunction" title="Logical disjunction">disjunction</a> (<code>OR = a || b</code>) and <a href="/wiki/Logical_conjunction" title="Logical conjunction">conjunction</a> (<code>AND = a && b</code>)</li> <li>ternary <a href="/wiki/Conditional_(programming)" class="mw-redirect" title="Conditional (programming)">conditional</a> (<code>c ? t : f</code>)</li></ul> <p>In the context of a logical operation, any expression evaluates to true except the following<b>:</b> </p> <ul><li>Strings: <code>""</code>, <code>''</code>,</li> <li>Numbers: <code>0</code>, <code>-0</code>, <code>NaN</code>,</li> <li>Special: <code>null</code>, <code>undefined</code>,</li> <li>Boolean: <code>false</code>.</li></ul> <p>The Boolean function can be used to explicitly convert to a primitive of type <code>Boolean</code>: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Only empty strings return false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">"false"</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">"0"</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="c1">// Only zero and NaN return false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="kc">NaN</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="o">-</span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// equivalent to -1*0</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="o">-</span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="c1">// All objects return true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="k">this</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">({})</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">([])</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span> <span class="c1">// These types return false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="kc">undefined</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// equivalent to Boolean()</span> </pre></div> <p>The NOT operator evaluates its operand as a Boolean and returns the negation. Using the operator twice in a row, as a <a href="/wiki/Double_negative" title="Double negative">double negative</a>, explicitly converts an expression to a primitive of type Boolean: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="w"> </span><span class="o">!</span><span class="mf">0</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="mf">0</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!!</span><span class="mf">1</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!!</span><span class="mf">1</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="mf">1</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!!</span><span class="mf">0</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="mf">0</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!</span><span class="mf">1</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!</span><span class="mf">1</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="mf">1</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!</span><span class="s2">""</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="s2">""</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!!</span><span class="s2">"s"</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!!</span><span class="s2">"s"</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">"s"</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!!</span><span class="s2">""</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">""</span><span class="p">));</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nb">Boolean</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="o">!</span><span class="s2">"s"</span><span class="p">);</span><span class="w"> </span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="o">!</span><span class="s2">"s"</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="nb">Boolean</span><span class="p">(</span><span class="o">!</span><span class="s2">"s"</span><span class="p">));</span> </pre></div> <p>The ternary operator can also be used for explicit conversion: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([]</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// “truthy”, but the comparison uses [].toString()</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">0</span><span class="p">]</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// [0].toString() == "0"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"0"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"0"</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// "0" → 0 ... (0 == 0) ... 0 ← false</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">1</span><span class="p">]</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// [1].toString() == "1"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"1"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"1"</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// "1" → 1 ... (1 == 1) ... 1 ← true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">2</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">([</span><span class="mf">2</span><span class="p">]</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// [2].toString() == "2"</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"2"</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"2"</span><span class="o">?</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="c1">// "2" → 2 ... (2 != 1) ... 1 ← true</span> </pre></div> <p>Expressions that use features such as post–incrementation (<code>i++</code>) have an anticipated <a href="/wiki/Side_effect_(computer_science)" title="Side effect (computer science)">side effect</a>. JavaScript provides <a href="/wiki/Short-circuit_evaluation" title="Short-circuit evaluation">short-circuit evaluation</a> of expressions; the right operand is only executed if the left operand does not suffice to determine the value of the expression. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// When a is true, there is no reason to evaluate b.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// When a is false, there is no reason to evaluate b.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">c</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">f</span><span class="p">);</span><span class="w"> </span><span class="c1">// When c is true, there is no reason to evaluate f.</span> </pre></div> <p>In early versions of JavaScript and <a href="/wiki/JScript" title="JScript">JScript</a>, the binary logical operators returned a Boolean value (like most C-derived programming languages). However, all contemporary implementations return one of their operands instead: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// if a is true, return a, otherwise return b</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span><span class="w"> </span><span class="c1">// if a is false, return a, otherwise return b</span> </pre></div> <p>Programmers who are more familiar with the behavior in C might find this feature surprising, but it allows for a more concise expression of patterns like <a href="/wiki/Null_coalescing_operator" title="Null coalescing operator">null coalescing</a>: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="s2">"(default)"</span><span class="p">;</span><span class="w"> </span><span class="c1">// assigns t, or the default value, if t is null, empty, etc.</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Logical_assignment">Logical assignment</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=41" title="Edit section: Logical assignment"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <table class="wikitable"> <tbody><tr> <td align="center"><code>??=</code> </td> <td>Nullish assignment </td></tr> <tr> <td align="center"><code>||=</code> </td> <td>Logical Or assignment </td></tr> <tr> <td align="center"><code>&&=</code> </td> <td>Logical And assignment </td></tr></tbody></table> <div class="mw-heading mw-heading3"><h3 id="Bitwise">Bitwise</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=42" title="Edit section: Bitwise"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript supports the following <b>binary <a href="/wiki/Bitwise_operation" title="Bitwise operation">bitwise operators</a></b>: </p> <table class="wikitable"> <tbody><tr> <td align="center"><code>&</code></td> <td>AND </td></tr> <tr> <td align="center"><code>|</code></td> <td>OR </td></tr> <tr> <td align="center"><code>^</code></td> <td>XOR </td></tr> <tr> <td align="center"><code>!</code></td> <td>NOT </td></tr> <tr> <td align="center"><code><<</code></td> <td>shift left (zero fill at right) </td></tr> <tr> <td align="center"><code>>></code></td> <td>shift right (sign-propagating); copies of the<br />leftmost bit (sign bit) are shifted in from the left </td></tr> <tr> <td align="center"><code>>>></code></td> <td>shift right (zero fill at left). For positive numbers,<br /><code>>></code> and <code>>>></code> yield the same result. </td></tr></tbody></table> <p>Examples: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">11</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="mf">6</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// 2</span> </pre></div> <p>JavaScript supports the following <b>unary bitwise operator</b>: </p> <table class="wikitable"> <tbody><tr> <td align="center"><code>~</code></td> <td>NOT (inverts the bits) </td></tr></tbody></table> <div class="mw-heading mw-heading3"><h3 id="Bitwise_Assignment">Bitwise Assignment</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=43" title="Edit section: Bitwise Assignment"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript supports the following <b>binary assignment operators:</b> </p> <table class="wikitable"> <tbody><tr> <td align="center"><code>&=</code></td> <td>and </td></tr> <tr> <td align="center"><code>|=</code></td> <td>or </td></tr> <tr> <td align="center"><code>^=</code></td> <td>xor </td></tr> <tr> <td align="center"><code><<=</code></td> <td>shift left (zero fill at right) </td></tr> <tr> <td align="center"><code>>>=</code></td> <td>shift right (sign-propagating); copies of the<br />leftmost bit (sign bit) are shifted in from the left </td></tr> <tr> <td align="center"><code>>>>=</code></td> <td>shift right (zero fill at left). For positive numbers,<br /><code>>>=</code> and <code>>>>=</code> yield the same result. </td></tr></tbody></table> <p>Examples: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="o">=</span><span class="mf">7</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// 7</span> <span class="nx">x</span><span class="o"><<=</span><span class="mf">3</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// 7->14->28->56</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="String_2">String</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=44" title="Edit section: String"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <table class="wikitable"> <tbody><tr> <td align="center"><code>=</code></td> <td>assignment </td></tr> <tr> <td align="center"><code>+</code></td> <td>concatenation </td></tr> <tr> <td align="center"><code>+=</code></td> <td>concatenate and assign </td></tr></tbody></table> <p>Examples: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"ab"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"cd"</span><span class="p">;</span><span class="w"> </span><span class="c1">// "abcd"</span> <span class="nx">str</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="s2">"e"</span><span class="p">;</span><span class="w"> </span><span class="c1">// "abcde"</span> <span class="kd">const</span><span class="w"> </span><span class="nx">str2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// "22", not "4" or 4.</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="??"><span id=".3F.3F"></span>??</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=45" title="Edit section: ??"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="excerpt-block"><style data-mw-deduplicate="TemplateStyles:r1066933788">.mw-parser-output .excerpt-hat .mw-editsection-like{font-style:normal}</style><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable dablink excerpt-hat selfref">This section is an excerpt from <a href="/wiki/Null_coalescing_operator#JavaScript" title="Null coalescing operator">Null coalescing operator § JavaScript</a>.<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Null_coalescing_operator&action=edit">edit</a><span class="mw-editsection-bracket">]</span></span></div><div class="excerpt"> <p><a href="/wiki/JavaScript" title="JavaScript">JavaScript</a>'s nearest operator is <code>??</code>, the "nullish coalescing operator", which was added to the standard in <a href="/wiki/ECMAScript" title="ECMAScript">ECMAScript</a>'s 11th edition.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> In earlier versions, it could be used via a <a href="/wiki/Babel_(compiler)" class="mw-redirect" title="Babel (compiler)">Babel</a> plugin, and in <a href="/wiki/TypeScript" title="TypeScript">TypeScript</a>. It evaluates its left-hand operand and, if the result value is <i>not</i> "nullish" (<code>null</code> or <code>undefined</code>), takes that value as its result; otherwise, it evaluates the right-hand operand and takes the resulting value as its result. </p><p>In the following example, <code>a</code> will be assigned the value of <code>b</code> if the value of <code>b</code> is not <code>null</code> or <code>undefined</code>, otherwise it will be assigned 3. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">??</span><span class="w"> </span><span class="mf">3</span><span class="p">;</span> </pre></div> <p>Before the nullish coalescing operator, programmers would use the logical OR operator (<code>||</code>). But where <code>??</code> looks specifically for <code>null</code> or <code>undefined</code>, the <code>||</code> operator looks for any <a class="mw-selflink-fragment" href="#Boolean">falsy</a> value: <code>null</code>, <code>undefined</code>, <code>""</code>, <code>0</code>, <code>NaN</code>, and of course, <code>false</code>. </p><p>In the following example, <code>a</code> will be assigned the value of <code>b</code> if the value of <code>b</code> is <a class="mw-selflink-fragment" href="#Boolean">truthy</a>, otherwise it will be assigned 3. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="mf">3</span><span class="p">;</span> </pre></div></div></div> <div class="mw-heading mw-heading2"><h2 id="Control_structures">Control structures</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=46" title="Edit section: Control structures"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-heading mw-heading3"><h3 id="Compound_statements">Compound statements</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=47" title="Edit section: Compound statements"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>A pair of curly brackets <code>{ }</code> and an enclosed sequence of statements constitute a compound statement, which can be used wherever a statement can be used. </p> <div class="mw-heading mw-heading3"><h3 id="If_..._else">If ... else</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=48" title="Edit section: If ... else"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">expr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">//statements;</span> <span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">expr2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">//statements;</span> <span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">//statements;</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Conditional_(ternary)_operator"><span id="Conditional_.28ternary.29_operator"></span>Conditional (ternary) operator</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=49" title="Edit section: Conditional (ternary) operator"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The conditional operator creates an expression that evaluates as one of two expressions depending on a condition. This is similar to the <i>if</i> statement that selects one of two statements to execute depending on a condition. I.e., the conditional operator is to expressions what <i>if</i> is to statements. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">condition</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nx">expression</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">alternative</span><span class="p">;</span> </pre></div> <p>is the same as: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">expression</span><span class="p">;</span> <span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">alternative</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p>Unlike the <i>if</i> statement, the conditional operator cannot omit its "else-branch". </p> <div class="mw-heading mw-heading3"><h3 id="Switch_statement">Switch statement</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=50" title="Edit section: Switch statement"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Switch_statement" title="Switch statement">Switch statement</a></div> <p>The syntax of the JavaScript <a href="/wiki/Control_flow#Choice" title="Control flow">switch statement</a> is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="nx">expr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nx">SOMEVALUE</span><span class="o">:</span> <span class="w"> </span><span class="c1">// statements;</span> <span class="w"> </span><span class="k">break</span><span class="p">;</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nx">ANOTHERVALUE</span><span class="o">:</span> <span class="w"> </span><span class="c1">// statements for when ANOTHERVALUE || ORNAOTHERONE</span> <span class="w"> </span><span class="c1">// no break statement, falling through to the following case</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nx">ORANOTHERONE</span><span class="o">:</span> <span class="w"> </span><span class="c1">// statements specific to ORANOTHERONE (i.e. !ANOTHERVALUE && ORANOTHER);</span> <span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"> </span><span class="c1">//The buck stops here.</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nx">YETANOTHER</span><span class="o">:</span> <span class="w"> </span><span class="c1">// statements;</span> <span class="w"> </span><span class="k">break</span><span class="p">;</span> <span class="w"> </span><span class="k">default</span><span class="o">:</span> <span class="w"> </span><span class="c1">// statements;</span> <span class="w"> </span><span class="k">break</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> </pre></div> <ul><li><code>break;</code> is optional; however, it is usually needed, since otherwise code execution will continue to the body of the next case block. This <i>fall through</i> behavior can be used when the same set of statements apply in several cases, effectively creating a <a href="/wiki/Disjunction" class="mw-redirect" title="Disjunction">disjunction</a> between those cases.</li> <li>Add a break statement to the end of the last case as a precautionary measure, in case additional cases are added later.</li> <li>String literal values can also be used for the case values.</li> <li>Expressions can be used instead of values.</li> <li>The default case (optional) is executed when the expression does not match any other specified cases.</li> <li>Braces are required.</li></ul> <div class="mw-heading mw-heading3"><h3 id="For_loop">For loop</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=51" title="Edit section: For loop"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The syntax of the JavaScript <a href="/wiki/For_loop" title="For loop">for loop</a> is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nx">initial</span><span class="p">;</span><span class="w"> </span><span class="nx">condition</span><span class="p">;</span><span class="w"> </span><span class="nx">loop</span><span class="w"> </span><span class="nx">statement</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="cm">/*</span> <span class="cm"> statements will be executed every time</span> <span class="cm"> the for{} loop cycles, while the</span> <span class="cm"> condition is satisfied</span> <span class="cm"> */</span> <span class="w"> </span><span class="p">}</span> </pre></div> <p>or </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nx">initial</span><span class="p">;</span><span class="w"> </span><span class="nx">condition</span><span class="p">;</span><span class="w"> </span><span class="nx">loop</span><span class="w"> </span><span class="nx">statement</span><span class="p">(</span><span class="nx">iteration</span><span class="p">))</span><span class="w"> </span><span class="c1">// one statement</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="For_..._in_loop">For ... in loop</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=52" title="Edit section: For ... in loop"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The syntax of the JavaScript <code><a href="/wiki/Foreach_loop" title="Foreach loop">for ... in loop</a></code> is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">property_name</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nx">some_object</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">// statements using some_object[property_name];</span> <span class="p">}</span> </pre></div> <ul><li>Iterates through all enumerable properties of an object.</li> <li>Iterates through all used indices of array including all user-defined properties of array object, if any. Thus it may be better to use a traditional for loop with a numeric index when iterating over arrays.</li> <li>There are differences between the various Web browsers with regard to which properties will be reflected with the for...in loop statement. In theory, this is controlled by an internal state property defined by the ECMAscript standard called "DontEnum", but in practice, each browser returns a slightly different set of properties during introspection. It is useful to test for a given property using <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">some_object</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">property_name</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span></code>}. Thus, adding a method to the array prototype with <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">newMethod</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{...</span></code>} may cause <code>for ... in</code> loops to loop over the method's name.</li></ul> <div class="mw-heading mw-heading3"><h3 id="While_loop">While loop</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=53" title="Edit section: While loop"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The syntax of the JavaScript <a href="/wiki/While_loop" title="While loop">while loop</a> is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">statement1</span><span class="p">;</span> <span class="w"> </span><span class="nx">statement2</span><span class="p">;</span> <span class="w"> </span><span class="nx">statement3</span><span class="p">;</span> <span class="w"> </span><span class="p">...</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Do_..._while_loop">Do ... while loop</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=54" title="Edit section: Do ... while loop"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The syntax of the JavaScript <code><a href="/wiki/Do_while_loop" title="Do while loop">do ... while loop</a></code> is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">do</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">statement1</span><span class="p">;</span> <span class="w"> </span><span class="nx">statement2</span><span class="p">;</span> <span class="w"> </span><span class="nx">statement3</span><span class="p">;</span> <span class="w"> </span><span class="p">...</span> <span class="p">}</span><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nx">condition</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="With">With</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=55" title="Edit section: With"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The with statement adds all of the given object's properties and methods into the following block's scope, letting them be referenced as if they were local variables. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">with</span><span class="w"> </span><span class="p">(</span><span class="nb">document</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'a'</span><span class="p">);</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'b'</span><span class="p">);</span> <span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'c'</span><span class="p">);</span> <span class="p">};</span> </pre></div> <ul><li>Note the absence of <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">document.</span> before each <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">getElementById()</span> invocation.</li></ul> <p>The semantics are similar to the with statement of <a href="/wiki/Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>. </p><p>Because the availability of with statements hinders program performance and is believed to reduce code clarity (since any given variable could actually be a property from an enclosing <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">with</span>), this statement is not allowed in <i>strict mode</i>. </p> <div class="mw-heading mw-heading3"><h3 id="Labels">Labels</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=56" title="Edit section: Labels"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript supports nested labels in most implementations. Loops or blocks can be labelled for the break statement, and loops for <code>continue</code>. Although <code><a href="/wiki/Goto" title="Goto">goto</a></code> is a reserved word,<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup> <code>goto</code> is not implemented in JavaScript. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">loop1</span><span class="o">:</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mf">10</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="nx">a</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">4</span><span class="p">)</span><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="nx">loop1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Stops after the 4th attempt</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'a = '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">a</span><span class="p">);</span> <span class="w"> </span><span class="nx">loop2</span><span class="o">:</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mf">10</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="nx">b</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">3</span><span class="p">)</span><span class="w"> </span><span class="k">continue</span><span class="w"> </span><span class="nx">loop2</span><span class="p">;</span><span class="w"> </span><span class="c1">// Number 3 is skipped</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">6</span><span class="p">)</span><span class="w"> </span><span class="k">continue</span><span class="w"> </span><span class="nx">loop1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Continues the first loop, 'finished' is not shown</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'b = '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">//end of loop2</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'finished'</span><span class="p">);</span> <span class="p">}</span><span class="w"> </span><span class="c1">//end of loop1</span> <span class="nx">block1</span><span class="o">:</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Hello'</span><span class="p">);</span><span class="w"> </span><span class="c1">// Displays 'Hello'</span> <span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="nx">block1</span><span class="p">;</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'World'</span><span class="p">);</span><span class="w"> </span><span class="c1">// Will never get here</span> <span class="p">}</span> <span class="kr">goto</span><span class="w"> </span><span class="nx">block1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Parse error.</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Functions">Functions</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=57" title="Edit section: Functions"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Function_(computer_programming)" title="Function (computer programming)">Function (computer programming)</a></div> <p>A <a href="/wiki/Function_(computer_programming)" title="Function (computer programming)">function</a> is a block with a (possibly empty) parameter list that is normally given a name. A function may use local variables. If you exit the function without a return statement, the value <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">undefined</span> is returned. </p> <div class="mw-highlight mw-highlight-lang-javascript+genshitext mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">gcd</span><span class="p">(</span><span class="nx">number1</span><span class="p">,</span><span class="w"> </span><span class="nx">number2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">number1</span><span class="o">*</span><span class="nx">number2</span><span class="p">))</span><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ne">TypeError</span><span class="p">(</span><span class="s2">"Non-Numeric arguments not allowed."</span><span class="p">);</span> <span class="w"> </span><span class="nx">number1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">round</span><span class="p">(</span><span class="nx">number1</span><span class="p">);</span> <span class="w"> </span><span class="nx">number2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">round</span><span class="p">(</span><span class="nx">number2</span><span class="p">);</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">difference</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">number1</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">number2</span><span class="p">;</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">difference</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">number1</span><span class="p">;</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">difference</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nx">gcd</span><span class="p">(</span><span class="nx">number2</span><span class="p">,</span><span class="w"> </span><span class="nx">difference</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">gcd</span><span class="p">(</span><span class="nx">number1</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="nx">difference</span><span class="p">);</span> <span class="p">}</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">gcd</span><span class="p">(</span><span class="mf">60</span><span class="p">,</span><span class="w"> </span><span class="mf">40</span><span class="p">));</span><span class="w"> </span><span class="c1">// 20</span> <span class="c1">//In the absence of parentheses following the identifier 'gcd' on the RHS of the assignment below,</span> <span class="c1">//'gcd' returns a reference to the function itself without invoking it.</span> <span class="kd">let</span><span class="w"> </span><span class="nx">mygcd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">gcd</span><span class="p">;</span><span class="w"> </span><span class="c1">// mygcd and gcd reference the same function.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">mygcd</span><span class="p">(</span><span class="mf">60</span><span class="p">,</span><span class="w"> </span><span class="mf">40</span><span class="p">));</span><span class="w"> </span><span class="c1">// 20</span> </pre></div> <p>Functions are <a href="/wiki/First_class_object" class="mw-redirect" title="First class object">first class objects</a> and may be assigned to other variables. </p><p>The number of arguments given when calling a function may not necessarily correspond to the number of arguments in the function definition; a named argument in the definition that does not have a matching argument in the call will have the value <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">undefined</span> (that can be implicitly cast to false). Within the function, the arguments may also be accessed through the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">arguments</span> object; this provides access to all arguments using indices (e.g. <code class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" style="" dir="ltr"><span class="nx">arguments</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="nx">arguments</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="nx">arguments</span><span class="p">[</span><span class="nx">n</span><span class="p">]</span></code>), including those beyond the number of named arguments. (While the arguments list has a <code>.length</code> property, it is <i>not</i> an instance of <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Array</span>; it does not have methods such as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.slice()</span>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.sort()</span>, etc.) </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">add7</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">y</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">7</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">arguments</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span> <span class="p">};</span> <span class="nx">add7</span><span class="p">(</span><span class="mf">3</span><span class="p">);</span><span class="w"> </span><span class="c1">// 11</span> <span class="nx">add7</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">);</span><span class="w"> </span><span class="c1">// 9</span> </pre></div> <p>Primitive values (number, boolean, string) are passed by value. For objects, it is the reference to the object that is passed. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">obj1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">a</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">obj2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">b</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">};</span> <span class="kd">function</span><span class="w"> </span><span class="nx">foo</span><span class="p">(</span><span class="nx">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">obj2</span><span class="p">;</span><span class="w"> </span><span class="c1">// Ignores actual parameter</span> <span class="w"> </span><span class="nx">p</span><span class="p">.</span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">arguments</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span> <span class="p">}</span> <span class="nx">foo</span><span class="p">(</span><span class="nx">obj1</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span><span class="w"> </span><span class="c1">// Does not affect obj1 at all. 3 is additional parameter</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">obj1</span><span class="p">.</span><span class="nx">a</span><span class="si">}</span><span class="sb"> </span><span class="si">${</span><span class="nx">obj2</span><span class="p">.</span><span class="nx">b</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span><span class="w"> </span><span class="c1">// writes 1 3</span> </pre></div> <p>Functions can be declared inside other functions, and access the outer function's local variables. Furthermore, they implement full <a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closures</a> by remembering the outer function's local variables even after the outer function has exited. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Top"</span><span class="p">;</span> <span class="kd">let</span><span class="w"> </span><span class="nx">bar</span><span class="p">,</span><span class="w"> </span><span class="nx">baz</span><span class="p">;</span> <span class="kd">function</span><span class="w"> </span><span class="nx">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"foo var"</span><span class="p">;</span> <span class="w"> </span><span class="nx">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="nx">baz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span><span class="w"> </span><span class="p">};</span> <span class="p">}</span> <span class="nx">foo</span><span class="p">();</span> <span class="nx">baz</span><span class="p">(</span><span class="s2">"baz arg"</span><span class="p">);</span> <span class="nx">bar</span><span class="p">();</span><span class="w"> </span><span class="c1">// "baz arg" (not "foo var") even though foo() has exited.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">t</span><span class="p">);</span><span class="w"> </span><span class="c1">// Top</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Async/await"><span id="Async.2Fawait"></span>Async/await</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=58" title="Edit section: Async/await"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="excerpt-block"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1066933788"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable dablink excerpt-hat selfref">This section is an excerpt from <a href="/wiki/Async/await#In_JavaScript" title="Async/await">Async/await § In JavaScript</a>.<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Async/await&action=edit">edit</a><span class="mw-editsection-bracket">]</span></span></div><div class="excerpt"> <p>The await operator in JavaScript can only be used from inside an async function or at the top level of a <a href="/wiki/Modular_programming" title="Modular programming">module</a>. If the parameter is a <a href="/wiki/Futures_and_promises" title="Futures and promises">promise</a>, execution of the async function will resume when the promise is resolved (unless the promise is rejected, in which case an error will be thrown that can be handled with normal JavaScript <a href="/wiki/Exception_handling" title="Exception handling">exception handling</a>). If the parameter is not a promise, the parameter itself will be returned immediately.<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> </p><p>Many libraries provide promise objects that can also be used with await, as long as they match the specification for native JavaScript promises. However, promises from the <a href="/wiki/JQuery" title="JQuery">jQuery</a> library were not Promises/A+ compatible until jQuery 3.0.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup> </p><p>Here's an example (modified from this<sup id="cite_ref-Async/await_JavaScript-async-await_18-0" class="reference"><a href="#cite_note-Async/await_JavaScript-async-await-18"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup> article): </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">createNewDoc</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">response</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">db</span><span class="p">.</span><span class="nx">post</span><span class="p">({});</span><span class="w"> </span><span class="c1">// post a new doc</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">db</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">id</span><span class="p">);</span><span class="w"> </span><span class="c1">// find by id</span> <span class="p">}</span> <span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">doc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">createNewDoc</span><span class="p">();</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">doc</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="nx">main</span><span class="p">();</span> </pre></div> <a href="/wiki/Node.js" title="Node.js">Node.js</a> version 8 includes a utility that enables using the standard library callback-based methods as promises.<sup id="cite_ref-19" class="reference"><a href="#cite_note-19"><span class="cite-bracket">[</span>19<span class="cite-bracket">]</span></a></sup></div></div> <div class="mw-heading mw-heading2"><h2 id="Objects">Objects</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=59" title="Edit section: Objects"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>For convenience, types are normally subdivided into <i>primitives</i> and <i>objects</i>. Objects are entities that have an identity (they are only equal to themselves) and that map property names to values ("slots" in <a href="/wiki/Prototype-based_programming" title="Prototype-based programming">prototype-based programming</a> terminology). Objects may be thought of as <a href="/wiki/Associative_arrays" class="mw-redirect" title="Associative arrays">associative arrays</a> or hashes, and are often implemented using these data structures. However, objects have additional features, such as a <a href="/wiki/Prototype-based_programming" title="Prototype-based programming">prototype chain</a>, which ordinary associative arrays do not have. </p><p>JavaScript has several kinds of built-in objects, namely <code>Array</code>, <code>Boolean</code>, <code>Date</code>, <code>Function</code>, <code>Math</code>, <code>Number</code>, <code>Object</code>, <code>RegExp</code> and <code>String</code>. Other objects are "host objects", defined not by the language, but by the runtime environment. For example, in a browser, typical host objects belong to the DOM (window, form, links, etc.). </p> <div class="mw-heading mw-heading3"><h3 id="Creating_objects">Creating objects</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=60" title="Edit section: Creating objects"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Objects can be created using a constructor or an object literal. The constructor can use either a built-in Object function or a custom function. It is a convention that constructor functions are given a name that starts with a capital letter: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Constructor</span> <span class="kd">const</span><span class="w"> </span><span class="nx">anObject</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Object</span><span class="p">();</span> <span class="c1">// Object literal</span> <span class="kd">const</span><span class="w"> </span><span class="nx">objectA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span> <span class="kd">const</span><span class="w"> </span><span class="nx">objectA2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span><span class="w"> </span><span class="c1">// A != A2, {}s create new objects as copies.</span> <span class="kd">const</span><span class="w"> </span><span class="nx">objectB</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="nx">index1</span><span class="o">:</span><span class="w"> </span><span class="s1">'value 1'</span><span class="p">,</span><span class="w"> </span><span class="nx">index2</span><span class="o">:</span><span class="w"> </span><span class="s1">'value 2'</span><span class="p">};</span> <span class="c1">// Custom constructor (see below)</span> </pre></div> <p>Object literals and array literals allow one to easily create flexible data structures: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">myStructure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">first</span><span class="o">:</span><span class="w"> </span><span class="s2">"Mel"</span><span class="p">,</span> <span class="w"> </span><span class="nx">last</span><span class="o">:</span><span class="w"> </span><span class="s2">"Smith"</span> <span class="w"> </span><span class="p">},</span> <span class="w"> </span><span class="nx">age</span><span class="o">:</span><span class="w"> </span><span class="mf">33</span><span class="p">,</span> <span class="w"> </span><span class="nx">hobbies</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"chess"</span><span class="p">,</span><span class="w"> </span><span class="s2">"jogging"</span><span class="p">]</span> <span class="p">};</span> </pre></div> <p>This is the basis for <a href="/wiki/JSON" title="JSON">JSON</a>, which is a simple notation that uses JavaScript-like syntax for data exchange. </p> <div class="mw-heading mw-heading3"><h3 id="Methods">Methods</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=61" title="Edit section: Methods"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>A <a href="/wiki/Method_(computer_programming)" title="Method (computer programming)">method</a> is simply a function that has been assigned to a property name of an object. Unlike many object-oriented languages, there is no distinction between a function definition and a method definition in object-related JavaScript. Rather, the distinction occurs during function calling; a function can be called as a method. </p><p>When called as a method, the standard local variable <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">this</span></i> is just automatically set to the object instance to the left of the "<link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">.</span>". (There are also <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">call</span></i> and <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">apply</span></i> methods that can set <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">this</span></i> explicitly—some packages such as <a href="/wiki/JQuery" title="JQuery">jQuery</a> do unusual things with <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">this</span></i>.) </p><p>In the example below, Foo is being used as a constructor. There is nothing special about a constructor - it is just a plain function that initialises an object. When used with the <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">new</span></i> keyword, as is the norm, <i><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">this</span></i> is set to a newly created blank object. </p><p>Note that in the example below, Foo is simply assigning values to slots, some of which are functions. Thus it can assign different functions to different instances. There is no prototyping in this example. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">px</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">prefix</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"X"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kd">function</span><span class="w"> </span><span class="nx">Foo</span><span class="p">(</span><span class="nx">yz</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">prefix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"a-"</span><span class="p">;</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">yz</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">pyz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">prefix</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"Y"</span><span class="p">;</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">pyz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">prefix</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"Z"</span><span class="p">;</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">m1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px</span><span class="p">;</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">;</span> <span class="p">}</span> <span class="kd">const</span><span class="w"> </span><span class="nx">foo1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Foo</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span> <span class="kd">const</span><span class="w"> </span><span class="nx">foo2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Foo</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span> <span class="nx">foo2</span><span class="p">.</span><span class="nx">prefix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"b-"</span><span class="p">;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"foo1/2 "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">foo1</span><span class="p">.</span><span class="nx">pyz</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">foo2</span><span class="p">.</span><span class="nx">pyz</span><span class="p">());</span> <span class="c1">// foo1/2 a-Y b-Z</span> <span class="nx">foo1</span><span class="p">.</span><span class="nx">m3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px</span><span class="p">;</span><span class="w"> </span><span class="c1">// Assigns the function itself, not its evaluated result, i.e. not px()</span> <span class="kd">const</span><span class="w"> </span><span class="nx">baz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="s2">"prefix"</span><span class="o">:</span><span class="w"> </span><span class="s2">"c-"</span><span class="p">};</span> <span class="nx">baz</span><span class="p">.</span><span class="nx">m4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px</span><span class="p">;</span><span class="w"> </span><span class="c1">// No need for a constructor to make an object.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"m1/m3/m4 "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">foo1</span><span class="p">.</span><span class="nx">m1</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">foo1</span><span class="p">.</span><span class="nx">m3</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">baz</span><span class="p">.</span><span class="nx">m4</span><span class="p">());</span> <span class="c1">// m1/m3/m4 a-X a-X c-X</span> <span class="nx">foo1</span><span class="p">.</span><span class="nx">m2</span><span class="p">();</span><span class="w"> </span><span class="c1">// Throws an exception, because foo1.m2 doesn't exist.</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Constructors">Constructors</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=62" title="Edit section: Constructors"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/Constructor_function" class="mw-redirect" title="Constructor function">Constructor functions</a> simply assign values to slots of a newly created object. The values may be data or other functions. </p><p>Example: Manipulating an object: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">MyObject</span><span class="p">(</span><span class="nx">attributeA</span><span class="p">,</span><span class="w"> </span><span class="nx">attributeB</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">attributeA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">attributeA</span><span class="p">;</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">attributeB</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">attributeB</span><span class="p">;</span> <span class="p">}</span> <span class="nx">MyObject</span><span class="p">.</span><span class="nx">staticC</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">;</span><span class="w"> </span><span class="c1">// On MyObject Function, not object</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">MyObject</span><span class="p">.</span><span class="nx">staticC</span><span class="p">);</span><span class="w"> </span><span class="c1">// blue</span> <span class="kd">const</span><span class="w"> </span><span class="nx">object</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">MyObject</span><span class="p">(</span><span class="s1">'red'</span><span class="p">,</span><span class="w"> </span><span class="mf">1000</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">object</span><span class="p">.</span><span class="nx">attributeA</span><span class="p">);</span><span class="w"> </span><span class="c1">// red</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">object</span><span class="p">.</span><span class="nx">attributeB</span><span class="p">);</span><span class="w"> </span><span class="c1">// 1000</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">object</span><span class="p">.</span><span class="nx">staticC</span><span class="p">);</span><span class="w"> </span><span class="c1">// undefined</span> <span class="nx">object</span><span class="p">.</span><span class="nx">attributeC</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Date</span><span class="p">();</span><span class="w"> </span><span class="c1">// add a new property</span> <span class="ow">delete</span><span class="w"> </span><span class="nx">object</span><span class="p">.</span><span class="nx">attributeB</span><span class="p">;</span><span class="w"> </span><span class="c1">// remove a property of object</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">object</span><span class="p">.</span><span class="nx">attributeB</span><span class="p">);</span><span class="w"> </span><span class="c1">// undefined</span> </pre></div> <p>The constructor itself is referenced in the object's prototype's <i>constructor</i> slot. So, </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">Foo</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span> <span class="c1">// Use of 'new' sets prototype slots (for example, </span> <span class="c1">// x = new Foo() would set x's prototype to Foo.prototype,</span> <span class="c1">// and Foo.prototype has a constructor slot pointing back to Foo).</span> <span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Foo</span><span class="p">();</span> <span class="c1">// The above is almost equivalent to</span> <span class="kd">const</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span> <span class="nx">y</span><span class="p">.</span><span class="kr">constructor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Foo</span><span class="p">;</span> <span class="nx">y</span><span class="p">.</span><span class="kr">constructor</span><span class="p">();</span> <span class="c1">// Except</span> <span class="nx">x</span><span class="p">.</span><span class="kr">constructor</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">y</span><span class="p">.</span><span class="kr">constructor</span><span class="p">;</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">x</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">Foo</span><span class="p">;</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">y</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">Foo</span><span class="p">;</span><span class="w"> </span><span class="c1">// false</span> <span class="c1">// y's prototype is Object.prototype, not</span> <span class="c1">// Foo.prototype, since it was initialised with</span> <span class="c1">// {} instead of new Foo.</span> <span class="c1">// Even though Foo is set to y's constructor slot,</span> <span class="c1">// this is ignored by instanceof - only y's prototype's</span> <span class="c1">// constructor slot is considered.</span> </pre></div> <p>Functions are objects themselves, which can be used to produce an effect similar to "static properties" (using C++/Java terminology) as shown below. (The function object also has a special <code>prototype</code> property, as discussed in the "Inheritance" section below.) </p><p>Object deletion is rarely used as the scripting engine will <a href="/wiki/Garbage_collection_(computer_science)" title="Garbage collection (computer science)">garbage collect</a> objects that are no longer being referenced. </p> <div class="mw-heading mw-heading3"><h3 id="Inheritance">Inheritance</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=63" title="Edit section: Inheritance"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript supports inheritance hierarchies through prototyping in the manner of <a href="/wiki/Self_(programming_language)" title="Self (programming language)">Self</a>. </p><p>In the following example, the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Derived</span> class inherits from the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Base</span> class. When <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">d</span> is created as <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Derived</span>, the reference to the base instance of <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">Base</span> is copied to <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">d.base</span>. </p><p>Derive does not contain a value for <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">aBaseFunction</span>, so it is retrieved from <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">aBaseFunction</span> <i>when <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">aBaseFunction</span> is accessed</i>. This is made clear by changing the value of <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">base.aBaseFunction</span>, which is reflected in the value of <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">d.aBaseFunction</span>. </p><p>Some implementations allow the prototype to be accessed or set explicitly using the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">__proto__</span> slot as shown below. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">Base</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">anOverride</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Base::anOverride()"</span><span class="p">);</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">aBaseFunction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Base::aBaseFunction()"</span><span class="p">);</span><span class="w"> </span><span class="p">};</span> <span class="p">}</span> <span class="kd">function</span><span class="w"> </span><span class="nx">Derived</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">anOverride</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Derived::anOverride()"</span><span class="p">);</span><span class="w"> </span><span class="p">};</span> <span class="p">}</span> <span class="kd">const</span><span class="w"> </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Base</span><span class="p">();</span> <span class="nx">Derived</span><span class="p">.</span><span class="nx">prototype</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="c1">// Must be before new Derived()</span> <span class="nx">Derived</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="kr">constructor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Derived</span><span class="p">;</span><span class="w"> </span><span class="c1">// Required to make `instanceof` work</span> <span class="kd">const</span><span class="w"> </span><span class="nx">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Derived</span><span class="p">();</span><span class="w"> </span><span class="c1">// Copies Derived.prototype to d instance's hidden prototype slot.</span> <span class="nx">d</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">Derived</span><span class="p">;</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">d</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">Base</span><span class="p">;</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">base</span><span class="p">.</span><span class="nx">aBaseFunction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Base::aNEWBaseFunction()"</span><span class="p">);</span><span class="w"> </span><span class="p">};</span> <span class="nx">d</span><span class="p">.</span><span class="nx">anOverride</span><span class="p">();</span><span class="w"> </span><span class="c1">// Derived::anOverride()</span> <span class="nx">d</span><span class="p">.</span><span class="nx">aBaseFunction</span><span class="p">();</span><span class="w"> </span><span class="c1">// Base::aNEWBaseFunction()</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">aBaseFunction</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">Derived</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">aBaseFunction</span><span class="p">);</span><span class="w"> </span><span class="c1">// true</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">__proto__</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">base</span><span class="p">);</span><span class="w"> </span><span class="c1">// true in Mozilla-based implementations and false in many others.</span> </pre></div> <p>The following shows clearly how references to prototypes are <i>copied</i> on instance creation, but that changes to a prototype can affect all instances that refer to it. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">m1</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s2">"One"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kd">function</span><span class="w"> </span><span class="nx">m2</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s2">"Two"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kd">function</span><span class="w"> </span><span class="nx">m3</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s2">"Three"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kd">function</span><span class="w"> </span><span class="nx">Base</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span> <span class="nx">Base</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">m2</span><span class="p">;</span> <span class="kd">const</span><span class="w"> </span><span class="nx">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Base</span><span class="p">();</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"bar.m "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">bar</span><span class="p">.</span><span class="nx">m</span><span class="p">());</span><span class="w"> </span><span class="c1">// bar.m Two</span> <span class="kd">function</span><span class="w"> </span><span class="nx">Top</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">m3</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kd">const</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Top</span><span class="p">();</span> <span class="kd">const</span><span class="w"> </span><span class="nx">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Base</span><span class="p">();</span> <span class="nx">Base</span><span class="p">.</span><span class="nx">prototype</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">t</span><span class="p">;</span> <span class="c1">// No effect on foo, the *reference* to t is copied.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"foo.m "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">foo</span><span class="p">.</span><span class="nx">m</span><span class="p">());</span><span class="w"> </span><span class="c1">// foo.m Two</span> <span class="kd">const</span><span class="w"> </span><span class="nx">baz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Base</span><span class="p">();</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"baz.m "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">baz</span><span class="p">.</span><span class="nx">m</span><span class="p">());</span><span class="w"> </span><span class="c1">// baz.m Three</span> <span class="nx">t</span><span class="p">.</span><span class="nx">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">m1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Does affect baz, and any other derived classes.</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"baz.m1 "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">baz</span><span class="p">.</span><span class="nx">m</span><span class="p">());</span><span class="w"> </span><span class="c1">// baz.m1 One</span> </pre></div> <p>In practice many variations of these themes are used, and it can be both powerful and confusing. </p> <div class="mw-heading mw-heading2"><h2 id="Exception_handling">Exception handling</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=64" title="Edit section: Exception handling"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>JavaScript includes a <code>try ... catch ... finally</code> <a href="/wiki/Exception_handling" title="Exception handling">exception handling</a> statement to handle run-time errors. </p><p>The <code>try ... catch ... finally</code> statement catches <a href="/wiki/Exception_handling" title="Exception handling">exceptions</a> resulting from an error or a throw statement. Its syntax is as follows: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">try</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">// Statements in which exceptions might be thrown</span> <span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="p">(</span><span class="nx">errorValue</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">// Statements that execute in the event of an exception</span> <span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">// Statements that execute afterward either way</span> <span class="p">}</span> </pre></div> <p>Initially, the statements within the try block execute. If an exception is thrown, the script's control flow immediately transfers to the statements in the catch block, with the exception available as the error argument. Otherwise the catch block is skipped. The catch block can <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">throw(errorValue)</span>, if it does not want to handle a specific error. </p><p>In any case the statements in the finally block are always executed. This can be used to free resources, although memory is automatically garbage collected. </p><p>Either the catch or the finally clause may be omitted. The catch argument is required. </p><p>The Mozilla implementation allows for multiple catch statements, as an extension to the ECMAScript standard. They follow a syntax similar to that used in <a href="/wiki/Java_(programming_language)" title="Java (programming language)">Java</a>: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">statement</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"InvalidNameException"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">statement</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"InvalidIdException"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">statement</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"InvalidEmailException"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">statement</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">statement</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> </pre></div> <p>In a browser, the <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886049734"><span class="monospaced">onerror</span> event is more commonly used to trap exceptions. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="nx">onerror</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">errorValue</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="nx">lineNr</span><span class="p">)</span><span class="w"> </span><span class="p">{...;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="p">;};</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="Native_functions_and_methods">Native functions and methods</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=65" title="Edit section: Native functions and methods"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><span role="note" class="hatnote navigation-not-searchable selfref">(Not related to Web browsers.)</span> </p> <div class="mw-heading mw-heading3"><h3 id="eval_(expression)"><span id="eval_.28expression.29"></span>eval (expression)</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=66" title="Edit section: eval (expression)"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Evaluates the first parameter as an expression, which can include assignment statements. Variables local to functions can be referenced by the expression. However, <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">eval</code> represents a major security risk, as it allows a bad actor to execute arbitrary code, so its use is discouraged.<sup id="cite_ref-deve_eval_20-0" class="reference"><a href="#cite_note-deve_eval-20"><span class="cite-bracket">[</span>20<span class="cite-bracket">]</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-nodejsrepl mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">></span><span class="w"> </span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="nx">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="gp">...</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">7</span><span class="p">;</span> <span class="gp">...</span><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"val "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">eval</span><span class="p">(</span><span class="s2">"x + 2"</span><span class="p">));</span> <span class="gp">...</span><span class="w"> </span><span class="p">})();</span> <span class="nx">val</span><span class="w"> </span><span class="mf">9</span> <span class="kc">undefined</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=67" title="Edit section: See also"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a href="/wiki/Comparison_of_JavaScript-based_source_code_editors" title="Comparison of JavaScript-based source code editors">Comparison of JavaScript-based source code editors</a></li> <li><a href="/wiki/JavaScript" title="JavaScript">JavaScript</a></li></ul> <div class="mw-heading mw-heading2"><h2 id="References">References</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=68" title="Edit section: References"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1239543626">.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}</style><div class="reflist"> <div class="mw-references-wrap mw-references-columns"><ol class="references"> <li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://hepunx.rl.ac.uk/~adye/jsspec11/intro.htm#1006028">JavaScript 1.1 specification</a></span> </li> <li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20220210041253/http://speakingjs.com/es5/ch01.html">"Chapter 1. Basic JavaScript"</a>. <i>speakingjs.com</i>. Archived from <a rel="nofollow" class="external text" href="http://speakingjs.com/es5/ch01.html">the original</a> on 10 February 2022<span class="reference-accessdate">. Retrieved <span class="nowrap">22 September</span> 2020</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=speakingjs.com&rft.atitle=Chapter+1.+Basic+JavaScript&rft_id=http%3A%2F%2Fspeakingjs.com%2Fes5%2Fch01.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite id="CITEREFFlanagan2006" class="citation book cs1">Flanagan, David (2006). <span class="id-lock-registration" title="Free registration required"><a rel="nofollow" class="external text" href="https://archive.org/details/javascript00libg_297"><i>JavaScript: The definitive Guide</i></a></span>. p. <a rel="nofollow" class="external text" href="https://archive.org/details/javascript00libg_297/page/n14">16</a>. <a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/978-0-596-10199-2" title="Special:BookSources/978-0-596-10199-2"><bdi>978-0-596-10199-2</bdi></a>. <q>Omitting semicolons is not a good programming practice; you should get into the habit of inserting them.</q></cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=JavaScript%3A+The+definitive+Guide&rft.pages=16&rft.date=2006&rft.isbn=978-0-596-10199-2&rft.aulast=Flanagan&rft.aufirst=David&rft_id=https%3A%2F%2Farchive.org%2Fdetails%2Fjavascript00libg_297&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables">"Storing the information you need — Variables - Learn web development | MDN"</a>. <i>developer.mozilla.org</i>. 9 May 2023<span class="reference-accessdate">. Retrieved <span class="nowrap">23 June</span> 2023</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=developer.mozilla.org&rft.atitle=Storing+the+information+you+need+%E2%80%94+Variables+-+Learn+web+development+%7C+MDN&rft.date=2023-05-09&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FLearn%2FJavaScript%2FFirst_steps%2FVariables&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20110629131728/https://developer.mozilla.org/en/JavaScript/Guide/Values%2C_Variables%2C_and_Literals%26revision%3D22#Variables">"Values, Variables, and Literals - MDC"</a>. Mozilla Developer Network. 16 September 2010. Archived from <a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals&revision=22#Variables">the original</a> on 29 June 2011<span class="reference-accessdate">. Retrieved <span class="nowrap">1 February</span> 2020</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Values%2C+Variables%2C+and+Literals+-+MDC&rft.pub=Mozilla+Developer+Network&rft.date=2010-09-16&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen%2FJavaScript%2FGuide%2FValues%2C_Variables%2C_and_Literals%26revision%3D22%23Variables&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.w3schools.com/js/js_hoisting.asp">"JavaScript Hoisting"</a>. <a href="/wiki/W3Schools" title="W3Schools">W3Schools</a>. <q>In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.</q></cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=JavaScript+Hoisting&rft.pub=W3Schools&rft_id=https%3A%2F%2Fwww.w3schools.com%2Fjs%2Fjs_hoisting.asp&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text">"<a rel="nofollow" class="external text" href="http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html">JavaScript Scoping and Hoisting</a>", <a rel="nofollow" class="external text" href="http://www.adequatelygood.com/about.html">Ben Cherry</a>, <i><a rel="nofollow" class="external text" href="http://www.adequatelygood.com/">Adequately Good</a>,</i> 2010-02-08</span> </li> <li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text">ECMA-262 5e edition clarified this behavior with the <i>Declarative Environment Record</i> and <i>Object Environment Record</i>. With this formalism, the <i>global object</i> is the <i>Object Environment Record</i> of the global <i>Lexical Environment</i> (the <i>global scope</i>).</span> </li> <li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">"Template literals"</a>. <i>MDN Web Docs</i><span class="reference-accessdate">. Retrieved <span class="nowrap">4 November</span> 2023</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=MDN+Web+Docs&rft.atitle=Template+literals&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FTemplate_literals&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators">"Comparison Operators - MDC Doc Center"</a>. Mozilla. 5 August 2010<span class="reference-accessdate">. Retrieved <span class="nowrap">5 March</span> 2011</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Comparison+Operators+-+MDC+Doc+Center&rft.pub=Mozilla&rft.date=2010-08-05&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen%2FJavaScript%2FReference%2FOperators%2FComparison_Operators&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://javascript.crockford.com/style2.html">"The Elements of JavaScript Style"</a>. Douglas Crockford<span class="reference-accessdate">. Retrieved <span class="nowrap">5 March</span> 2011</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=The+Elements+of+JavaScript+Style&rft.pub=Douglas+Crockford&rft_id=http%3A%2F%2Fjavascript.crockford.com%2Fstyle2.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">"Spread syntax (...) - JavaScript | MDN"</a>. <i>developer.mozilla.org</i>. 25 September 2023.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=developer.mozilla.org&rft.atitle=Spread+syntax+%28...%29+-+JavaScript+%26%23124%3B+MDN&rft.date=2023-09-25&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FOperators%2FSpread_syntax&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">"rest parameters"</a>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=rest+parameters&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FFunctions%2Frest_parameters&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.ecma-international.org/ecma-262/11.0/index.html">"ECMAScript 2020 Language Specification"</a>. <a href="/wiki/Ecma_International" title="Ecma International">Ecma International</a>. June 2020.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=ECMAScript+2020+Language+Specification&rft.pub=Ecma+International&rft.date=2020-06&rft_id=http%3A%2F%2Fwww.ecma-international.org%2Fecma-262%2F11.0%2Findex.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text">ECMA-262, Edition 3, 7.5.3 Future Reserved Words</span> </li> <li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">"await - JavaScript (MDN)"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2 May</span> 2017</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=await+-+JavaScript+%28MDN%29&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FOperators%2Fawait&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://jquery.com/upgrade-guide/3.0/#breaking-change-and-feature-jquery-deferred-is-now-promises-a-compatible">"jQuery Core 3.0 Upgrade Guide"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2 May</span> 2017</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=jQuery+Core+3.0+Upgrade+Guide&rft_id=https%3A%2F%2Fjquery.com%2Fupgrade-guide%2F3.0%2F%23breaking-change-and-feature-jquery-deferred-is-now-promises-a-compatible&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-Async/await_JavaScript-async-await-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-Async/await_JavaScript-async-await_18-0">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html">"Taming the asynchronous beast with ES7"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">12 November</span> 2015</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Taming+the+asynchronous+beast+with+ES7&rft_id=http%3A%2F%2Fpouchdb.com%2F2015%2F03%2F05%2Ftaming-the-async-beast-with-es7.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-19"><span class="mw-cite-backlink"><b><a href="#cite_ref-19">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite id="CITEREFFoundation2017" class="citation web cs1">Foundation, Node.js (30 May 2017). <a rel="nofollow" class="external text" href="https://nodejs.org/en/blog/release/v8.0.0/#improved-support-for-promises">"Node v8.0.0 (Current) - Node.js"</a>. <i>Node.js</i>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=Node.js&rft.atitle=Node+v8.0.0+%28Current%29+-+Node.js&rft.date=2017-05-30&rft.aulast=Foundation&rft.aufirst=Node.js&rft_id=https%3A%2F%2Fnodejs.org%2Fen%2Fblog%2Frelease%2Fv8.0.0%2F%23improved-support-for-promises&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> <li id="cite_note-deve_eval-20"><span class="mw-cite-backlink"><b><a href="#cite_ref-deve_eval_20-0">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Never_use_eval!">"eval()"</a>. <i>MDN Web Docs</i><span class="reference-accessdate">. Retrieved <span class="nowrap">29 January</span> 2020</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=MDN+Web+Docs&rft.atitle=eval%28%29&rft_id=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2Feval%23Never_use_eval%21&rfr_id=info%3Asid%2Fen.wikipedia.org%3AJavaScript+syntax" class="Z3988"></span></span> </li> </ol></div></div> <div class="mw-heading mw-heading2"><h2 id="Further_reading">Further reading</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=69" title="Edit section: Further reading"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li>Danny Goodman: <i>JavaScript Bible</i>, Wiley, John & Sons, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/0-7645-3342-8" title="Special:BookSources/0-7645-3342-8">0-7645-3342-8</a>.</li> <li>David Flanagan, Paula Ferguson: <i>JavaScript: The Definitive Guide</i>, O'Reilly & Associates, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/0-596-10199-6" title="Special:BookSources/0-596-10199-6">0-596-10199-6</a>.</li> <li>Thomas A. Powell, Fritz Schneider: <i>JavaScript: The Complete Reference</i>, McGraw-Hill Companies, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/0-07-219127-9" title="Special:BookSources/0-07-219127-9">0-07-219127-9</a>.</li> <li>Axel Rauschmayer: <i>Speaking JavaScript: An In-Depth Guide for Programmers</i>, 460 pages, O'Reilly Media, 25 February 2014, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/978-1449365035" title="Special:BookSources/978-1449365035">978-1449365035</a>. (<a rel="nofollow" class="external text" href="http://speakingjs.com/">free online edition</a>)</li> <li>Emily Vander Veer: <i>JavaScript For Dummies, 4th Edition</i>, Wiley, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <a href="/wiki/Special:BookSources/0-7645-7659-3" title="Special:BookSources/0-7645-7659-3">0-7645-7659-3</a>.</li></ul> <div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=JavaScript_syntax&action=edit&section=70" title="Edit section: External links"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1235681985">.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}</style><style data-mw-deduplicate="TemplateStyles:r1237033735">@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}</style><div class="side-box side-box-right plainlinks sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}</style> <div class="side-box-flex"> <div class="side-box-image"><span class="noviewer" typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/40px-Wikibooks-logo-en-noslogan.svg.png" decoding="async" width="40" height="40" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/60px-Wikibooks-logo-en-noslogan.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/80px-Wikibooks-logo-en-noslogan.svg.png 2x" data-file-width="400" data-file-height="400" /></span></span></div> <div class="side-box-text plainlist">Wikibooks has a book on the topic of: <i><b><a href="https://en.wikibooks.org/wiki/JavaScript" class="extiw" title="wikibooks:JavaScript">JavaScript</a></b></i></div></div> </div> <ul><li><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/docs/A_re-introduction_to_JavaScript">A re-introduction to JavaScript - Mozilla Developer Center</a></li> <li><a rel="nofollow" class="external text" href="https://codetopology.com/category/scripts/javascript-tutorials/">JavaScript Loops</a></li> <li>ECMAScript standard references: <a rel="nofollow" class="external text" href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262</a></li> <li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20120527095306/http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?sub=javascript&code=script">Interactive JavaScript Lessons - example-based</a></li> <li><a rel="nofollow" class="external text" href="http://javascript.about.com/">JavaScript on About.com: lessons and explanation</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20170225135310/http://javascript.about.com/">Archived</a> 25 February 2017 at the <a href="/wiki/Wayback_Machine" title="Wayback Machine">Wayback Machine</a></li> <li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20150110202627/http://wisentechnologies.com/it-courses/JavaScript-Training-in-Chennai.aspx">JavaScript Training</a></li> <li>Mozilla Developer Center Core References for JavaScript versions <a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference">1.5</a>, <a rel="nofollow" class="external text" href="https://web.archive.org/web/20070210000908/http://research.nihonsoft.org/javascript/CoreReferenceJS14/index.html">1.4</a>, <a rel="nofollow" class="external text" href="https://web.archive.org/web/20070210000504/http://research.nihonsoft.org/javascript/ClientReferenceJS13/index.html">1.3</a> and <a rel="nofollow" class="external text" href="https://web.archive.org/web/20070210000545/http://research.nihonsoft.org/javascript/jsref/index.htm">1.2</a></li> <li><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/docs/JavaScript">Mozilla JavaScript Language Documentation</a></li></ul> <div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}</style><style data-mw-deduplicate="TemplateStyles:r1236075235">.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}</style></div><div role="navigation" class="navbox" aria-labelledby="JavaScript" style="padding:3px"><table class="nowraplinks mw-collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1129693374"><style data-mw-deduplicate="TemplateStyles:r1239400231">.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}</style><div class="navbar plainlinks hlist navbar-mini"><ul><li class="nv-view"><a href="/wiki/Template:JavaScript" title="Template:JavaScript"><abbr title="View this template">v</abbr></a></li><li class="nv-talk"><a href="/wiki/Template_talk:JavaScript" title="Template talk:JavaScript"><abbr title="Discuss this template">t</abbr></a></li><li class="nv-edit"><a href="/wiki/Special:EditPage/Template:JavaScript" title="Special:EditPage/Template:JavaScript"><abbr title="Edit this template">e</abbr></a></li></ul></div><div id="JavaScript" style="font-size:114%;margin:0 4em"><a href="/wiki/JavaScript" title="JavaScript">JavaScript</a></div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Static_program_analysis" title="Static program analysis">Code analysis</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/ESLint" title="ESLint">ESLint</a></li> <li><a href="/wiki/JSHint" title="JSHint">JSHint</a></li> <li><a href="/wiki/JSLint" title="JSLint">JSLint</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Subset" title="Subset">Supersets</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/JS%2B%2B" title="JS++">JS++</a></li> <li><a href="/wiki/TypeScript" title="TypeScript">TypeScript</a></li> <li><a href="/wiki/ArkTS" title="ArkTS">ArkTS</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Source-to-source_compiler" title="Source-to-source compiler">Transpilers</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/AtScript" title="AtScript">AtScript</a></li> <li><a href="/wiki/Babel_(transcompiler)" title="Babel (transcompiler)">Babel</a></li> <li><a href="/wiki/ClojureScript" class="mw-redirect" title="ClojureScript">ClojureScript</a></li> <li><a href="/wiki/CoffeeScript" title="CoffeeScript">CoffeeScript</a></li> <li><a href="/wiki/Dart_(programming_language)" title="Dart (programming language)">Dart</a></li> <li><a href="/wiki/Elm_(programming_language)" title="Elm (programming language)">Elm</a></li> <li><a href="/wiki/Emscripten" title="Emscripten">Emscripten</a></li> <li><a href="/wiki/Google_Closure_Compiler" class="mw-redirect" title="Google Closure Compiler">Google Closure Compiler</a></li> <li><a href="/wiki/Google_Web_Toolkit" title="Google Web Toolkit">Google Web Toolkit</a></li> <li><a href="/wiki/Haxe" title="Haxe">Haxe</a></li> <li><a href="/wiki/LiveScript_(programming_language)" title="LiveScript (programming language)">LiveScript</a></li> <li><a href="/wiki/Morfik" title="Morfik">Morfik</a></li> <li><a href="/wiki/Nim_(programming_language)" title="Nim (programming language)">Nim</a></li> <li><a href="/wiki/Opa_(programming_language)" title="Opa (programming language)">Opa</a></li> <li><a href="/wiki/PureScript" title="PureScript">PureScript</a></li> <li><a href="/wiki/Reason_(programming_language)" title="Reason (programming language)">Reason</a></li> <li><a href="/wiki/WebSharper" title="WebSharper">WebSharper</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Concepts</th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/JavaScript_library" title="JavaScript library">JavaScript library</a></li> <li><a class="mw-selflink selflink">JavaScript syntax</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Debugger" title="Debugger">Debuggers</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Chrome_(browser)" class="mw-redirect" title="Chrome (browser)">Chrome DevTools</a></li> <li><a href="/wiki/Firefox" title="Firefox">Firefox Inspector</a></li> <li><a href="/wiki/Komodo_IDE" title="Komodo IDE">Komodo IDE</a></li> <li><a href="/wiki/Microsoft_Edge" title="Microsoft Edge">Microsoft Edge DevTools</a></li> <li><a href="/wiki/Opera_(web_browser)" title="Opera (web browser)">Opera DevTools</a></li> <li><a href="/wiki/Safari_(web_browser)" title="Safari (web browser)">Safari Web Inspector</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Documentation_generator" title="Documentation generator">Doc generators</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/JSDoc" title="JSDoc">JSDoc</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Editors <span style="font-weight:normal">(<a href="/wiki/Comparison_of_JavaScript-based_source_code_editors" title="Comparison of JavaScript-based source code editors">comparison</a>)</span></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Ace_(editor)" title="Ace (editor)">Ace</a> <ul><li><a href="/wiki/Cloud9_IDE" title="Cloud9 IDE">Cloud9 IDE</a></li></ul></li> <li><a href="/wiki/Atom_(text_editor)" title="Atom (text editor)">Atom</a></li> <li><a href="/wiki/CodeMirror" title="CodeMirror">CodeMirror</a> <ul><li><a href="/wiki/Brackets_(text_editor)" title="Brackets (text editor)">Brackets</a></li> <li><a href="/wiki/Light_Table_(software)" title="Light Table (software)">Light Table</a></li></ul></li> <li><a href="/wiki/PhpStorm" class="mw-redirect" title="PhpStorm">PhpStorm</a></li> <li><a href="/wiki/List_of_Eclipse_projects#Top-level_sub-projects" title="List of Eclipse projects">Orion</a></li> <li><a href="/wiki/Visual_Studio" title="Visual Studio">Visual Studio</a> <ul><li><a href="/wiki/Microsoft_Visual_Studio_Express" title="Microsoft Visual Studio Express">Visual Studio Express</a></li></ul></li> <li><a href="/wiki/Visual_Studio_Code" title="Visual Studio Code">Visual Studio Code</a></li> <li><a href="/wiki/Visual_Studio#Azure_DevOps_Services" title="Visual Studio">Visual Studio Team Services</a></li> <li><a href="/wiki/Vim_(text_editor)" title="Vim (text editor)">Vim</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/JavaScript_engine" title="JavaScript engine">Engines</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/List_of_ECMAScript_engines" title="List of ECMAScript engines">List of ECMAScript engines</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Software_framework" title="Software framework">Frameworks</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Comparison_of_JavaScript-based_web_frameworks" title="Comparison of JavaScript-based web frameworks">Comparison of JavaScript frameworks</a></li> <li><a href="/wiki/List_of_JavaScript_libraries" title="List of JavaScript libraries">List of JavaScript libraries</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Related <a href="/wiki/Technology" title="Technology">technologies</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Ajax_(programming)" title="Ajax (programming)">Ajax</a></li> <li><a href="/wiki/AssemblyScript" title="AssemblyScript">AssemblyScript</a></li> <li><a href="/wiki/Asm.js" title="Asm.js">asm.js</a></li> <li><a href="/wiki/Cascading_Style_Sheets" class="mw-redirect" title="Cascading Style Sheets">Cascading Style Sheets</a></li> <li><a href="/wiki/Document_Object_Model" title="Document Object Model">Document Object Model</a></li> <li><a href="/wiki/HTML" title="HTML">HTML</a> <ul><li><a href="/wiki/HTML5" title="HTML5">HTML5</a></li></ul></li> <li><a href="/wiki/JSON" title="JSON">JSON</a></li> <li><a href="/wiki/WebAssembly" title="WebAssembly">WebAssembly</a></li> <li><a href="/wiki/WebAuthn" title="WebAuthn">WebAuthn</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Package_manager" title="Package manager">Package managers</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Npm" title="Npm">npm</a></li> <li><a href="/wiki/Pnpm" title="Pnpm">pnpm</a></li> <li><a href="/wiki/Yarn_(package_manager)" title="Yarn (package manager)">yarn</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Module bundlers</th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Webpack" title="Webpack">Webpack</a></li> <li><a href="/wiki/Vite_(software)" title="Vite (software)">Vite</a></li> <li><a href="/wiki/Esbuild" title="Esbuild">esbuild</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Server-side_JavaScript" class="mw-redirect" title="Server-side JavaScript">Server-side</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Active_Server_Pages" title="Active Server Pages">Active Server Pages</a></li> <li><a href="/wiki/Bun_(software)" title="Bun (software)">Bun</a></li> <li><a href="/wiki/CommonJS" title="CommonJS">CommonJS</a></li> <li><a href="/wiki/Deno_(software)" title="Deno (software)">Deno</a></li> <li><a href="/wiki/JSGI" title="JSGI">JSGI</a></li> <li><a href="/wiki/Node.js" title="Node.js">Node.js</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="/wiki/Unit_testing" title="Unit testing">Unit testing</a> frameworks <span style="font-weight:normal">(<a href="/wiki/List_of_unit_testing_frameworks#JavaScript" title="List of unit testing frameworks">list</a>)</span></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Jasmine_(JavaScript_testing_framework)" class="mw-redirect" title="Jasmine (JavaScript testing framework)">Jasmine</a></li> <li><a href="/wiki/Mocha_(JavaScript_framework)" title="Mocha (JavaScript framework)">Mocha</a></li> <li><a href="/wiki/QUnit" title="QUnit">QUnit</a></li></ul> </div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">People</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em"> <ul><li><a href="/wiki/Douglas_Crockford" title="Douglas Crockford">Douglas Crockford</a></li> <li><a href="/wiki/Ryan_Dahl" title="Ryan Dahl">Ryan Dahl</a></li> <li><a href="/wiki/Brendan_Eich" title="Brendan Eich">Brendan Eich</a></li> <li><a href="/wiki/John_Resig" title="John Resig">John Resig</a></li></ul> </div></td></tr></tbody></table></div> <!-- NewPP limit report Parsed by mw‐api‐int.codfw.main‐6fdd9f9b88‐5h24j Cached time: 20241130034248 Cache expiry: 2592000 Reduced expiry: false Complications: [vary‐revision‐sha1, show‐toc] CPU time usage: 1.140 seconds Real time usage: 1.404 seconds Preprocessor visited node count: 6934/1000000 Post‐expand include size: 96019/2097152 bytes Template argument size: 9600/2097152 bytes Highest expansion depth: 14/100 Expensive parser function count: 135/500 Unstrip recursion depth: 1/20 Unstrip post‐expand size: 316326/5000000 bytes Lua time usage: 0.569/10.000 seconds Lua memory usage: 8594199/52428800 bytes Number of Wikibase entities loaded: 0/400 --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 979.648 1 -total 17.33% 169.779 1 Template:Reflist 15.99% 156.608 1 Template:Short_description 14.28% 139.915 15 Template:Cite_web 13.19% 129.197 2 Template:Excerpt 13.16% 128.964 2 Template:Pagetype 9.31% 91.201 1 Template:JavaScript 9.03% 88.416 1 Template:Navbox 8.10% 79.311 12 Template:Main_other 6.99% 68.429 1 Template:Update --> <!-- Saved in parser cache with key enwiki:pcache:idhash:3824186-0!canonical and timestamp 20241130034248 and revision id 1258701791. Rendering was triggered because: api-parse --> </div><!--esi <esi:include src="/esitest-fa8a495983347898/content" /> --><noscript><img src="https://login.wikimedia.org/wiki/Special:CentralAutoLogin/start?type=1x1&useformat=desktop" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://en.wikipedia.org/w/index.php?title=JavaScript_syntax&oldid=1258701791">https://en.wikipedia.org/w/index.php?title=JavaScript_syntax&oldid=1258701791</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/wiki/Help:Category" title="Help:Category">Categories</a>: <ul><li><a href="/wiki/Category:JavaScript" title="Category:JavaScript">JavaScript</a></li><li><a href="/wiki/Category:Programming_language_syntax" title="Category:Programming language syntax">Programming language syntax</a></li></ul></div><div id="mw-hidden-catlinks" class="mw-hidden-catlinks mw-hidden-cats-hidden">Hidden categories: <ul><li><a href="/wiki/Category:Articles_with_short_description" title="Category:Articles with short description">Articles with short description</a></li><li><a href="/wiki/Category:Short_description_is_different_from_Wikidata" title="Category:Short description is different from Wikidata">Short description is different from Wikidata</a></li><li><a href="/wiki/Category:Wikipedia_articles_in_need_of_updating_from_November_2020" title="Category:Wikipedia articles in need of updating from November 2020">Wikipedia articles in need of updating from November 2020</a></li><li><a href="/wiki/Category:All_Wikipedia_articles_in_need_of_updating" title="Category:All Wikipedia articles in need of updating">All Wikipedia articles in need of updating</a></li><li><a href="/wiki/Category:Use_dmy_dates_from_April_2022" title="Category:Use dmy dates from April 2022">Use dmy dates from April 2022</a></li><li><a href="/wiki/Category:All_articles_with_unsourced_statements" title="Category:All articles with unsourced statements">All articles with unsourced statements</a></li><li><a href="/wiki/Category:Articles_with_unsourced_statements_from_January_2021" title="Category:Articles with unsourced statements from January 2021">Articles with unsourced statements from January 2021</a></li><li><a href="/wiki/Category:Articles_with_excerpts" title="Category:Articles with excerpts">Articles with excerpts</a></li><li><a href="/wiki/Category:Webarchive_template_wayback_links" title="Category:Webarchive template wayback links">Webarchive template wayback links</a></li><li><a href="/wiki/Category:Articles_with_example_JavaScript_code" title="Category:Articles with example JavaScript code">Articles with example JavaScript code</a></li></ul></div></div> </div> </main> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" > <ul id="footer-info"> <li id="footer-info-lastmod"> This page was last edited on 21 November 2024, at 03:20<span class="anonymous-show"> (UTC)</span>.</li> <li id="footer-info-copyright">Text is available under the <a href="/wiki/Wikipedia:Text_of_the_Creative_Commons_Attribution-ShareAlike_4.0_International_License" title="Wikipedia:Text of the Creative Commons Attribution-ShareAlike 4.0 International License">Creative Commons Attribution-ShareAlike 4.0 License</a>; additional terms may apply. By using this site, you agree to the <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use" class="extiw" title="foundation:Special:MyLanguage/Policy:Terms of Use">Terms of Use</a> and <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy" class="extiw" title="foundation:Special:MyLanguage/Policy:Privacy policy">Privacy Policy</a>. Wikipedia® is a registered trademark of the <a rel="nofollow" class="external text" href="https://wikimediafoundation.org/">Wikimedia Foundation, Inc.</a>, a non-profit organization.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/wiki/Wikipedia:About">About Wikipedia</a></li> <li id="footer-places-disclaimers"><a href="/wiki/Wikipedia:General_disclaimer">Disclaimers</a></li> <li id="footer-places-contact"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact Wikipedia</a></li> <li id="footer-places-wm-codeofconduct"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct">Code of Conduct</a></li> <li id="footer-places-developers"><a href="https://developer.wikimedia.org">Developers</a></li> <li id="footer-places-statslink"><a href="https://stats.wikimedia.org/#/en.wikipedia.org">Statistics</a></li> <li id="footer-places-cookiestatement"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement">Cookie statement</a></li> <li id="footer-places-mobileview"><a href="//en.m.wikipedia.org/w/index.php?title=JavaScript_syntax&mobileaction=toggle_view_mobile" class="noprint stopMobileRedirectToggle">Mobile view</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-copyrightico"><a href="https://wikimediafoundation.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/static/images/footer/wikimedia-button.svg" width="84" height="29" alt="Wikimedia Foundation" loading="lazy"></a></li> <li id="footer-poweredbyico"><a href="https://www.mediawiki.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/w/resources/assets/poweredby_mediawiki.svg" alt="Powered by MediaWiki" width="88" height="31" loading="lazy"></a></li> </ul> </footer> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul></ul> </div><script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw-web.codfw.main-5c59558b9d-tfj4k","wgBackendResponseTime":149,"wgPageParseReport":{"limitreport":{"cputime":"1.140","walltime":"1.404","ppvisitednodes":{"value":6934,"limit":1000000},"postexpandincludesize":{"value":96019,"limit":2097152},"templateargumentsize":{"value":9600,"limit":2097152},"expansiondepth":{"value":14,"limit":100},"expensivefunctioncount":{"value":135,"limit":500},"unstrip-depth":{"value":1,"limit":20},"unstrip-size":{"value":316326,"limit":5000000},"entityaccesscount":{"value":0,"limit":400},"timingprofile":["100.00% 979.648 1 -total"," 17.33% 169.779 1 Template:Reflist"," 15.99% 156.608 1 Template:Short_description"," 14.28% 139.915 15 Template:Cite_web"," 13.19% 129.197 2 Template:Excerpt"," 13.16% 128.964 2 Template:Pagetype"," 9.31% 91.201 1 Template:JavaScript"," 9.03% 88.416 1 Template:Navbox"," 8.10% 79.311 12 Template:Main_other"," 6.99% 68.429 1 Template:Update"]},"scribunto":{"limitreport-timeusage":{"value":"0.569","limit":"10.000"},"limitreport-memusage":{"value":8594199,"limit":52428800}},"cachereport":{"origin":"mw-api-int.codfw.main-6fdd9f9b88-5h24j","timestamp":"20241130034248","ttl":2592000,"transientcontent":false}}});});</script> <script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"Article","name":"JavaScript syntax","url":"https:\/\/en.wikipedia.org\/wiki\/JavaScript_syntax","sameAs":"http:\/\/www.wikidata.org\/entity\/Q1024422","mainEntity":"http:\/\/www.wikidata.org\/entity\/Q1024422","author":{"@type":"Organization","name":"Contributors to Wikimedia projects"},"publisher":{"@type":"Organization","name":"Wikimedia Foundation, Inc.","logo":{"@type":"ImageObject","url":"https:\/\/www.wikimedia.org\/static\/images\/wmf-hor-googpub.png"}},"datePublished":"2006-01-23T23:53:16Z","dateModified":"2024-11-21T03:20:26Z","image":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/e4\/Source_code_in_Javascript.png","headline":"set of rules that define a JavaScript program"}</script> </body> </html>