DEV Community

Ryoichi Homma
Ryoichi Homma

Posted on • Updated on

Cont. of JS Basic - Modern vs. Traditional & Debugging

Today's Concept Highlights:

  • Access elements with querySelector methods
  • Access elements using older methods
  • Modifying element classes
  • Troubleshooting JavaScript in the browser console

1. Why Use querySelector() & querySelectorAll() Instead of getElementById() and getElementsByClassName()?

// Traditional methods
let elementById = document.getElementById('my-id');
let elementsByClassName = document.getElementsByClassName('my-class');

// Modern methods
let firstElement = document.querySelector('#my-id');
let allElements = document.querySelectorAll('.my-class');
Enter fullscreen mode Exit fullscreen mode

Limited Flexibility: These methods can only target elements by ID or class, which limits their flexibility compared to more modern alternatives.

  • getElementById() selects a single element with the specified ID.
  • getElementsByClassName() returns a live HTMLCollection of elements with the specified class.

CSS Selector Syntax: These methods can accept any valid CSS selector, allowing for more complex and powerful selections such as combining classes, IDs, attributes, and more.

  • querySelector() returns the first element matching the CSS selector.
  • querySelectorAll() returns a static NodeList of all elements matching the CSS selector.

Consistency: Using querySelector() and querySelectorAll() creates a more consistent codebase as the same method can be used to select elements by ID, class, tag, or any other CSS selector.

Static NodeList: querySelectorAll() returns a static NodeList, meaning the list of selected elements does not change if the DOM is updated. This can be beneficial for performance and predictability compared to the live HTMLCollection returned by getElementsByClassName().

2. Why Use Element.classList Instead of Element.classList?

// Traditional method
let element = document.getElementById('my-element');
element.className = 'new-class';

// Modern method
element.classList.add('new-class');
element.classList.remove('old-class');
element.classList.toggle('toggle-class');
let hasClass = element.classList.contains('check-class');
Enter fullscreen mode Exit fullscreen mode

2-1. className

Element.className is an older method used to manipulate the class attribute of an element. While it's simple and easy to use, it has some drawbacks:

String Manipulation: className requires manipulating the entire class string. This can be error-prone and less efficient, especially when dealing with multiple classes.

Overwriting Classes: Setting className directly overwrites all existing classes, which can lead to unintentional removal of other classes.

2-2. classList

Element.classList is a modern method that provides a more robust and convenient way to work with classes:

Convenient Methods: classList includes methods like add(), remove(), toggle(), and contains() to easily manage classes:

  • Element.classList.add()
  • Element.classList.remove()
  • Element.classList.toggle()
  • Element.classList.contains()

Performance: classList methods are more efficient for adding or removing individual classes without affecting others.

Readability & Maintainability: Code using classList is generally more readable and easier to maintain.

3. How to Troubleshoot JavaScript in the Browser Console

When your JavaScript code isn't working as expected, the browser console is an invaluable tool for troubleshooting. Here's how you can use it effectively:

Using Breakpoints:

  1. Open the Source Tab: In your browser developer tool, navigate to the Source tab.
  2. Open Your Code: Locate and open the script.js file or any JS file you're working on.
  3. Adding Breakpoints: Click on the line number where you want to add a breakpoint. This will pause the execution at that line when the code runs.
  4. Start Debugging: Click F9 to start debugging. The execution will stop at your breakpoints, allowing you to inspect variables, check the call stack, and step through the code line by line.

Benefits of Using the Browser Console:

  • Immediate Feedback: Instantly see the values of variables and the state of your application.
  • Step-by-Step Execution: Walk through your code one line at a time to identify where things might be going wrong.
  • Call Stack Inspection: See the sequence of function calls that led to the current point in execution.
  • Variable Inspection: View and modify the values of variables at any point during execution.

Using these tools, you can gain deeper insights into your code and quickly identify and fix issues.

Top comments (0)