{"id":4035,"date":"2023-03-02T18:29:20","date_gmt":"2023-03-02T23:29:20","guid":{"rendered":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/?p=4035"},"modified":"2023-03-02T18:29:20","modified_gmt":"2023-03-02T23:29:20","slug":"5a-javascript-variables","status":"publish","type":"post","link":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/2023\/03\/02\/5a-javascript-variables\/","title":{"rendered":"5a: JavaScript Variables"},"content":{"rendered":"<p><span style=\"font-size: 8pt\"><span style=\"font-weight: 400\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-4036 alignright\" src=\"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/files\/2023\/03\/variable-300x98.png\" alt=\"\" width=\"300\" height=\"98\" srcset=\"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/files\/2023\/03\/variable-300x98.png 300w, https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/files\/2023\/03\/variable.png 403w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>A variable in JavaScript is a way to name the storage of a piece of data, so that the data can be referred to throughout the code by its variable name as opposed to reentering the data every time. Declaring a variable is indicated by <\/span><i><span style=\"font-weight: 400\">var<\/span><\/i><span style=\"font-weight: 400\"> or <\/span><i><span style=\"font-weight: 400\">let<\/span><\/i><span style=\"font-weight: 400\"> followed by what you wish to name the data, and to assign a value you would add an equals sign followed by the value. If you want to return a string, the value must be in single or double quotation marks (ex: <\/span><i><span style=\"font-weight: 400\">let <\/span><\/i><span style=\"font-weight: 400\">myText = \u201cToday is March 2nd\u201d.) There are rules as to what a variable can be named in JS: it can only start with a letter, an underscore, or a $. Though they can\u2019t start with a number, a variable name can have numbers in it after the first character. It\u2019s important to remember that JavaScript is case-sensitive, so myVariable1 &amp; MyVariable1 would represent different pieces of data.<\/span><\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">There are a few ways to display outputs:<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">console.log() to test code on the console<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">alert() to create a popup<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">document.write() to write directly on the webpage<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">Concatenation is putting two strings together and can be done with a + or += :<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">Example with + \u2192\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">firstName = \u201cSteven\u201d, lastName = \u201cSclarow\u201d<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">fullName = lastName + \u201c,\u201d + firstName<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">fullName= \u201cSclarow, Steven\u201d<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">Example with += \u2192\u00a0<\/span><\/p>\n<p><span style=\"font-size: 8pt\"><i><span style=\"font-weight: 400\">let<\/span><\/i><span style=\"font-weight: 400\"> a = 10;<\/span><\/span><\/p>\n<p><span style=\"font-size: 8pt\"><i><span style=\"font-weight: 400\">let <\/span><\/i><span style=\"font-weight: 400\">b = \u201chello\u201d;<\/span><\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">console.log( a += 5 ); \/\/ this is addition<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">\/\/ Expected output: 15<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">console.log( b +=\u00a0 \u201cworld\u201d ); \/\/ this is concatenation<\/span><\/p>\n<p><span style=\"font-weight: 400;font-size: 8pt\">\/\/ Expected output: &#8220;hello world&#8221;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A variable in JavaScript is a way to name the storage of a piece of data, so that the data can be referred to throughout the code by its variable name as opposed to reentering the data every time. Declaring a variable is indicated by var or let followed by what you wish to name [&hellip;]<\/p>\n","protected":false},"author":29253,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[663943],"tags":[],"class_list":{"0":"post-4035","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-instructor","7":"entry"},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/posts\/4035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/users\/29253"}],"replies":[{"embeddable":true,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/comments?post=4035"}],"version-history":[{"count":1,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/posts\/4035\/revisions"}],"predecessor-version":[{"id":4037,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/posts\/4035\/revisions\/4037"}],"wp:attachment":[{"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/media?parent=4035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/categories?post=4035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/community.mis.temple.edu\/mis2101sec730spring2023\/wp-json\/wp\/v2\/tags?post=4035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}