Online Marketing

Debugging JavaScript – Chrome DevTools 101

Debugging than you need to this tutorial shows you how to make the most of chrome dev tools so that you can debug your JavaScript as quickly as possible. I’r going to use this buggy demo here to demonstrate all of the debugging tools and dip tools. I recommend that you open up this demo yourself and follow along with each step.

The link is in the description. Whatever issue you’re debugging, you always want to start by reproducing the issue. You want to find a series of actions that consistently reproduces the book in this demo. When I add five and one the result down here at the bottom is 51. Obviously that should be 6. This is the bug that I need to check down at this point. You might be tempted to use console.Log to infer where the code is going wrong.

Sure it may get the job done, but it’s rather inefficient. First, you have to find the relevant code which could take a while on a big code base. Then you have to sprinkle constant log statements throughout the code. Then you got to reload the page and look at the logs, but maybe the logs didn’t give you all the information you need, so you got to go back and add more logs and so on with dev tools.

You can pause the code in the middle of its execution, inspect the variables that are in scope at that moment in time and walk through your code, one line at a time open, dev tools by pressing command option, J on Mac or ctrl shift J on Windows And Linux, then click the sources tab. The sources panel is where you debug JavaScript. It has three main parts at the top left. Here is the file navigator pane, where you can inspect the files that the page uses after clicking on a file.

You can see the contents of it here in the code editor pin and down here is the JavaScript debugging pane, which you’ll learn more about shortly. Note that when your dev tools window is wide, the JavaScript debugging pane moves to the right. If you take a step back and think about how the app works, it’s probably safe to guess that the incorrect sum gets computed in the click event listener that’s associated to the button.

Therefore, you want to pause code right when the eventlistener executes event.Listen their breakpoints. Let you do exactly that. Expand the eventlistener breakpoint section then expand the mouse category then check the click check, box, dev tools now pauses on the first line of any click event listener that executes back in the demo, click the button again and dev tools. Pauses on the first line of this on-click function, the blue highlight indicates what line of code you’re currently paused on, if you’re paused on a different line of code press resume script execution until you’re paused on the correct line you paused on the wrong line, because you Have a Chrome extension which registers click event listeners on every page that you visit.

If you try the workflow I’m describing in an incognito window, you can see that you always pause on the correct line of code event. Listener, breakpoints are just one type of a breakpoint. Dev tools offers many other types. For example, you can set a breakpoint on a specific line of code or when a Dom node changes or when an exception gets thrown and so on. After this tutorial, I recommend checking out our breakpoints guide, which teaches you when and how to use each type.

The link to that guide is in the description too, I’m paused in the click listener, and now I want to execute the code one line at a time. The code stepping control is right here. Let you do that click. The step into next function call button to investigate a function further. For example, when I step into the inputs our empty function, it jumps me to the first line of that function when I’m confident that a function is working as expected, I can click the step over next function call button the function executes, but I don’t walk through It line by line, for example, if I click to step into the next function, call this line here would be highlighted, blue meaning.

This is where I’m currently paused, but when I click step over next function, call the function executes to completion and I pause them. The next line of the function that I’m currently stepping through last suppose, I’m stepping through a function, and I realize it’s not relevant to my bug. In that case, I can press step out of current function and dev tools executes the rest of the function. If this doesn’t make complete sense right now, I recommend creating a snippet, which is a little block of JavaScript code, that you can execute at any time set a breakpoint and your snippet and play around with the code stepping controls yourself in order to understand how they All work back in the script, I can tell that the bug is probably somewhere in the update label function rather than stepping through a bunch of irrelevant code.

I can set a line of code breakpoint right around where the bug probably occurs to do that. I just click the line number next to the code. This blue icon indicates that there’s a breakpoint on this line when I press resume script, execution dip tools runs all the code up until that lime and then pauses before that line executes over here in the JavaScript debugging pane. I can see the call stack that caused this code to execute.

