{"id":15,"date":"2019-03-14T16:28:01","date_gmt":"2019-03-14T16:28:01","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/javascript\/?p=15"},"modified":"2019-03-27T18:36:03","modified_gmt":"2019-03-27T18:36:03","slug":"01-javascript-program-syntax","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/javascript\/01-javascript-program-syntax\/","title":{"rendered":"JavaScript &#8211; Program Syntax"},"content":{"rendered":"<p>As you know, JavaScript programs are\u00a0 placed inside a web page. You place the code inside &lt;script&gt; &lt;\/script&gt; tag.<\/p>\n<p>The &lt;script&gt;&lt;\/script&gt; tag can be placed anywhere in the web page. So when the browser encounters a &lt;script&gt;&lt;\/script&gt; tag, it would then interpret the code as a JavaScript code.<\/p>\n<p>For example, the code below displays an alert to the screen.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;title&gt;<\/span> Welcome to JavaScript<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;script&gt;<\/span>\r\nalert(<span style=\"background-color: #fff0f0;\">'This your first JavaScript program'<\/span>);\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>How to Run JavaScript Program<\/strong><\/h4>\n<p>I would recomend you download NotePad++. It&#8217;s free. You use it for editing your code. Although you can use NotePad but NotePad++ have more features. <a href=\"https:\/\/notepad-plus-plus.org\/repository\/7.x\/7.6.4\/npp.7.6.4.Installer.exe\" target=\"_blank\" rel=\"noopener\">Download Notepad++ from here<\/a>.<\/p>\n<p>Open NotePad++ and enter the code.<\/p>\n<p>Now, Save it as Prog1.html.(you can also use another name). In the Save As type, choose Hypertext Markup Language. Save it in a new folder.<\/p>\n<p>Open the location you saved the file and double-click to open it.<\/p>\n<p>You will notice, it opens a web page and displays an alert<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Attribute of the Script Tag<\/strong><\/h4>\n<p>The script tag has two important attributes: Language and Type.<\/p>\n<p><strong>Language<\/strong> &#8211; The language attribute is used to specify the scripting language. In this case, it is &#8220;javascript&#8221;. However, recent versions of HTML makes this tag optional. So you can well omit it. This is as we did in the program above.<\/p>\n<p><strong>Type:<\/strong> Again this attribute is optional. Your program would still work without it. However, this is the attribute recommended at this time. The value is set to &#8220;text\/javascript&#8221;<\/p>\n<p>So if we rewrite our program, we would have:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">language=<\/span><span style=\"background-color: #fff0f0;\">\"javascript\"<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n\r\nalert(<span style=\"background-color: #fff0f0;\">'This your first JavaScript program'<\/span>);\r\n\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using document.write()<\/strong><\/p>\n<p>The document.write() function is used to write a text to the html document. This is the text the user will see in the html page. The code below uses document.write(). Create a new html file with the following content:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;title&gt;<\/span>Your Second Program<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>   \r\n   <span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">language =<\/span> <span style=\"background-color: #fff0f0;\">\"javascript\"<\/span> <span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Welcome to JavaScript. Enjoy!\"<\/span>)\r\n   <span style=\"color: #007700;\">&lt;\/script&gt;<\/span>      \r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Comments in JavaScript<\/strong><\/h4>\n<p>There are different syntax for comment in JavaScript.<\/p>\n<p>You can enclose comments using \/* and *\/. This can also be multiple line comment<\/p>\n<p>Single-line comment can be specified using \/\/<\/p>\n<p>HTML comment begins with &lt;!&#8211; (JavaScript as well as single-line comment)<\/p>\n<p>The HTML comment closing sequence is &#8211;&gt;. However it is not recognized by JavaScript. So it is written as \/\/&#8211;&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">language =<\/span> <span style=\"background-color: #fff0f0;\">\"javascript\"<\/span> <span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n   <span style=\"color: #888888;\">&lt;!--<\/span>\r\n      <span style=\"color: #888888;\">\/\/ This is a single-line comment<\/span>\r\n   \r\n      <span style=\"color: #888888;\">\/*<\/span>\r\n<span style=\"color: #888888;\">      * This is a multi-line comment in JavaScript<\/span>\r\n<span style=\"color: #888888;\">      *\/<\/span>\r\n   <span style=\"color: #888888;\">\/\/--&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>I recommend you try this out.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Other Things to Note<\/strong><\/h4>\n<p>Note the following about JavaScript:<\/p>\n<p><strong>Case Sensitivity<\/strong><\/p>\n<p>JavaScript is case-sensitive. This is just like the Java programming language.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Semicolons<\/strong><\/p>\n<p>Although you use semicolons at the end of a line, you can well not use them. They are optional.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Whitespace<\/strong><\/p>\n<p>Spaces, taps and newlines that appear on a JavaScript program are ignored.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you know, JavaScript programs are\u00a0 placed inside a web page. You place the code inside &lt;script&gt; &lt;\/script&gt; tag. The &lt;script&gt;&lt;\/script&gt; tag can be placed &hellip; <\/p>\n","protected":false},"author":395,"featured_media":16,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/15\/revisions\/19"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media\/16"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}