{"id":207,"date":"2019-12-27T03:11:00","date_gmt":"2019-12-27T03:11:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:52","modified_gmt":"2024-09-13T15:04:52","slug":"javascript-tutorial-series-variables","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/","title":{"rendered":"JavaScript Tutorial Series &#8211; Variables"},"content":{"rendered":"<p>In JavaScript, variable names must start with a letter or underscore, are case sensitive and multiple words should be camel cased, example: aVariableName. Variable names may only contain letters, numbers and underscores.<\/p>\n<p>JavaScript determines what type a variable is. There are primitive and complex types. Valid primitive types include string, number, boolean, null, symbol and undefined. Variables are defined as either var, let or const. Variables defined as var are hoisted to the top of the defining function. Variables defined using let have block scope, so they are only visible within the enclosing scope, such as within a for loop or if statement. Variables defined using const also have block scope and can not be reassigned a new value. They are constant. Since let was introduced, using var isn&#8217;t really necessary. Using let also protects from unintentionally re-declaring a variable with the same name. For example, the code below using var will not throw an error:<\/p>\n<pre><code>\nvar diameter = 11;\ndiameter += 5;\nconsole.log(diameter);\nvar diameter = 12;\nconsole.log(diameter);\n<\/code><\/pre>\n<p>However using let will throw an error when declaring a variable with the same name as an already declared variable with that name.<\/p>\n<pre><code>\nlet diameter = 11;\ndiameter += 5;\nconsole.log(diameter);\nlet diameter = 12;\nconsole.log(diameter);\n<\/code><\/pre>\n<p><\/p>\n<pre><code>\nlet diameter = 13;&nbsp; \/\/ A number\nlet radius = 6.5;&nbsp; &nbsp;\/\/ A number\nlet shape = \"Circle\"; \/\/ A string\nlet large = false; \/\/ A boolean\nlet small = x; \/\/ Undefined\n<\/code><\/pre>\n<p>Semi-colons at the end of the declaration are optional. I prefer to use them as they give a clear indication of the end of a statement.<\/p>\n<p>Assigning a variable a different value with a different type than it&#8217;s previous definition, changes the variable&#8217;s type.<\/p>\n<pre><code>\nradius = '6';&nbsp; \/\/ Radius is now a string\n<\/code><\/pre>\n<p>\nThe type of a variable can be found by:<\/p>\n<pre><code>\ntypeof radius;\n<\/code><\/pre>\n<p>\nA variable may be displayed on the screen by using the built in console function:<\/p>\n<pre><code>\nconsole.log(typeof radius);\n<\/code><\/pre>\n<p>\nTo convert a variable of one type to another type, cast the variable. Examples:<\/p>\n<pre><code>\nlet str_cast = diameter.toString();\nlet int_cast = parseInt(shape);\nlet float_cast = parseFloat(diameter);\n<\/code><\/pre>\n<p>\nNumber will also cast a string to a number, example;<\/p>\n<pre><code>\nlet num_cast = Number(diameter);\n<\/code><\/pre>\n<p>\nComplex variable types are function and object. Object is returned for null, arrays and objects, but not functions. Functions and objects will be discussed in later lessons.<\/p>\n<p><strong>Implementation:<\/strong><\/p>\n<p>Inside the javascript_tutorial.js file created in the <a href=\"http:\/\/www.zofxare.com\/blog\/home\/2019\/12\/14\/javascript-tutorial-series-set-up\/\" rel=\"noopener\" target=\"_blank\">JavaScript set up tutorial<\/a>, add the following lines that are in bold:<\/p>\n<pre>\/\/ JavaScript Tutorial\n\n\/*\n* This file will add JavaScript\n* to a website.\n*\/\n\n<strong>(function() {\n&nbsp; let doc = document;\n})();\n<\/strong>\n<\/pre>\n<p>The JavaScript tutorial series starts with <a href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/javascript-tutorial-series-set-up.html\/\" rel=\"noopener noreferrer\" target=\"_blank\">this post<\/a>.<\/p>\n<p>\n(paid links)<br \/>\n<a href=\"https:\/\/amzn.to\/39aSKtV\" rel=\"noopener\" target=\"_blank\">More JavaScript<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=144934013X&amp;asins=144934013X&amp;linkId=1c564951ff9e32885fd78e6d3e51cd88&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1119366445&amp;asins=1119366445&amp;linkId=dd23c2d87c50139ea87f57684a12c407&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=0596517742&amp;asins=0596517742&amp;linkId=6d3fc10c6f7129dfd9a699a17a1caa01&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1497408180&amp;asins=1497408180&amp;linkId=13300f4284c721f7e5b0da78795da9eb&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, variable names must start with a letter or underscore, are case sensitive and multiple words should be camel cased, example: aVariableName. Variable names may only contain letters, numbers and underscores. JavaScript determines what type a variable is. There are primitive and complex types. Valid primitive types include string, number, boolean, null, symbol and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340,339,341],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-javascript","category-javascript-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Tutorial Series - Variables - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Tutorial Series - Variables - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"In JavaScript, variable names must start with a letter or underscore, are case sensitive and multiple words should be camel cased, example: aVariableName. Variable names may only contain letters, numbers and underscores. JavaScript determines what type a variable is. There are primitive and complex types. Valid primitive types include string, number, boolean, null, symbol and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-27T03:11:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:52+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"JavaScript Tutorial Series &#8211; Variables\",\"datePublished\":\"2019-12-27T03:11:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/\"},\"wordCount\":340,\"commentCount\":0,\"articleSection\":[\"javascript\",\"javascript tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/\",\"name\":\"JavaScript Tutorial Series - Variables - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-27T03:11:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-tutorial-series-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Tutorial Series &#8211; Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Tutorial Series - Variables - Zofxare Blog Home","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Tutorial Series - Variables - Zofxare Blog Home","og_description":"In JavaScript, variable names must start with a letter or underscore, are case sensitive and multiple words should be camel cased, example: aVariableName. Variable names may only contain letters, numbers and underscores. JavaScript determines what type a variable is. There are primitive and complex types. Valid primitive types include string, number, boolean, null, symbol and [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-27T03:11:00+00:00","article_modified_time":"2024-09-13T15:04:52+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"JavaScript Tutorial Series &#8211; Variables","datePublished":"2019-12-27T03:11:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/"},"wordCount":340,"commentCount":0,"articleSection":["javascript","javascript tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/","name":"JavaScript Tutorial Series - Variables - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-27T03:11:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial Series &#8211; Variables"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":2,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/207\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}