DEV Community

NovaJay2415
NovaJay2415

Posted on

Review of my first JavaScript code?

Hello to all the devs out there! This is my first post. I saw that there are a lot of awesome devs up here so I was wondering.. could you look over my first JavaScript code? It's hardly anything, just something super basic I made with the things I learned. If there is any way that I could improve my code please let me know. And if there is an easier way to do what I did, I am open and willing to learn. Thank you!

https://codepen.io/Nova/pen/XwPXOq

Top comments (5)

Collapse
 
larsklopstra profile image
Lars Klopstra ⚡

Hey that's pretty cool! Here's my feedback on the code:

  1. Make the prompt a const variable instead of let.

  2. Please, don't use var anymore, you seem to know let and const do exist ;)

  3. Your prompt is prone to XSS, try to find a different solution for inserting content in the DOM

  4. Don't use else if statements, they add unesseccary code, an if statement is good enough (bail out early)

  5. (My opinion) use arrow functions instead of regular ones, ex: const foo = (a, b) => a + b;

  6. (Also my opinion) use document.querySelector('.classEl #idEl');

How long have you been using JavaScript? It looks amazing if you're just starting out!

Collapse
 
novajay2415 profile image
NovaJay2415

Thank you for such awesome feedback! I will definitely take all this into account. I really appreciate it. I might be hittin you up for more code reviews.

I started learning about 10 days ago. I'm taking courses on Udemy and got a couple books too. And been reading things on javascript.info (they have some nice documentation). Do you know any good Javascript courses or resources for beginners?

Thank you again!

Collapse
 
larsklopstra profile image
Lars Klopstra ⚡ • Edited

10 days ago? This is some solid code! I'd recommend looking at Traversy Media, Google Chrome Developers, Andre Madarang and Fireship. Once you get more advanced you'll feel home in documentation sites like Mozilla

Collapse
 
strdr4605 profile image
Dragoș Străinu

There is no need to init the text variable on top of the function. Just init it before setting innerHTML

const text = `Hello ${rebelName}, you have a total of ${xWings} xWing fighter pilots. Good luck!`;
document.getElementById("demo").innerHTML = text;
Collapse
 
novajay2415 profile image
NovaJay2415

Thank you for your feedback! Taken into account.