I can click on the functions to see where each one got caught. The scope section shows me all the local and global variables that are currently defined at this moment in time. I can click on values to edit them, but I’m not going to do that right now. However, when I look at these values, they look suspicious they’re, wrapped in double quotes, which means that their strings. This could be the cause of the book.

I’r going to investigate this further over in the read expressions section here. I can read the values of variables over time. You can store any valid JavaScript expression here, for example, I click, Add expression, then type type of sum then press enter, and I can now see the value of that expression, as I suspected sum is a string when it should be an integer. Now I’m going to open up the console to test out a potential fix for the book when I’m paused on a line of code, the console has access to all the variables that are currently in scope.

For example, when I evaluate addend 1, it returns 5. I think I can fix this bug by converting the addend 1 and adding two variables two integers before adding them. So let me try that now yep that fixes it. I can verify that this change fixes the bug by editing the code directly from dev tools. First I’ll resume script. Execution then make my change, save the change. I press command s on Mac or ctrl s on Windows and Linux.

Then I click deactivate breakpoints so that I can test out the app without triggering the breakpoints I’ve set. Now the app sums up numbers as expected, all right, you now have an excellent foundation and how to effectively debug JavaScript using dev tools from here. I recommend learning about all the breakpoints that dev tools has to offer. As I mentioned before, we also have a JavaScript debugging reference where you can learn about literally every feature related to debugging in dev tools.

Links to both are in the description thanks for reading and happy bug, hunting


Online Marketing

Developer tools for designers – Designing in the Browser

Today, we’re going to dive into the tools that browsers give us to quite literally design in them will be using chrome, dev tools, a material design as our baseline to see where we can adjust and play. Let’s dive in like design tools, developer tools, help designers and developers build test and debug in the browser.

Luckily, there are a ton of great tools and plugins that make this process of prototyping iterating so much fun. There are tools from everything from color selection to finessing animation, to ensuring a nice user experience on a variety of devices and even testing load at various network speeds. Dev tools help us not only to see under the hood but to also make changes and decisions and see what those decisions look like in the browser, the medium in which we’re delivering the final product.

That’s why the series is so important as designers. We have to work in this medium and use it to our advantage to really have the most control and power over our designs and what’s the first tool that we need inspect element. This is the primary entry of dev tools. Inspect element is often how I open up dev tools just by left-clicking, an element and finding that inspect item in the drop down, inspect element lets you select an elements and get information inside of it.

This information includes the Cascade of styles, styles on various States, computed values, classes, shape and size and more you can even change the text content in order, the Dom of the element within the elements panel. You do so like this, so here I’m going to left-click on this heading and hit inspect, and now I’m opening up this information here – and this is the elements panel you can see. The sizing here is 350 pixels by 32 pixels and Heights.

We have the class name here of headlines, 6 and all of the class styles inside of that. You can see here that this Moz OSX font smoothing, is not being applied in favor of the WebKit font smoothing. So it shows you which styles are being applied and which ones are being applied. We also can see here this h1, so we’re getting styles from the h1 as well as the clasp on top of it, and it shows you which styles are being overridden.

For example, this font size of 2 M is being overridden in favor of a font size of 1.25 bream with the class MVC typography headline 6. There’s also a variety of element classes that I can play with here, and I think that this is a really great way to prototype. So inside of this, if I started typing MVC typography, we get an autocomplete evolve, the different type of graphic options, so you can just sort of scroll through and start to see what these would look like within here.

So you can see that headline. One highlight two aren’t actually making a difference and that’s because I still have MVC headline six selected, so you want to uncheck that and now we can see what the headline two looks like. So if you truly want to play with a baseline for the styling here, remove all the classes and then start to search the classes that are relevant to this element. So here we can see all the typographic styles and decide which ones make sense.

