Design comparison
Solution retrospective
I can code javascript language now which i am proud of
What challenges did you encounter, and how did you overcome them?how can i change javascript code for mobile and desktop separately
Community feedback
- @sksksk2024Posted 18 days ago
Hellow, @Jamal-Digital !!! :D
You can use
window.innerWidth
to check the screen width and adapt your JavaScript code for mobile and desktop experiences separately. Here’s how you might approach it:if (window.innerWidth < 768) { // Code for mobile view console.log("Mobile view"); } else { // Code for desktop view console.log("Desktop view"); }
By using a conditional like this, you can tailor specific actions based on screen width. The value 768 is a common breakpoint, but you can adjust it to fit the layout of your site. It’s a simple and effective solution for making your JavaScript responsive!
If you need the layout to adjust dynamically as the window resizes, you might also consider listening for the resize event:
window.addEventListener("resize", function() { if (window.innerWidth < 768) { // Code for mobile view console.log("Mobile view"); } else { // Code for desktop view console.log("Desktop view"); } });
Keep it up and have fun coding!!!
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord