{"id":2097,"date":"2021-11-22T09:48:41","date_gmt":"2021-11-22T14:48:41","guid":{"rendered":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/?p=2097"},"modified":"2021-11-29T18:41:25","modified_gmt":"2021-11-29T23:41:25","slug":"running-javascript","status":"publish","type":"post","link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/topics\/javascript\/running-javascript\/","title":{"rendered":"Running JavaScript"},"content":{"rendered":"\n<p>There are a number of different ways you can run JavaScript code. This post goes over a number of them. Some are better for simple tests and practice and some are what you would use in production (the final version of your web site).<\/p>\n\n\n\n<p>Yo<a rel=\"noreferrer noopener\" href=\"https:\/\/wesbos.com\/javascript\/01-the-basics\/browser-editor-and-terminal-setup\" target=\"_blank\">u can also look at Wes Boss&#8217; Setup instructions<\/a> and <a href=\"https:\/\/wesbos.com\/javascript\/01-the-basics\/running-and-loading-js\/\" target=\"_blank\" rel=\"noreferrer noopener\">Running and Loading JS<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Tests<\/h2>\n\n\n\n<p>These are some methods you can use to quickly try out some JavaScript. They don&#8217;t require that you create a file. That also means they don&#8217;t save the code you write.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Browser Console<\/h3>\n\n\n\n<p>All of the major browsers have a console that lets you run JavaScript. the JavaScript you run in the console will work on the current page that is open.<\/p>\n\n\n\n<details class=\"wp-block-stackable-accordion stk-block-accordion stk-inner-blocks stk-block-content stk-block stk-7a19b44\" data-block-id=\"7a19b44\">\n<summary class=\"wp-block-stackable-column stk--container-small stk-block-accordion__heading stk-block-column stk-column stk-block stk-99d8fc4\" data-block-id=\"99d8fc4\"><div class=\"stk-column-wrapper stk-block-column__content stk-container stk-99d8fc4-container stk-hover-parent\"><div class=\"stk-block-content stk-inner-blocks\">\n<div class=\"wp-block-stackable-icon-label stk-block-icon-label stk-block stk-f27f7da\" data-block-id=\"f27f7da\"><div class=\"stk-row stk-inner-blocks stk-block-content\">\n<div class=\"wp-block-stackable-heading stk-block-heading stk-block stk-d2ee3bc\" data-block-id=\"d2ee3bc\"><h4 class=\"stk-block-heading__text\">Chrome<\/h4><\/div>\n\n\n\n<div class=\"wp-block-stackable-icon stk-block-icon stk-block stk-5113bb1\" data-block-id=\"5113bb1\"><span class=\"stk--svg-wrapper\"><div class=\"stk--inner-svg\"><svg style=\"height:0;width:0\"><defs><linearGradient id=\"linear-gradient-5113bb1\" x1=\"0\" x2=\"100%\" y1=\"0\" y2=\"0\"><stop offset=\"0%\" style=\"stop-opacity:1;stop-color:var(--linear-gradient-5113-bb-1-color-1)\"><\/stop><stop offset=\"100%\" style=\"stop-opacity:1;stop-color:var(--linear-gradient-5113-bb-1-color-2)\"><\/stop><\/linearGradient><\/defs><\/svg><svg data-prefix=\"fas\" data-icon=\"chevron-down\" class=\"svg-inline--fa fa-chevron-down fa-w-14\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 448 512\" aria-hidden=\"true\" width=\"32\" height=\"32\"><path fill=\"currentColor\" d=\"M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z\"><\/path><\/svg><\/div><\/span><\/div>\n<\/div><\/div>\n<\/div><\/div><\/summary>\n\n\n\n<div class=\"wp-block-stackable-column stk-block-accordion__content stk-block-column stk-column stk-block stk-7ab745a\" data-block-id=\"7ab745a\"><div class=\"stk-column-wrapper stk-block-column__content stk-container stk-7ab745a-container stk--no-background stk--no-padding\"><div class=\"stk-block-content stk-inner-blocks\">\n<div class=\"wp-block-stackable-heading stk-block-heading stk-block stk-33067bc\" data-block-id=\"33067bc\"><h3 class=\"stk-block-heading__text\">Through Inspector<\/h3><\/div>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Right Click<\/strong> on a web page<\/li><li>Click on <strong>Inspect<\/strong><\/li><li>Click on the <strong>Console<\/strong> tab<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Through Dev Tools<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>press Ctrl+shift+j (win) or Command+option+j (mac) to open the console<\/li><\/ol>\n\n\n\n<p>Alternatively you can <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Click on the three small vertial dots on the top right of the browser<ol><li>Go to More tools<\/li><li>then Developer Tools<\/li><\/ol><\/li><li>Click the Console tab<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"How to Open JavaScript Console in Google Chrome\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/jvAAL0I9kyI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n<\/div><\/div><\/div>\n<\/details>\n\n\n\n<details class=\"wp-block-stackable-accordion stk-block-accordion stk-inner-blocks stk-block-content stk-block stk-71049d5\" data-block-id=\"71049d5\">\n<summary class=\"wp-block-stackable-column stk--container-small stk-block-accordion__heading stk-block-column stk-column stk-block stk-f1f8995\" data-block-id=\"f1f8995\"><div class=\"stk-column-wrapper stk-block-column__content stk-container stk-f1f8995-container stk-hover-parent\"><div class=\"stk-block-content stk-inner-blocks\">\n<div class=\"wp-block-stackable-icon-label stk-block-icon-label stk-block stk-9f985a3\" data-block-id=\"9f985a3\"><div class=\"stk-row stk-inner-blocks stk-block-content\">\n<div class=\"wp-block-stackable-heading stk-block-heading stk-block stk-40989bf\" data-block-id=\"40989bf\"><h4 class=\"stk-block-heading__text\">Firefox<\/h4><\/div>\n\n\n\n<div class=\"wp-block-stackable-icon stk-block-icon stk-block stk-f2361c4\" data-block-id=\"f2361c4\"><span class=\"stk--svg-wrapper\"><div class=\"stk--inner-svg\"><svg style=\"height:0;width:0\"><defs><linearGradient id=\"linear-gradient-f2361c4\" x1=\"0\" x2=\"100%\" y1=\"0\" y2=\"0\"><stop offset=\"0%\" style=\"stop-opacity:1;stop-color:var(--linear-gradient-f-2361-c-4-color-1)\"><\/stop><stop offset=\"100%\" style=\"stop-opacity:1;stop-color:var(--linear-gradient-f-2361-c-4-color-2)\"><\/stop><\/linearGradient><\/defs><\/svg><svg data-prefix=\"fas\" data-icon=\"chevron-down\" class=\"svg-inline--fa fa-chevron-down fa-w-14\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 448 512\" aria-hidden=\"true\" width=\"32\" height=\"32\"><path fill=\"currentColor\" d=\"M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z\"><\/path><\/svg><\/div><\/span><\/div>\n<\/div><\/div>\n<\/div><\/div><\/summary>\n\n\n\n<div class=\"wp-block-stackable-column stk-block-accordion__content stk-block-column stk-column stk-block stk-7651a10\" data-block-id=\"7651a10\"><div class=\"stk-column-wrapper stk-block-column__content stk-container stk-7651a10-container stk--no-background stk--no-padding\"><div class=\"stk-block-content stk-inner-blocks\">\n<div class=\"wp-block-stackable-heading stk-block-heading stk-block stk-985dd14\" data-block-id=\"985dd14\"><h3 class=\"stk-block-heading__text\">Through Inspector<\/h3><\/div>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Right Click<\/strong> on a web page<\/li><li>Click on <strong>Inspect<\/strong><\/li><li>Click on the <strong>Console<\/strong> tab<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Through Dev Tools<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>press <strong>Ctrl+shift+i (win<\/strong>) or <strong>Command+option+i (mac)<\/strong> to open the developer tools<\/li><li>Click on the <strong>Console<\/strong> tab<\/li><\/ol>\n\n\n\n<p>Alternatively you can <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Click on the hamburger menu on the top right of the browser<ol><li>Go to <strong>More Tools<\/strong><\/li><li>then <strong>WebDeveloper Tools<\/strong><\/li><\/ol><\/li><li>Click the <strong>Console<\/strong> tab<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"How to Open JavaScript Console in Google Chrome\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/jvAAL0I9kyI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n<\/div><\/div><\/div>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">Node through the Terminal<\/h3>\n\n\n\n<p>If you have Node installed then you can open the terminal and run node by itself to get at REPL (<strong>R<\/strong>ead|<strong>E<\/strong>val|<strong>P<\/strong>rint <strong>L<\/strong>oop).<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Open the Terminal<ol><li>[optional] check that you have node installed by typing: node -v<\/li><li>if you see v16.11.1 or something like that, it&#8217;s installed. If not check the <a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/dev-environment-setup\/\" data-type=\"page\" data-id=\"206\">dev setup for your OS<\/a><\/li><\/ol><\/li><li>Type just the word node: node<ol><li>You should see something like &#8220;Welcome to Node.js vXX&#8221; and a command prompt.<\/li><\/ol><\/li><li>You are in the REPL, Now you can write JavaScript code.<\/li><li>Press <strong>Control+C twice<\/strong> to exit the REPL<ol><li>or you can type: <strong>.exit<\/strong> (don&#8217;t forget the . in front of exit)<\/li><\/ol><\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Using the Node REPL - Node.js Basics [05] - Java Brains\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/FLwt4e53_6c?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">JavaScript in Files<\/h2>\n\n\n\n<p>When you write the JavaScript in a file, it&#8217;s saved. This is the method you use to actually run JavaScript on your web site.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inside an HTML File<\/h3>\n\n\n\n<p>Using the &lt;script&gt; element, you can write JavaScript in your .html file. Most of the time you should add it after the page content and right before the closing &lt;\/body&gt; tag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n  console.log(\"This is in your browser Console \");\n&lt;\/script&gt;\n&lt;\/body&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">In an External JavaScript File<\/h3>\n\n\n\n<p>You can also write your JavaScript in a separate file. This is often the preferred way as you can write one file and reuse it in any .html file that needs it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Setup<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a folder for your JavaScript if you haven&#8217;t already. The most common name for the folder is <strong>js<\/strong> (some also use javascript).<\/li><li>Create a file with .js extension. People often use <strong>main.js<\/strong> for the main JavaScript file for their site.<\/li><li>In any .html file where you want to use the JavaScript, write a &lt;script&gt; element with a src attribute that points to the file you made. For example:<ol><li><strong>&lt;script src=&#8221;\/js\/main.js&#8221;&gt;&lt;\/script&gt;<\/strong><\/li><\/ol><\/li><\/ol>\n\n\n\n<p>The <strong>main.js<\/strong> file will include things that you might need on each page like JS needed for the navigation. If you&#8217;re using a template system like Eleventy, then you can put this link in your main template.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"600\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-22-at-9.20.07-AM.png\" alt=\"\" class=\"wp-image-2103\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-22-at-9.20.07-AM.png 947w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-22-at-9.20.07-AM-300x190.png 300w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-22-at-9.20.07-AM-768x487.png 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Writing Code<\/h4>\n\n\n\n<p>After you create the file and link it to your HTML files, then you can just write JavaScript in the file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a number of different ways you can run JavaScript code. This post goes over a number of them. Some are better for simple tests and practice and some are what you would use in production (the final version of your web site). You can also look at Wes Boss&#8217; Setup instructions and Running&hellip; <a class=\"more-link\" href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/topics\/javascript\/running-javascript\/\">Continue reading <span class=\"screen-reader-text\">Running JavaScript<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","portfolio_post_id":0,"portfolio_citation":"","portfolio_annotation":"","openlab_post_visibility":"","footnotes":""},"categories":[22,20],"tags":[170,41,172,171],"coauthors":[87],"class_list":["post-2097","post","type-post","status-publish","format-standard","hentry","category-jamstack","category-javascript","tag-basics","tag-javascript","tag-js","tag-repl","entry"],"featured_image_urls_v2":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":"","post-thumbnail":"","gform-image-choice-sm":"","gform-image-choice-md":"","gform-image-choice-lg":""},"post_excerpt_stackable_v2":"<p>There are a number of different ways you can run JavaScript code. This post goes over a number of them. Some are better for simple tests and practice and some are what you would use in production (the final version of your web site). You can also look at Wes Boss&#8217; Setup instructions and Running and Loading JS. Quick Tests These are some methods you can use to quickly try out some JavaScript. They don&#8217;t require that you create a file. That also means they don&#8217;t save the code you write. Browser Console All of the major browsers have a&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/category\/topics\/jamstack\/\" rel=\"category tag\">Jamstack<\/a>, <a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/category\/topics\/javascript\/\" rel=\"category tag\">JavaScript<\/a>","author_info_v2":{"name":"Christopher Stein","url":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/author\/cstein\/"},"comments_num_v2":"1 comment","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false,"gform-image-choice-sm":false,"gform-image-choice-md":false,"gform-image-choice-lg":false},"uagb_author_info":{"display_name":"Christopher Stein","author_link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/author\/cstein\/"},"uagb_comment_info":1,"uagb_excerpt":"There are a number of different ways you can run JavaScript code. This post goes over a number of them. Some are better for simple tests and practice and some are what you would use in production (the final version of your web site). You can also look at Wes Boss&#8217; Setup instructions and Running&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":6,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/posts\/2097\/revisions"}],"predecessor-version":[{"id":2104,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/posts\/2097\/revisions\/2104"}],"wp:attachment":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/media?parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/categories?post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/tags?post=2097"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/wp-json\/wp\/v2\/coauthors?post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}