This also lets you separate your logic from your styling. Your logic is the date of the text content here. So this is a header one in this form fields element, but you can apply a class on top of it to style it. However, you want say we want to style this headline, one like a headline: five or headline six. We can do that and we can test that inside of this elements panel, I’m going to select the button now so here I’m just going to hit inspect on the button and it’s going to go right to that element, and we can see that this button has A few classes as well so right now we’re using the MVC buttons raised style.

But here I could select this outline style as well and put that in there. You can play with density inside of buttons, and these are all classes that I’ve just sort of playing with earlier applying them, seeing what they looked like testing this in the browser. So it’s a really powerful tool and there’s a ton that you can do by changing. Some of these Styles around you can reorder things in the Dom as well.

I’m going to just grow back into my UI here so say we want this headline. Maybe after the paragraph we could do that we’ll probably want to change that to be not an h1. In that case, you know make sure that your Dom still makes sense, but you could reorder things you could reorder the buns. You could reorder this remember this device text there and really just start to play with your UI in the browser.

So here you could even change the text of what this says: I’m finding this label here and now I can change this to always Pat disco, and now I’m checking I’m always going to Pat disco. This is a place where you can test our front language if you’re unsure how a piece of text will fit into an element on your screen, then you can test that and you can test this at various sizes. Let’s look at the color palette now.

In this example, we have a custom properties based theme playground for material design if you click to inspect the page and we’re opening up that panel and inside the HTML. You can see this route here with all the custom properties and their color values. So here we can start to play with what our colors look like. So if I want to change the primary theme color, I could click on that square and I have this whole array of color here that I can start to pick and choose and play with and see what that looks like in my UI.

Maybe I want like a bright paint color here. You can also change the color type, so here’s the hex code, if I click on these arrows here it changes from hex to RGB a to hsla and speaking of alpha. You can also use this alpha toggle to toggle the Alpha, which is the amount from transparent to full color and how much will show through to elma behind it. So we have all of these capabilities and another fun one.

Is this color palette, so in this color palette we get a base palette of colors, but we can also start to pick color from the page. So you see these page colors here were the initial page colors. These color variable is that the dev tools have found from the page as most proud colors on that page. So, if you’re working within a design – and you have a pallet that you’re working with you – can easily find the colors from that pallet inside of this dev tool here, there’s also the CSS variables that are being pulled out here that we can get the color values From and we can just start to really play with color in this way so say I want this to be a bright pink.

That sounds good. Maybe we want to change the color on that primary here. We can also change this color value. We can change the we can change the secondary color value, so we can change this to maybe a purple value and you can start to see how this cascades throughout the entire application. So now, if I scroll through, you can see that all the secondary values, like the check boxes, the switches.

This is all live code and we can see that this is in the browser, transitioned and transformed all these elements to be pulling in those colors in the drawer. Here we see a background on the active elements and that is also pulling from the primary color but being all but faded behind, so you can still see a contrast with the text on top of it. So by applying these color changed in the browser, you can really see what that looks like in various elements and in various states of those elements.

So let’s go back to the top up here and select this button. What we can also do in dev tools is figure out if something is accessible or not, which is a really great shortcut to have so here on the button, we have a color value on a background, color value. If I click on this color value, you can see that we have a contrast. Ratio of 8.5 point 1, that’s great. If we made that color value a little bit more closer to the background color that contrast ratio starts to get lower and you can see that that’s no longer accessible.

So we get a lot of help here when you have a color and background color within an element to help us make decisions on contrast and on colors that are accessible for our users. If we click this open, we can see where that’s accessible. So we can see that’s accessible for double a for a larger text and not necessarily for Triple A which means that we will have to have that white value for it to be triple a contrast, accessible and for this to work for our users.

Make sure that you’re keeping accessibility in mind at all times and you can use the color palette inside of dev tools to help you to do that, especially when it comes to color selection and readability on various elements. Let’s talk about the animations palette in episode 1, where we talk about motion on the web, we recreated this wringing button. So let’s inspect this element in dev tools and explore what that looks like behind the scenes.

