So, you’ve started learning JavaScript—great choice! JavaScript powers everything from simple websites to full-on web apps. But once you’ve got the basics down, knowing a few clever tricks can really level up your code.
Here are 10 JavaScript tips and tricks that every beginner should have in their toolkit. Let’s jump in.
1. Use const
and let
—Not var
If you’re still using var
, it’s time to let it go.
Use const
for values that shouldn’t change, and let
for values that might. This keeps your code more readable and avoids unexpected bugs.
const name = "Alex";
let age = 30;
2. Template Literals > String Concatenation
Backticks (`
) are your new best friend.
They let you write cleaner, more readable strings—especially when you need to include variables.
const name = "Alex";
console.log(`Hello, my name is ${name}.`);
3. Logical Shortcuts with ||
and &&
Need a default value? Use ||
.
Want to run something only if a condition is true? Use &&
.
const userName = input || "Guest"; // If input is falsy, default to "Guest"
isLoggedIn && showDashboard(); // Only run if isLoggedIn is true
4. Use the Ternary Operator for Quick Decisions
Replace simple if/else
blocks with one-liners using the ternary operator:
const age = 18;
const canVote = age >= 18 ? "Yes" : "No";
5. Loop Smarter with forEach()
Forget traditional for
loops for simple array tasks. forEach()
is cleaner and more readable:
const fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => console.log(fruit));
6. Arrow Functions = Cleaner Code
Arrow functions aren’t just shorter—they also behave better with this
in many situations.
const greet = name => `Hello, ${name}!`;
7. Destructure Like a Pro
Destructuring lets you pull out values from objects or arrays with less code:
const user = { name: "Alex", age: 30 };
const { name, age } = user;
8. Use includes()
for Easy Checks
Want to know if an item exists in an array or string? includes()
makes it easy:
const colors = ["red", "green", "blue"];
console.log(colors.includes("green")); // true
9. Chain Methods for Clean, Powerful Logic
Methods like filter
, map
, and reduce
can be chained together to transform arrays beautifully:
const numbers = [1, 2, 3, 4, 5];
const doubledEvens = numbers
.filter(n => n % 2 === 0)
.map(n => n * 2);
10. Label Your console.log()
Outputs
Debugging? console.log()
is your best friend. Just be sure to label your logs clearly.
console.log("User age:", user.age);
Final Thought
Learning JavaScript is a journey—and picking up these tricks along the way makes that journey smoother and more fun. Try them out in your next project, and see how much cleaner your code can be.
Want to learn JavaScript? Be sure to check out my Udemy course:
Got a favorite beginner trick I missed? Drop it in the comments!