{"id":17639,"date":"2025-02-08T13:00:15","date_gmt":"2025-02-08T11:00:15","guid":{"rendered":"https:\/\/belvg.com\/blog\/?p=17639"},"modified":"2026-05-13T15:40:40","modified_gmt":"2026-05-13T13:40:40","slug":"4-way-to-add-javascript-to-magento-2","status":"publish","type":"post","link":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html","title":{"rendered":"Four Ways to Add JS in Magento 2 in 2026"},"content":{"rendered":"\n<p>In this article, we are going to discuss how you can connect JavaScript in Magento 2. Check it out and get a clear understanding of this process. In our step-by-step guide we will consider four ways of connecting as well as look through their advantages and disadvantages.<\/p>\n<p><!--more--><\/p>\n<p><a href=\"#step_1\">Via &lt;script type=&rdquo;text\/javascript&gt;&lt;\/script&gt;<br>\n<\/a><a href=\"#step_2\">In Magento style<br>\n<\/a><a href=\"#step_3\">Imperative approach<br>\n<\/a><a href=\"#step_4\">Via require-config.js file<\/a><\/p>\n<h2 id=\"step_1\">Via &lt;script type=&rdquo;text\/javascript&gt;&lt;\/script&gt;<\/h2>\n<p>Using <code>&lt;script type=\"text\/javascript&gt;&lt;\/script&gt;<\/code>&nbsp;is considered to be the easiest, but quite unreliable way. We are assured that its one and only advantage is simplicity. Following this method can be justified if you want to connect third-party libraries. In other cases, this approach has several significant drawbacks.<\/p>\n<p>First of all, the code will be difficult to use again and it can block the page loading (of course, it depends on what the script contains). Secondly, it is obvious that as the complexity of the project increases, it becomes very difficult to control such a code. When discussing this method we should also mention CSP (Content-Security-Policy). The third issue is that the code connected in this way is highly vulnerable to CSS attacks (Cross-site scripting). If you still want to use this method, then you should add the following attributes when connecting: deferred=&rdquo;true&rdquo;, async=&rdquo;true&rdquo;.<\/p>\n<div class=\"content_sales_info\" style=\"background-image: url(https:\/\/belvg.com\/blog\/wp-content\/uploads\/2019\/03\/Sales_background.png\"><img width=\"65\" height=\"65\" src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2015\/11\/Andrey_Dubina.png\" alt=\"Andrey_Dubina\" title=\"Four Ways to Add JS in Magento 2 in 2026\"><div class=\"content_part\"><h3 class=\"title\">Partner With Us <\/h3><span class=\"sales_content_text\">Let's discuss how to grow your business. Get a Free Quote.<\/span><\/div> <a class=\"call-to-action\" href=\"https:\/\/belvg.com\/contact?utm_source=blog&amp;utm_campaign=sales_block&amp;utm_content=middle\" target=\"_blank\" rel=\"noopener\">Talk to Andrey<\/a><\/div>\n<h2 id=\"step_2\">In Magento style<\/h2>\n<p>Now, let&rsquo;s look through the ways to connect JS scripts in Magento style. First of all, it&rsquo;s worth mentioning that Magento 2 uses RequireJS scripts to connect JS. This approach allows using JS modularly as well as background and asynchronous loading. To learn more aboutRequireJS, follow the <a href=\"https:\/\/requirejs.org\/\" target=\"_blank\" rel=\"noopener\">link<\/a>.<\/p>\n<p>So, the first way is to use the special <b>data-mage-init <\/b>attribute . The attribute must contain the path to the JS module as a value, the element which we want to apply our JS code to and configuration parameters.<\/p>\n<p>For example, imagine that we have a RequireJS module that calls a popup window. Let&rsquo;s create a JS file in the following directory:<\/p>\n<p><strong>app\/code\/Example\/JavascriptInitExample\/view\/frontend\/web\/example.js<\/strong><\/p>\n<p>Next, we will write the following code inside the file:<\/p>\n<pre class=\"lang:default decode:true \">define([], function(){\r\n    alert(\"A simple Example module\");  \r\n});<\/pre>\n<p>Now, let&rsquo;s say there is a certain template in which we want to connect our JS module. For the beginners, we can connect it the following way:<\/p>\n<pre class=\"lang:default decode:true \">&lt;div class=\"example-element\"  data-mage-init='{\"Example_JavascriptInitExample\/example\": {}}'&gt;A single div&lt;\/div&gt;\r\n<\/pre>\n<p>When loading a page, the browser will simply show a pop-up window with the specified text. Nothing special, but what if we pass some configuration values to data-mage-init? For example, let&rsquo;s add this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div class=\"example-element\"  data-mage-init='{\"Example_JavascriptInitExample\/example\": {\"config\": \"value\"}}'&gt;A single div&lt;\/div&gt;<\/pre>\n<p>And then we change the JS module in the following way:<\/p>\n<pre class=\"lang:default decode:true \">define([], function(config){\r\n    alert(\"A simple Example module\");\r\n    console.log(config);   \r\n});<\/pre>\n<p>When the page is reloaded, we will get the same pop-up window, but if we take a look at the console, we will see that the value of the config parameter equals the value. Following the same steps, we can pass any parameters inside our JS module. In addition to that, we can pass a DOM element as a parameter, on which we call our script. In order to do it, we rewrite our module in the following way:<\/p>\n<pre class=\"lang:default decode:true \">define([], function(config, element){\r\n    alert(\"A simple Example module\");\r\n    console.log(config);\r\n    console.log(element);   \r\n});\r\n<\/pre>\n<p>When the page is reloaded, the output of the element will be added to the console. In our case, this will be<\/p>\n<pre class=\"lang:default decode:true \">&lt;div class=\"example-element\"&gt;&lt;\/div&gt;<\/pre>\n<p>We can also come across a situation, when we pass a group of arguments to our block using the <strong>&lt;arguments&gt;&lt;\/arguments&gt;<\/strong> tag in the layout. If we want to get these arguments as parameters for our JS module, then we need to pass them in the following way:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div class=\"example-element\"  data-mage-init='{\"Example_JavascriptInitExample\/example\": \r\n&lt;?= \/* @escapeNotVerified *\/ $block-&gt;getJsLayout() ?&gt;'&gt;A single div&lt;\/div&gt;\r\n<\/pre>\n<p>We get JSON as an input parameter, which we can further use at our discretion.<\/p>\n<p>Often we have to pass more than one argument to our module. The syntax that was described above can be difficult to read, so <b>type=&rdquo;text\/x-magento-init&rdquo;<\/b> can be used as an alternative. It looks like this:<\/p>\n<pre class=\"lang:default decode:true \">&lt;script type=\"text\/x-magento-init\"&gt;\r\n    {\r\n        \".example-element\": {\r\n            \"Example_JavascriptInitExample\/example\":{\r\n\"config\":\"value\",\r\n\"config2\":\"value2\",\r\n\"config3\": \"optionConfig\": &lt;?php \/* @noEscape *\/ echo $block-&gt;getJsonConfig()?&gt;\r\n\r\n   }          \r\n        }\r\n    }        \r\n&lt;\/script&gt;<\/pre>\n<p>As you can see, this code is much easier to read.<\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-custom-development\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><img src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2019\/01\/magento-custom-development.png\" width=\"120\" height=\"120\" alt=\"magento custom development\" class=\"module_image\" title=\"Four Ways to Add JS in Magento 2 in 2026\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Custom Development<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento Custom Development <\/p><a href=\"https:\/\/belvg.com\/magento-custom-development\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Visit the page<\/span><\/a><\/div><\/div><\/div>\n<h2 id=\"step_3\">The imperative approach<\/h2>\n<p>It is also worth to mention here the so-called imperative approach in connecting JS modules. You can often find it in the Magento 2 code. However, it is recommended in the Magento 2 official documentation not to use it. Here is what the connection looks like:<\/p>\n<pre class=\"lang:default decode:true\">&lt;script type=\"text\/javascript\"&gt;\r\nrequire( [\r\n       'jquery'\r\n   ],\r\n   function($) {\r\n       \talert('Example');\r\n       });\r\n   }\r\n);\r\n&lt;\/script&gt;\r\n<\/pre>\n<h2 id=\"step_4\">Via&nbsp;require-config.js file<\/h2>\n<p>Finally, let&rsquo;s consider the situation when we need to connect the script not at the same page and in a specific element, but on multiple pages at once. In this case, you can use the <b>require-config.js<\/b> file, which needs to be created in the module folder in the <strong>view\/base\/require-config.js<\/strong> sub-directory. The content should look like this:<\/p>\n<pre class=\"lang:default decode:true \">var config = {\r\ndeps: [\r\n'Example_JavascriptInitExample\/js\/example',\r\n'AnotherExample_JavascriptInitExample\/js\/example',\r\n]\r\n};\r\n<\/pre>\n<p>Then, the scripts we have connected will work on all the pages of the module. Usually, some third-party libraries are connected in this way. You can also set the map property and define the prefixes for various modules. In the future, we can use these prefixes to connect the required JS modules in other modules. For example:<\/p>\n<pre class=\"lang:default decode:true \">var config = {\r\nmap: {\r\n\t\" * \" : {\r\n\t\t\"example_alias\": \"example.js\"\r\n}\r\n}\r\n}\r\n<\/pre>\n<p>Now, we can use the created alias to connect the corresponding JS module:<\/p>\n<pre class=\"lang:default decode:true \">define([\r\n\t\"jquery\",\r\n\t\"example_alias\"\r\n], function($, config){\r\n\t\/* code goes here *\/    \r\n});\r\n<\/pre>\n<p>If you need to determine the loading order of JS files, you can use the shim option.<\/p>\n<p>Now let&rsquo;s consider injecting dynamic content and executing JavaScript in&nbsp;<strong>data-mage-init<\/strong> and <strong>x-magento-init<\/strong>.<\/p>\n<p>In order to do it, you can just insert the content in <strong>DOM<\/strong> and after that trigger the event &lsquo;<strong>contentUpdated<\/strong>&rsquo;. For example, it can look like this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;code&gt;\r\n$.ajax({\r\n    url: 'https:\/\/www.you-url.com',\r\n    method: 'GET'\r\n    success: function (response) {\r\n        $('#element').html(data)\r\n                     .trigger('contentUpdated')\r\n    }\r\n});\r\n\r\n&lt;\/code&gt;\r\n<\/pre>\n<h2>Wrapping it up<\/h2>\n<p>Magento 2 offers multiple ways to connect JavaScript, each with its own advantages and trade-offs. Simple &lt;script&gt; tags are quick but unreliable and prone to security issues. Magento-style connections using data-mage-init or x-magento-init provide modular, configurable, and maintainable solutions. The imperative approach exists but is discouraged, and require-config.js is ideal for scripts that must load globally across pages.<\/p>\n<p>Yet, there is one potential issue when connecting JavaScript. It&rsquo;s common knowledge that adding JS increases page loading time, which can negatively affect your webstore. What can you do in this case?&nbsp;<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">We would strongly recommend you to try&nbsp;<a href=\"https:\/\/store.belvg.com\/defer-js-for-magento-2-0.html\" target=\"_blank\" rel=\"noopener\">Defer JS for the Magento 2<\/a>&nbsp;module, which moves JavaScript to the bottom of the page and speeds up loading.<\/span>&nbsp;With this module, you will be able to install JavaScript, keeping your webstore fast.<\/p>\n<p>We hope that everything was clear, but if you have any questions, leave them below. Thanks for reading!<\/p>\n\n","protected":false},"excerpt":{"rendered":"In this article, we are going to discuss how you can connect JavaScript in Magento &#8230;","protected":false},"author":47,"featured_media":29207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"footnotes":""},"categories":[491,389,473,524],"tags":[],"class_list":["post-17639","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-magento-2","category-magento2-development","category-magento-front-end-developer","category-magento-speed-optimization","post_format-post-format-image"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Find out about the 4 possible ways to add JavaScript to Magento 2 with examples. We also consider which one is the best to use for different goals.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Artsem Miklashevich\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#article\",\"name\":\"Magento 2 and JavaScript: How To Add It in 2026 | BelVG Blog\",\"headline\":\"Four Ways to Add JS in Magento 2 in 2026\",\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/artsem-miklashevich#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Four-Ways-to-Add-JS-in-Magento-2.webp\",\"width\":1108,\"height\":590},\"datePublished\":\"2025-02-08T13:00:15+02:00\",\"dateModified\":\"2026-05-13T15:40:40+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#webpage\"},\"articleSection\":\"Magento 2, Magento 2 Development, Magento Front End Developer, Magento Speed Optimization, Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"name\":\"Magento 2 Development\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"position\":2,\"name\":\"Magento 2 Development\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#listItem\",\"name\":\"Four Ways to Add JS in Magento 2 in 2026\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#listItem\",\"position\":3,\"name\":\"Four Ways to Add JS in Magento 2 in 2026\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"name\":\"Magento 2 Development\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/artsem-miklashevich#author\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/artsem-miklashevich\",\"name\":\"Artsem Miklashevich\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/145dc6a225439ebb3a257da8fd0f46383a3649cdca2e58190b33a28af5f216a5?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Artsem Miklashevich\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#webpage\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html\",\"name\":\"Magento 2 and JavaScript: How To Add It in 2026 | BelVG Blog\",\"description\":\"Find out about the 4 possible ways to add JavaScript to Magento 2 with examples. We also consider which one is the best to use for different goals.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/artsem-miklashevich#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/artsem-miklashevich#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Four-Ways-to-Add-JS-in-Magento-2.webp\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html\\\/#mainImage\",\"width\":1108,\"height\":590},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/4-way-to-add-javascript-to-magento-2.html#mainImage\"},\"datePublished\":\"2025-02-08T13:00:15+02:00\",\"dateModified\":\"2026-05-13T15:40:40+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Magento 2 and JavaScript: How To Add It in 2026 | BelVG Blog","description":"Find out about the 4 possible ways to add JavaScript to Magento 2 with examples. We also consider which one is the best to use for different goals.","canonical_url":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#article","name":"Magento 2 and JavaScript: How To Add It in 2026 | BelVG Blog","headline":"Four Ways to Add JS in Magento 2 in 2026","author":{"@id":"https:\/\/belvg.com\/blog\/author\/artsem-miklashevich#author"},"publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2020\/04\/Four-Ways-to-Add-JS-in-Magento-2.webp","width":1108,"height":590},"datePublished":"2025-02-08T13:00:15+02:00","dateModified":"2026-05-13T15:40:40+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#webpage"},"isPartOf":{"@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#webpage"},"articleSection":"Magento 2, Magento 2 Development, Magento Front End Developer, Magento Speed Optimization, Image"},{"@type":"BreadcrumbList","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/belvg.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","name":"Magento 2 Development"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","position":2,"name":"Magento 2 Development","item":"https:\/\/belvg.com\/blog\/category\/magento2-development","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#listItem","name":"Four Ways to Add JS in Magento 2 in 2026"},"previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#listItem","position":3,"name":"Four Ways to Add JS in Magento 2 in 2026","previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","name":"Magento 2 Development"}}]},{"@type":"Organization","@id":"https:\/\/belvg.com\/blog\/#organization","name":"BelVG Blog","description":"Magento and Prestashop articles","url":"https:\/\/belvg.com\/blog\/"},{"@type":"Person","@id":"https:\/\/belvg.com\/blog\/author\/artsem-miklashevich#author","url":"https:\/\/belvg.com\/blog\/author\/artsem-miklashevich","name":"Artsem Miklashevich","image":{"@type":"ImageObject","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/145dc6a225439ebb3a257da8fd0f46383a3649cdca2e58190b33a28af5f216a5?s=96&d=mm&r=g","width":96,"height":96,"caption":"Artsem Miklashevich"}},{"@type":"WebPage","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#webpage","url":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html","name":"Magento 2 and JavaScript: How To Add It in 2026 | BelVG Blog","description":"Find out about the 4 possible ways to add JavaScript to Magento 2 with examples. We also consider which one is the best to use for different goals.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/belvg.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#breadcrumblist"},"author":{"@id":"https:\/\/belvg.com\/blog\/author\/artsem-miklashevich#author"},"creator":{"@id":"https:\/\/belvg.com\/blog\/author\/artsem-miklashevich#author"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2020\/04\/Four-Ways-to-Add-JS-in-Magento-2.webp","@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html\/#mainImage","width":1108,"height":590},"primaryImageOfPage":{"@id":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html#mainImage"},"datePublished":"2025-02-08T13:00:15+02:00","dateModified":"2026-05-13T15:40:40+02:00"},{"@type":"WebSite","@id":"https:\/\/belvg.com\/blog\/#website","url":"https:\/\/belvg.com\/blog\/","name":"BelVG Blog","description":"Magento and Prestashop articles","inLanguage":"en-US","publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"17639","title":"Magento 2 and JavaScript: How To Add It in 2026 | #site_title","description":"Find out about the 4 possible ways to add JavaScript to Magento 2 with examples. We also consider which one is the best to use for different goals.","keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-11-03 18:36:59","updated":"2026-11-03 18:36:59"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/category\/magento2-development\" title=\"Magento 2 Development\">Magento 2 Development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tFour Ways to Add JS in Magento 2 in 2026\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/belvg.com\/blog\/"},{"label":"Magento 2 Development","link":"https:\/\/belvg.com\/blog\/category\/magento2-development"},{"label":"Four Ways to Add JS in Magento 2 in 2026","link":"https:\/\/belvg.com\/blog\/4-way-to-add-javascript-to-magento-2.html"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/comments?post=17639"}],"version-history":[{"count":21,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17639\/revisions"}],"predecessor-version":[{"id":30300,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17639\/revisions\/30300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media\/29207"}],"wp:attachment":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media?parent=17639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/categories?post=17639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/tags?post=17639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}