So here I’m going to hit inspect on this button. I want to make sure that I have the button selected here and then in the bottom. Here we have this animations palette. Now, if you don’t have this, you can find it in this drop-down menu, this little kebab menu and you can hit more tools in the drop-down and then animations is the first item in that secondary drop-down. So that’s what’s going to bring up this animations palette for us so now, inside of here we see that we have multiple animations happening.

If I click open on those animations, we can find that on the button we have this grow animation, where it’s growing over time and on the icon. Inside of that button, we have this ring and I can actually toggle back and forth between here like this. Is a keyframe, I can go forwards and backwards. I can pause. I can play I’m going to pull this up a little bit here. So what I can also do is I can adjust the time stamps.

These are percentage based animations. So if I wanted to make this grow really long, this will break up the effect of ringing as it grows, but just to show you, I now have it sort of off-kilter. It’s ringing its ringing still sort of not changing its size, so you can start to really break things down here. I have it ringing very tightly in this section here. I could break that up too. You can move these around, so I can move it back and forth.

I can have this start to grow before I have the ring happening, I’m ruining this animation, but the idea is to show you that you can have animations that are separate from each other and then also test what this looks like together. So now I have it ringing off kilter from the size changes, but here, if you have a lot of animations that are complexed and you have to orchestrate them together, you can take a look at what that is what that looks like you can take a look At what the animations are happening inside of this element, so we have a box shadow change to as its growing and changing color there and then start to play with this start to finesse in the browser – and this is a really great opportunity for prototyping, because if Things are off, it’s a lot easier to see it visually.

That is, to try to read the code and figure out what’s a little bit off when those time stamps are different too. If you want to slow this down to get a little bit more of a finesse and detail here, you can also change the speed at which you’re animating. So here I’ve set 25 %, and I have this very slowly now, starting to grow and shake here. Starting to ring very slowly so this is another opportunity for you to adjust speeds and see where things are in a very precise way.

10 % is another speed option for very complex animations. Again, you can start to really get in there in the details and intricacies of these animations. Let’s talk about the device panel. Now I use this tool all the time and we’re going to showcase it. I’ve opened up the material dot, IO websites and I’m just going to hit inspect from any part of the page to open up dev tools, and here is that device toggle toolbar.

So I could also hit command shift M on my Mac computer, but anywhere that you’re using Chrome. You could always, let’s click into the browser, screen and open up dev tools, so I’ve clicked this open here. I have a few options. I have a responsive option, so I could see what this looks like at various screen sizes by dragging it over. You also have a drop-down here with some default devices like the Galaxy s5, the pixel to excel, to see what this looks like at various screen sizes.

We have the iPad pro here and you’re also able to adjust this from horizontal to vertical. So you can see what that looks like when you flip that device. That’s a little bit more dramatic on a phone here. So if you flip that you get a completely different layouts, you can also again use this responsive mode. You can even edit what the devices are that you want to showcase. So here in the dev tools, I’m going to pull this out and going to hit edit, and I have a bunch of devices here – you can add a custom device.

You can add devices that aren’t currently in your drop-down by default. So if you want to test, for example, on the iPhone 4 or if you want to test on the Nexus 7, I will now have these inside the drop-down. When I next open it so there they are iPhone 4 and the Nexus 7 somewhere in this drop-down right here. So you can see that that focal looks completely different than that iPhone 4 and that’s important for you to know.

As a designer. We also have various breakpoints here that allow for you to just quickly change between common sizes, mobile medium, large tablet, sizes, laptop sizes, and you get a percentage based visualization here, that’s scaled down to fit inside of this browser screen. So if you want, you can make this 100 % view you can make it a even larger view. If you want to sort of zoom into that, you can make it 50 % sort of fit in this area, and then you can see really large screens, 4k screens you’ve.

