{"id":226659,"date":"2019-10-16T11:00:34","date_gmt":"2019-10-16T18:00:34","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=226659"},"modified":"2019-10-11T13:46:27","modified_gmt":"2019-10-11T20:46:27","slug":"debug-javascript-in-microsoft-edge-from-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/debug-javascript-in-microsoft-edge-from-visual-studio\/","title":{"rendered":"Debug JavaScript in Microsoft Edge from Visual Studio"},"content":{"rendered":"<p>As you may know, the next version of Microsoft Edge will adopt the Chromium open source project to create better web compatibility and less fragmentation of different underlying web platforms. <strong>If you haven\u2019t already, you can try out preview builds of Microsoft Edge from <\/strong><a href=\"https:\/\/www.microsoftedgeinsider.com\/\"><strong>https:\/\/www.microsoftedgeinsider.com<\/strong><\/a><strong> which is now available on Windows 10, 8.1, 8, 7, and macOS!<\/strong><\/p>\n<p>With Visual Studio today, you can already debug JavaScript running in the current version of Microsoft Edge, built on top of the EdgeHTML web platform. Starting with Visual Studio 2019 version 16.2, we\u2019ve extended support to the preview builds of Microsoft Edge, which leverage Chromium. <strong>Head to <a href=\"http:\/\/visualstudio.com\/downloads\">visualstudio.com\/downloads\/<\/a> to download the latest Visual Studio now!<\/strong><\/p>\n<p><figure id=\"attachment_226739\" aria-labelledby=\"figcaption_attachment_226739\" class=\"wp-caption aligncenter\" ><img decoding=\"async\" class=\"wp-image-226739 size-large\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/featured_image-1024x310.png\" alt=\"Visual Studio hearts Microsoft Edge\" width=\"640\" height=\"194\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/featured_image-1024x310.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/featured_image-300x91.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/featured_image-768x232.png 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"figcaption_attachment_226739\" class=\"wp-caption-text\">Visual Studio now supports debugging JavaScript running in Microsoft Edge!<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<h2>Create a new ASP.NET Core Web Application<\/h2>\n<p>You can now debug JavaScript in Microsoft Edge for your ASP.NET Framework and ASP.NET Core applications. To try out this feature, let\u2019s start by creating a new ASP.NET Core Web Application.<\/p>\n<p><figure id=\"attachment_226741\" aria-labelledby=\"figcaption_attachment_226741\" class=\"wp-caption aligncenter\" ><img decoding=\"async\" class=\"wp-image-226741 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/0.png\" alt=\"Screenshot of the &quot;Create a new project&quot; dialog in Visual Studio\" width=\"1792\" height=\"1243\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/0.png 1792w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/0-300x208.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/0-768x533.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/0-1024x710.png 1024w\" sizes=\"(max-width: 1792px) 100vw, 1792px\" \/><figcaption id=\"figcaption_attachment_226741\" class=\"wp-caption-text\">Create a new project in Visual Studio and select ASP.NET Core Web Application.<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<p>To show off support for debugging JavaScript, we\u2019ll use the React.js template which shows you how to integrate React.js with an ASP.NET Core application. Once your project has been created, open <strong>ClientApp\/src\/App.js<\/strong> which you\u2019ll see is a React component for our app.<\/p>\n<p>&nbsp;<\/p>\n<h2>Using JavaScript to calculate the Fibonacci sequence<\/h2>\n<p>Let\u2019s assume that as part of this app, a user will input the term in the Fibonacci sequence they want to know and our client-side JavaScript code will be responsible for calculating it and displaying the result to the user. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting with 0 and 1.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 54px;\">\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Position<\/strong><\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">1<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">2<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">3<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">4<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">5<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">6<\/td>\n<td style=\"width: 6.25%; height: 27px; text-align: left;\">7<\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Value<\/strong><\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">0<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">1<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">1<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">2<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">3<\/td>\n<td style=\"width: 12.5%; height: 27px; text-align: left;\">5<\/td>\n<td style=\"width: 6.25%; height: 27px; text-align: left;\">8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>To handle this calculation, let\u2019s create a new Fibonacci component and add it to our app. Start by modifying <strong>App.js<\/strong> to import our soon-to-be-created Fibonacci component and route to it:<\/p>\n<pre class=\"lang:js decode:true\" title=\"Modifying app.js\">import React, { Component } from 'react';\r\nimport { Route } from 'react-router';\r\nimport { Layout } from '.\/components\/Layout';\r\nimport { Home } from '.\/components\/Home';\r\nimport { FetchData } from '.\/components\/FetchData';\r\nimport { Counter } from '.\/components\/Counter';\r\nimport { Fibonacci } from '.\/components\/Fibonacci';\r\n\r\nexport default class App extends Component {\r\n  displayName = App.name\r\n\r\n  render() {\r\n    return (\r\n      &lt;Layout&gt;\r\n        &lt;Route exact path='\/' component={Home} \/&gt;\r\n        &lt;Route path='\/counter' component={Counter} \/&gt;\r\n        &lt;Route path='\/fetchdata' component={FetchData} \/&gt;\r\n        &lt;Route path='\/fibonacci' component={Fibonacci} \/&gt;\r\n      &lt;\/Layout&gt;\r\n    );\r\n  }\r\n}\r\n<\/pre>\n<p>Now that our app will handle routing the <code>\/fibonacci<\/code> endpoint, let\u2019s modify the <strong>NavMenu<\/strong> to navigate to that endpoint. Open <strong>ClientApp\/src\/components\/NavMenu.js <\/strong>and add this <strong>LinkContainer<\/strong> component at line 36:<\/p>\n<pre class=\"lang:js decode:true\">&lt;LinkContainer to={'\/fibonacci'}&gt;\r\n  &lt;NavItem&gt;\r\n    &lt;Glyphicon glyph='alert' \/&gt; Fibonacci\r\n  &lt;\/NavItem&gt;\r\n&lt;\/LinkContainer&gt;<\/pre>\n<p>before the <code>&lt;\/Nav&gt;<\/code> tag. Now, you\u2019ll be able to easily test our new <code>\/fibonacci<\/code> route from the <strong>NavMenu<\/strong>. Finally, let\u2019s create our Fibonacci component. Create a new JavaScript file (<strong>Ctrl<\/strong>+<strong>N<\/strong>) and save it as <strong>Fibonacci.js<\/strong> in the <strong>ClientApp\/src\/components\/<\/strong> folder. Add the code below to your new component:<\/p>\n<pre class=\"lang:js decode:true\">import React, { Component } from 'react';\r\n\r\nexport class Fibonacci extends Component {\r\n  displayName = Fibonacci.name\r\n\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {\r\n      n: 8,\r\n      f_n: null,\r\n    };\r\n    this.calculateFibonacci = this.calculateFibonacci.bind(this);\r\n  }\r\n\r\n  calculateFibonacci() {\r\n    var f_0 = 0;\r\n    var f_1 = 1;\r\n    for (var i = 3; i &lt; this.state.n; i++) {\r\n      var f_2 = f_0 + f_1;\r\n      f_0 = f_1;\r\n      f_1 = f_2;\r\n    };\r\n    this.setState({\r\n      f_n: f_2\r\n    })\r\n    console.log(\"The \" + i.toString() + \"th Fibonnaci number is:\", f_2);\r\n  }\r\n\t\t\r\n  render() {\r\n    return (\r\n      &lt;div&gt;\r\n        &lt;h1&gt;Fibonacci&lt;\/h1&gt;\r\n        &lt;p&gt;This is a simple example of a React component.&lt;\/p&gt;\r\n        &lt;p&gt;The {this.state.n}th Fibonacci number is: &lt;strong&gt;{this.state.f_n}&lt;\/strong&gt;&lt;\/p&gt;\r\n        &lt;button onClick={this.calculateFibonacci}&gt;Calculate&lt;\/button&gt;\r\n      &lt;\/div&gt;\r\n    );\r\n  }\r\n}\r\n<\/pre>\n<p>Eventually, we\u2019ll add a form to the render function for the user to supply <code>n<\/code>, the variable we\u2019re using to represent the term in the Fibonacci sequence that they want to know. For now, just to test our logic, we\u2019ll assume that the user wants to know what the 8<sup>th<\/sup> term in the sequence is, which is 13. Let\u2019s build our app in the new Microsoft Edge to see if our code is calculating the right answer.<\/p>\n<p>If you don\u2019t have it installed already, head to <a href=\"https:\/\/www.microsoftedgeinsider.com\/\">https:\/\/www.microsoftedgeinsider.com<\/a> to download the preview builds of Microsoft Edge. In Visual Studio, click the dropdown next to <strong>IIS Express<\/strong> and select the version of Microsoft Edge (Beta, Dev, or Canary) that you have installed. If you don\u2019t see Microsoft Edge Beta, Dev or Canary in the dropdown, you may need to restart Visual Studio.<\/p>\n<p><figure id=\"attachment_226745\" aria-labelledby=\"figcaption_attachment_226745\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226745\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/1-1.png\" alt=\"Screenshot of Visual Studio, selecting the Microsoft Edge Dev browser to launch\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/1-1.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/1-1-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/1-1-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/1-1-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226745\" class=\"wp-caption-text\">Select Microsoft Edge Beta, Dev, or Canary for Visual Studio to launch<\/figcaption><\/figure><\/p>\n<p>Now click the green <strong>Play<\/strong> button or press <strong>F5<\/strong> on your keyboard. Visual Studio will start your web application and Microsoft Edge will automatically launch and navigate to your app.<\/p>\n<p><figure id=\"attachment_226746\" aria-labelledby=\"figcaption_attachment_226746\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-226746 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/2.png\" alt=\"Screenshot of the app in Microsoft Edge\" width=\"2256\" height=\"1434\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/2.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/2-300x191.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/2-768x488.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/2-1024x651.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226746\" class=\"wp-caption-text\">Your app is successfully running in Microsoft Edge!<\/figcaption><\/figure><\/p>\n<p>You\u2019ll see the entry we added for our Fibonacci component in the <strong>NavMenu<\/strong> on the left. Click on <strong>Fibonacci<\/strong>.<\/p>\n<p><figure id=\"attachment_226747\" aria-labelledby=\"figcaption_attachment_226747\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226747\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/3.png\" alt=\"A screenshot of the Fibonacci component we added in Microsoft Edge\" width=\"2256\" height=\"1434\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/3.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/3-300x191.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/3-768x488.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/3-1024x651.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226747\" class=\"wp-caption-text\">Microsoft Edge is showing the Fibonacci component we added<\/figcaption><\/figure><\/p>\n<p>Now click the <strong>Calculate<\/strong> button.<\/p>\n<p><figure id=\"attachment_226748\" aria-labelledby=\"figcaption_attachment_226748\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226748\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/4.png\" alt=\"Screenshot of Microsoft Edge where our Fibonacci component calculated the 8th term in the sequence as 8\" width=\"2256\" height=\"1434\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/4.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/4-300x191.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/4-768x488.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/4-1024x651.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226748\" class=\"wp-caption-text\">Our Fibonacci component is calculating the 8th term in the sequence as 8<\/figcaption><\/figure><\/p>\n<p>We know that the 8<sup>th<\/sup> term should be 13 but our code is saying that the 8<sup>th<\/sup> Fibonacci number is 8!<\/p>\n<h2>Debugging JavaScript in Visual Studio<\/h2>\n<p>Switching back to Visual Studio, since our <strong>calculateFibonacci()<\/strong> function prints to the Console, you can actually see that log in the Debug Output.<\/p>\n<p><figure id=\"attachment_226749\" aria-labelledby=\"figcaption_attachment_226749\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226749\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/5.png\" alt=\"Screenshot of Visual Studio with our console.log statement shown in the Debug Output\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/5.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/5-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/5-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/5-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226749\" class=\"wp-caption-text\">You can see console.log statements from your client-side JavaScript code in Visual Studio&#8217;s Debug Output<\/figcaption><\/figure><\/p>\n<p>To figure out where our code is going wrong, let\u2019s set a breakpoint on Line 19 inside the for loop in Visual Studio. We\u2019ll start by checking if our code is calculating the 3<sup>rd<\/sup> and 4<sup>th<\/sup> terms in the Fibonacci sequence correctly. Click the <strong>Restart<\/strong> button next to the Stop button or press <strong>Ctrl<\/strong>+<strong>Shift<\/strong>+<strong>F5<\/strong> to bind the breakpoint and start debugging.<\/p>\n<p><strong>Note<\/strong>: If you have not enabled JavaScript debugging before, your JavaScript breakpoint will not bind successfully. Visual Studio will ask if you want to enable JavaScript debugging and then restart the debugging process and bind your breakpoint. Click <strong>Enable JavaScript Debugging (Debugging Will Stop and Restart)<\/strong>.<\/p>\n<p><figure id=\"attachment_226751\" aria-labelledby=\"figcaption_attachment_226751\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226751\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/6.png\" alt=\"Screenshot of Visual Studio asking if you want to enable JavaScript debugging\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/6.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/6-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/6-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/6-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226751\" class=\"wp-caption-text\">Click &#8220;Enable JavaScript Debugging&#8221; and Visual Studio will restart debugging<\/figcaption><\/figure><\/p>\n<p>We know that the first two terms in the Fibonacci sequence are 0 and 1. The third term should also be 1. Switch from the <strong>Output<\/strong> view to <strong>Watch 1<\/strong> and add <code>f_2<\/code>, <code>f_1<\/code>, and <code>f_0<\/code> to watch. This is what Visual Studio should look like now:<\/p>\n<p><figure id=\"attachment_226753\" aria-labelledby=\"figcaption_attachment_226753\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226753\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/7.png\" alt=\"Screenshot of Visual Studio with a breakpoint set on Line 19 in Fibonacci.js and f_0, f_1, f_2 added to Watch 1\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/7.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/7-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/7-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/7-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226753\" class=\"wp-caption-text\">Visual Studio is paused at Line 19 in Fibonacci.js and you can see the values of f_0, f_1, and f_2 in Watch 1<\/figcaption><\/figure><\/p>\n<p>Click the <strong>Step Over<\/strong> button or press <strong>F10<\/strong>. You will now see that our code correctly calculated the third Fibonacci number, 1, as the value of f_2.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 54px;\">\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Position<\/strong><\/td>\n<td style=\"width: 7.08545%; height: 27px; text-align: left;\">3<\/td>\n<td style=\"width: 17.9146%; height: 27px; text-align: left;\">4<\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Value<\/strong><\/td>\n<td style=\"width: 7.08545%; height: 27px; text-align: left;\">1<\/td>\n<td style=\"width: 17.9146%; height: 27px; text-align: left;\">2<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.5%; text-align: left;\"><strong>Did our function compute this term correctly?<\/strong><\/td>\n<td style=\"width: 7.08545%; text-align: left;\"><img decoding=\"async\" class=\"wp-image-226704\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png\" alt=\"Checkmark\" width=\"87\" height=\"87\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png 384w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-150x150.png 150w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-300x300.png 300w\" sizes=\"(max-width: 87px) 100vw, 87px\" \/><\/td>\n<td style=\"width: 17.9146%; text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s keep stepping to see if there\u2019s a bug somewhere else in the loop. <strong>Step Over<\/strong> two more times and you should see both <code>f_0<\/code> and <code>f_1<\/code> are now equal to 1, which they need to be to calculate the 4<sup>th<\/sup> term in the sequence.<\/p>\n<p>You will now see that our code is paused at Line 18. Let\u2019s add <code>i<\/code> to our watch as it will tell us which term we\u2019re computing in the Fibonacci sequence. <strong>Step Over<\/strong> one more time and you\u2019ll see that the value of <code>i<\/code> is now 4. Now the code is checking to see if the value of <code>i<\/code> is less than <code>n<\/code>, the variable we\u2019re using to represent which term in the Fibonacci sequence we\u2019re trying to find. In this example, we\u2019ve hardcoded <code>n<\/code> as 8 since we\u2019re trying to calculate the 8<sup>th<\/sup> term in the sequence. Since 4 &lt; 8, <strong>step over<\/strong> again and we\u2019ll continue looping.<\/p>\n<p><strong>Step Over<\/strong> now and you should see that <code>f_2<\/code> is now 2, and since <code>i<\/code> is 4, we know that our code has successfully computed the 4<sup>th<\/sup> term in the Fibonacci sequence as 2.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 54px;\">\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Position<\/strong><\/td>\n<td style=\"width: 7.08545%; height: 27px; text-align: left;\">3<\/td>\n<td style=\"width: 17.9146%; height: 27px; text-align: left;\">4<\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"width: 12.5%; height: 27px; text-align: left;\"><strong>Value<\/strong><\/td>\n<td style=\"width: 7.08545%; height: 27px; text-align: left;\">1<\/td>\n<td style=\"width: 17.9146%; height: 27px; text-align: left;\">2<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.5%; text-align: left;\"><strong>Did our function compute this term correctly?<\/strong><\/td>\n<td style=\"width: 7.08545%; text-align: left;\"><img decoding=\"async\" class=\"wp-image-226704\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png\" alt=\"Checkmark\" width=\"87\" height=\"87\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png 384w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-150x150.png 150w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-300x300.png 300w\" sizes=\"(max-width: 87px) 100vw, 87px\" \/><\/td>\n<td style=\"width: 17.9146%; text-align: left;\"><img decoding=\"async\" class=\"wp-image-226704\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png\" alt=\"Checkmark\" width=\"87\" height=\"87\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5.png 384w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-150x150.png 150w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/checkmark-5-300x300.png 300w\" sizes=\"(max-width: 87px) 100vw, 87px\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We could keep stepping over and over again until we find the problem but since we\u2019ve already proven that we\u2019re calculating the 3<sup>rd<\/sup> and 4<sup>th<\/sup> terms in the Fibonnaci sequence correctly, let\u2019s jump ahead to the 7<sup>th<\/sup> term since it\u2019s the last term we calculate before we see the bug.<\/p>\n<h2>Using a conditional breakpoint to jump ahead in the for loop<\/h2>\n<p>Stop debugging for now by clicking the <strong>Stop<\/strong> button or pressing <strong>Shift<\/strong>+<strong>F5<\/strong>. Right click your breakpoint and select <strong>Conditions\u2026<\/strong> or press <strong>Alt<\/strong>+<strong>F9<\/strong>. This will allow us to set a condition for our breakpoint and we\u2019ll only break when that condition is true.<\/p>\n<p><figure id=\"attachment_226755\" aria-labelledby=\"figcaption_attachment_226755\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-226755 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/8.png\" alt=\"Screenshot of Visual Studio, adding a condition to the breakpoint you set on Line 19\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/8.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/8-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/8-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/8-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226755\" class=\"wp-caption-text\">You can add a condition to a breakpoint and that means you will only break execution when that condition is true.<\/figcaption><\/figure><\/p>\n<p>Enter <code>i == 7<\/code> as the condition we want to break on, which means we\u2019ll only break in the last loop before we see the bug. Start your web app again by clicking the green <strong>Play<\/strong> button or pressing <strong>F5<\/strong>. This time, we\u2019ll break only when <code>i<\/code> is 7. Here\u2019s what Visual Studio looks like now:<\/p>\n<p><figure id=\"attachment_226757\" aria-labelledby=\"figcaption_attachment_226757\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226757\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/9.png\" alt=\"A screenshot of Visual Studio, breaking on line 19 in Fibonacci.js when i is 7\" width=\"2256\" height=\"1446\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/9.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/9-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/9-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/9-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226757\" class=\"wp-caption-text\">With the conditional breakpoint, we only break when i is 7<\/figcaption><\/figure><\/p>\n<p><strong>Step over<\/strong> once and you will see that we\u2019ve calculated the 7<sup>th<\/sup> term in the Fibonacci sequence correctly since <code>f_2<\/code> is equal to 8.<\/p>\n<p><strong>Step over<\/strong> three times and we\u2019ll now be paused at <code>i &lt; n<\/code>. Since <code>i<\/code> is now 8, <code>i &lt; n<\/code> actually evaluates to false which means we\u2019re going to break out of the loop. We\u2019ve found the bug: we aren\u2019t going through the loop to calculate the 8<sup>th<\/sup> Fibonacci number!<\/p>\n<p>We can fix this bug by changing the <strong>calculateFibonacci()<\/strong> function to:<\/p>\n<pre class=\"lang:js decode:true\">calculateFibonacci() {\r\n  var f_0 = 0;\r\n  var f_1 = 1;\r\n  for (var i = 3; i &lt;= this.state.n; i++) {\r\n    var f_2 = f_0 + f_1;\r\n    f_0 = f_1;\r\n    f_1 = f_2;\r\n  };\r\n  this.setState({\r\n    f_n: f_2\r\n  })\r\n  console.log(\"The \" + (i - 1).toString() + \"th Fibonnaci number is:\", f_2);\r\n}<\/pre>\n<p>Now when <code>i<\/code> is 8, we\u2019ll actually go through the for loop since since 8 &lt;= 8. Remove the breakpoint, click the <strong>Restart<\/strong> button next to the Stop button or press <strong>Ctrl<\/strong>+<strong>Shift<\/strong>+<strong>F5<\/strong>. Click on <strong>Fibonacci<\/strong> in the <strong>NavMenu<\/strong> and click on the <strong>Calculate<\/strong> button to see that we\u2019ve correctly calculated the 8<sup>th<\/sup> term in the Fibonacci sequence as 13! We did it!<\/p>\n<p><figure id=\"attachment_226758\" aria-labelledby=\"figcaption_attachment_226758\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"size-full wp-image-226758\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/10.png\" alt=\"A screenshot of your app in Microsoft Edge, correctly calculating the 8th Fibonacci term as 13\" width=\"2256\" height=\"1434\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/10.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/10-300x191.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/10-768x488.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/10-1024x651.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><figcaption id=\"figcaption_attachment_226758\" class=\"wp-caption-text\">Your app in Microsoft Edge correctly calculated the 8th Fibonacci term as 13!<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<h2>Attaching to Microsoft Edge<\/h2>\n<p>So far in this post, we\u2019ve been using the green <strong>Play<\/strong> button in Visual Studio to build our web application, launch Microsoft Edge, and automatically have Edge navigate to our app. Starting in Visual Studio 2019 version 16.3 Preview 3, you can now attach the Visual Studio debugger to an already running instance of Microsoft Edge.<\/p>\n<p>First, ensure that there are no running instances of Microsoft Edge. Now, from your terminal, run the following command:<\/p>\n<p>start msedge &#8211;remote-debugging-port=9222<\/p>\n<p>From Visual Studio, open the <strong>Debug<\/strong> menu and select <strong>Attach to Process<\/strong> or press <strong>Ctrl<\/strong>+<strong>Alt<\/strong>+<strong>P<\/strong>.<\/p>\n<p><img decoding=\"async\" width=\"2256\" height=\"1446\" class=\"wp-image-226727\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-description-automatica-11.png\" alt=\"A screenshot of a computer Description automatically generated\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-description-automatica-11.png 2256w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-description-automatica-11-300x192.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-description-automatica-11-768x492.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-description-automatica-11-1024x656.png 1024w\" sizes=\"(max-width: 2256px) 100vw, 2256px\" \/><\/p>\n<p>From the <strong>Attach to Process<\/strong> dialog, set <strong>Connection type <\/strong>to <strong>Chrome devtools protocol websocket (no authentication)<\/strong>. In the <strong>Connecting target<\/strong> textbox, type in <a href=\"http:\/\/localhost:9222\/\">http:\/\/localhost:9222\/<\/a> and press <strong>Enter<\/strong>. You should see the list of open tabs you have in Microsoft Edge listed out in the <strong>Attach to Process<\/strong> dialog.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-226730\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-screenshot-of-a-computer-screen-description-aut-19.png\" alt=\"A screenshot of a computer screen Description automatically generated\" \/><\/p>\n<p>Click <strong>Select\u2026<\/strong> and check <strong>JavaScript (Microsoft Edge &#8211; Chromium)<\/strong>. You can add tabs, navigate to new tabs, and close tabs and see those changes reflected in the <strong>Attach to Process<\/strong> dialog by clicking the <strong>Refresh<\/strong> button. Select the tab you want to debug and click <strong>Attach<\/strong>.<\/p>\n<p>The Visual Studio debugger is now attached to Microsoft Edge! You can pause execution of JavaScript, set breakpoints, and see <code>console.log()<\/code> statements directly in the Debug Output window in Visual Studio.<\/p>\n<h2>Conclusion<\/h2>\n<p>To recap:<\/p>\n<ul>\n<li>We created an ASP.NET Core web application in Visual Studio 2019 version 16.2 and built it in a preview build of Microsoft Edge<\/li>\n<li>We added a new component to our web application that contained a bug<\/li>\n<li>We found the bug by setting breakpoints and debugging our web app running in Microsoft Edge from Visual Studio!<\/li>\n<li>We showed you how to attach the Visual Studio debugger to an existing instance of Microsoft Edge<\/li>\n<\/ul>\n<p>We\u2019re eager to learn more about how you work with JavaScript in Visual Studio! Please send us feedback by clicking the Feedback icon in Visual Studio or by tweeting <a href=\"https:\/\/twitter.com\/intent\/tweet?text=@VisualStudio+@EdgeDevTools\">@VisualStudio and @EdgeDevTools<\/a>.<\/p>\n<p><img decoding=\"async\" width=\"1432\" height=\"112\" class=\"wp-image-226733\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-close-up-of-a-screen-description-automatically-3.png\" alt=\"A close up of a screen Description automatically generated\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-close-up-of-a-screen-description-automatically-3.png 1432w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-close-up-of-a-screen-description-automatically-3-300x23.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-close-up-of-a-screen-description-automatically-3-768x60.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/10\/a-close-up-of-a-screen-description-automatically-3-1024x80.png 1024w\" sizes=\"(max-width: 1432px) 100vw, 1432px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can now debug JavaScript running in the next version of Microsoft Edge, built with Chromium, directly from Visual Studio! In this post, we walk through creating a new ASP.NET Core Web Application using React.js and show you how to set breakpoints and debug your client-side JavaScript code running in Edge with Visual Studio.<\/p>\n","protected":false},"author":8383,"featured_media":226739,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[561,155,1029],"tags":[85,9,124,475],"class_list":["post-226659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source","category-visual-studio","category-web","tag-asp-net","tag-debug","tag-javascript","tag-visual-studio-2019"],"acf":[],"blog_post_summary":"<p>You can now debug JavaScript running in the next version of Microsoft Edge, built with Chromium, directly from Visual Studio! In this post, we walk through creating a new ASP.NET Core Web Application using React.js and show you how to set breakpoints and debug your client-side JavaScript code running in Edge with Visual Studio.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/226659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/8383"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=226659"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/226659\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/226739"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=226659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=226659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=226659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}