DEV Community

Jasterix
Jasterix

Posted on • Updated on

6 JavaScript rules to get by

  1. Use const, then let

    • When creating a variable, you should use const, unless you expect you variable to change, in which case, you should use let. Never use var, especially as a beginner. If you're not sure whether your variable will change, always use const. Keeping with this rule will generate errors in your code that help you understand the nuanced differences between const and var over time. Intellectual curiosity will eventually lead you down that rabbit hole of addressing "scope" with this point. For now, stick with this role.
  2. Default to triple equality

    • JavaScript has this funky behavior of implicit type conversion (or coercion) that forces your variable into a type, based on the context. This is what allows you to add a string and a number. But when testing for a condition, using triple equality (===) insures that JavaScript will interpret your code exactly how you wrote it.
  3. Start with Model View Controller (MVC)

    • The first design pattern I learned will of course be my easiest to understand; so I may be biased. But having explored a few design patterns since my first line of code, I find MVC to be the most beginner friendly. MVC organize your code according to your programs concerns:
      • Model layer will hold your data model (your database)
      • View is the visual / presentation layer
      • Controller handles communication between the Model and View
  4. Try the module design pattern

    • Once you have a good grasp of the MVC pattern (or if you just want to learn an additional pattern), check out the module design patter covered here. Using this patter will encourage you to break your code into small functional components that each do one thing. This is a good habit to develop early.
  5. Learn functional programming before Object Oriented Programming

    • I'm sure you've heard that JavaScript is both a functional and an object-oriented language. Of the two, functional programming is easier and has a shorter learning curve than OOP. While there are hundreds of blogs to argue both sides of that statement, you can start building with FP before understanding classes, instances, inheritance and more. OOP can also be challenging if you don't have a good understanding of what your objects will be, also explored here.
    • Functional programming, on the other hand, is easy to get started with. It's about having modular code that focuses on what your functions do, instead of what your objects are. Again, developing great habits early.
  6. Always use template literals

    • Template literals are not only easier to read, but also easier to write, especially with multi-line strings. Switching to back ticks can be a bit awkward at first, but the small amount pain is completely worth never having to use a + sign to create a new line.

Top comments (9)

Collapse
 
rinsama77 profile image
imrinzzzz

Thank you! This is very helpful to a beginner like me!

Collapse
 
jasterix profile image
Jasterix

You're welcome! Let me know if there are any questions I can help you answer 😊

Some comments may only be visible to logged-in visitors. Sign in to view all comments.