{"id":272,"date":"2011-12-06T22:37:58","date_gmt":"2011-12-06T22:37:58","guid":{"rendered":"http:\/\/javascript-tutor.net\/index.php\/"},"modified":"2017-11-18T05:39:59","modified_gmt":"2017-11-18T05:39:59","slug":"lesson-4-variables-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-4-variables-javascript\/","title":{"rendered":"JavaScript Lesson 4: Variables in JavaScript"},"content":{"rendered":"<h4 align=\"center\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-3-javascript-dialog-box\/\">&lt;Lesson 3&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-5-arithmetic-operations-javascript\/\">&lt;Lesson 5&gt;<\/a><\/h4>\n<h3><strong>4.1 What is a Variable in JavaScript?<\/strong><\/h3>\n<p>A variable is something that holds values or information. The values can be numbers (numeric values), text (strings) or some other kinds of information. These values can vary from time to time, that is why the name variable. It is similar to the post boxes in the post office, the contents of the post boxes can change but the post boxes still have the same name or label. In order to identify the variables, we have to name them. There are a few rules regarding variable names in JavaScript, as shown below:<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nThere are no hard and fast rules that you must obey in choosing a variable name,\u00a0but it is best that you choose a name that is meaningful as it makes debugging your program easier.For example, you can use Tel_No for the telephone number, which is better than just using a variable x.<\/p>\n<h3>4.2 Assigning Values to Variables<\/h3>\n<p>After you have created a variable, you can assign a value to it. The value can be in the form of a number( numeric value), text (String) or boolean (true or false).<\/p>\n<p>The basic\u00a0 structure of value assignment in JavaScript is<br \/>\nvar variable_name= value<\/p>\n<h4><strong>Example 4.2(a): Assigning a numeric value<\/strong><\/h4>\n<pre style=\"font-size: 110%; width: 80%;\">var mark = 20\r\nvar sell_price=100\r\nvar age = 40\r\nvar height = 180\r\nvar temp = 30\r\n<\/pre>\n<h4><strong>Example 4.2(b) Assigning a String<\/strong><\/h4>\n<pre style=\"font-size: 110%; width: 80%;\">var First_Name=\"George\"\r\nvar car_model = \" Laser 100\"\r\nvar Company_Name = ' Ultra Mobile LLC '\r\n<\/pre>\n<p>Note: The string must be enclosed in quotation marks(either double or single)<\/p>\n<h4><strong>Example 4.2(c) Assigning a Boolean value<\/strong><\/h4>\n<pre style=\"font-size: 110%; width: 80%;\">var paid = true\r\nvar alive = false\r\nvar positive = true\r\n<\/pre>\n<h3><strong>4.3 Application<\/strong><\/h3>\n<p>Let&#8217;s apply what have learned about values assignment in the following example:<\/p>\n<h4><b>Example 4.3(a)<\/b><\/h4>\n<pre style=\"font-size: 110%;\">&lt;script\u00a0language=\"JavaScript\"&gt;\r\nvar Author_Name  =  \"John Brandon\",\r\n Author_ID\u00a0 =  \"A1234\",\r\n ISBN  =  \"0-7789-0234-6\",\r\n Royalty\u00a0 =  100000;\r\n\r\ndocument.writeln(\"Author Name:\"+Author_Name+\"&lt;BR&gt;\");\r\ndocument.writeln(\" Author ID:\"+Author_ID+\"&lt;BR&gt;\");\r\ndocument.writeln(\"ISBN:\"+ISBN+\"&lt;BR&gt;\");\r\ndocument.writeln(\"Royalty Payment: $\"+Royalty);\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Click\u00a0<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example4.3(a).htm\">Example4.3(a)<\/a> to view the output.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"2916349441\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 align=\"center\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-3-javascript-dialog-box\/\">&lt;Lesson 3&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-5-arithmetic-operations-javascript\/\">&lt;Lesson 5&gt;<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Lesson 3&gt; [Contents] &lt;Lesson 5&gt; 4.1 What is a Variable in JavaScript? A variable is something that holds values or information. The values can be numbers (numeric values), text (strings) or some other kinds of information. These values can vary from time to time, that is why the name variable. It is similar to the &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-4-variables-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 4: Variables in JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-272","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Variables<\/title>\n<meta name=\"description\" content=\"Learn how to manipulate variables in JavaScript\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Variables\" \/>\n<meta property=\"og:description\" content=\"Learn how to manipulate variables in JavaScript\" \/>\n<meta property=\"og:url\" content=\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-18T05:39:59+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-4-variables-javascript\/\",\"url\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html\",\"name\":\"JavaScript Variables\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2011-12-06T22:37:58+00:00\",\"dateModified\":\"2017-11-18T05:39:59+00:00\",\"description\":\"Learn how to manipulate variables in JavaScript\",\"breadcrumb\":{\"@id\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 4: Variables in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Variables","description":"Learn how to manipulate variables in JavaScript","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":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html","og_locale":"en_US","og_type":"article","og_title":"JavaScript Variables","og_description":"Learn how to manipulate variables in JavaScript","og_url":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-18T05:39:59+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-4-variables-javascript\/","url":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html","name":"JavaScript Variables","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2011-12-06T22:37:58+00:00","dateModified":"2017-11-18T05:39:59+00:00","description":"Learn how to manipulate variables in JavaScript","breadcrumb":{"@id":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson4.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 4: Variables in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":32,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/272\/revisions"}],"predecessor-version":[{"id":2262,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/272\/revisions\/2262"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}