Even and if you don’t have a 4k screen, you can still test on those devices. Your design does change, based on the DPI of your screen and kind of is determinate of whether you have a Retina screen or non-retina screen, and that can also come into play when you’re deciding what images to send to your users. So if you want to test those, I have an example here with disco right how this high-res image and now inside of my dev tools, I can actually test to see that I’m sending a lower res image with the lower resolution browsers in my CSS.

I have a media query where I’m sending a different image based on the density of the screen. So here, if I have a high density, Retina screen, I’m sending a high resolution in and if I don’t it’s going to fall back to this low resolution image. So I can actually test this with a medium dpi screen inside of my dev tools, and this becomes really useful if you’re sending a lot of large images. And you want to think about performance for your users and for their devices.

And if all of those aren’t enough for you, there are some tools that you can use to extend these capabilities and make designing of the browser even easier. One of those tools is called this bug created by Adam Argyle. We’re going to fly over to him to have him explain why it’s so useful for designers right now. Hmm this page, I like the layout – I think, there’s a lot going for it, but it’s lacking some color.

Don’t you think? Well, let’s have fun and and try it out. Let’s make some color in here with this bug. All I have to do is launch it. In fact, I’m going to scoot it down off of here so that it’s not conflicting with the header and I’m going to start with the header. It’s just asking for a nice, bold color, don’t you think? What do you think of, like a purple, ooh, pink, a hot pink, I’m always down with hot pink all right? I’m actually settled that that’s great okay, you can see our guides tool is trying to help us make some alignment checks here, as we as we hover on other elements, you can get distance between them all right, but I’m also interested in changing this image up here And I’m going to drag in a new one.

Will I have an SVG icon here? It looks like this. Oh thanks for that that didn’t work out very well, but if I drop it up here, look at that. I replaced it. That’s kind of nice all right. So what about all of this these cards? I feel like they’re a little tight. I want to check. I want to check out their spacing here. So if I select I’m trying to select the card element in here and I’m having a little bit of a trouble because there’s so many elements in here look, I can actually select the elements inside of here.

That’s crazy town, but in order to get to these grid items, I’m going to select the parent and use keyboard navigation to help me get there. Someone hit Enter, which is going to select the first item as a child, so I selected the grid container. I hit enter and I selected the first one I hit command shift E, which is going to expand my selection. It’s going to find all the other elements to match.

I found them there. I still have the margin tool. So if I hit shift down twice, I’m going to add 20 pixels to the bottom, and that’s going to give me a little bit of spacing between my rows super neat. I can actually continue using this multi selection of I hit enter. I’m going to find that that each of these cards had a rapper element I hit enter again and I’m going to get the imaged container. That’s in the upper part.

If I hit tab I’ll find the next element in the layer tree – and I want to change the alignment of these, so I’m going to grab my flex box a line tool, I’m going to hit left and right and find a nice alignment and I’m going to Pick the center and I’m going to hit enter again and we’ll find that there’s a span inside of that container, and I want to change the text size of this. I grabbed the text tool I’m going to hit up on the the keyboard a few times here.

I’m going to hit command down to change the font weight and, of course it needs some hot pink. So I’m going to go over here to my color picker and grab that hot, pink from up top close out and see what I did and look at that. We brought some color to that page, really pretty quick and we’re able to just sort of express ourselves in a nice fast manner. That was fun. I hope you enjoyed seeing a little bit of taste of what vis bug can do for you super cool.

Thank you. Adam, I really love using dev tools. I use them every day for designing directly in the browser for prototyping for making decisions, and I find that these tools are just getting better and better and better, and that’s really exciting. For me, as somebody who likes to design and build the browser, what are your favorite to have tools? Are there any that we missed in the show, and that we didn’t mention tools that you use every day? I would love to hear your thoughts.

Please leave a comment below and thanks for reading the show we’ll see you next time save I just go.