What else can JavaScript be used for?

Question

In the context of this exercise, what are some other things that JavaScript can be used for?

Answer

JavaScript can be used for many useful applications on a web page, mainly by making pages more dynamic and interactive. A few of the most useful things that JavaScript provides is user interaction, and the ability to change the structure and style of a page dynamically.

By utilizing the DOM (Document Object Model) of a page, JavaScript can change the structure of a web page. It can add, remove and change elements on the page dynamically. For example, if we wanted to add a new section to the page, we could do something like

var d = document.createElement("div");
document.body.appendChild(d);

This will create a new <div> element, then add it at the end of the document body.

JavaScript also allows us to change the style of the page, letting us change, add, or remove style rules to elements. For example, if we wanted to set the background color of an element with a specified id, we can do so using JavaScript, like so

document.getElementById("square").style.backgroundColor = "gray";

These are just a few of the many useful features that JavaScript gives us when working with web pages and applications.

15 Likes

This is just the beginning. Those lines will start to make more sense when you get to the JavaScript part of the course

26 Likes

Can entire code be written with JavaScript alone? Or do you still need html?

3 Likes

HTML is declarative and likely the best way to form the DOM, then use behaviors to manipulate it. It is not very practical to construct an entire document using JS, even while it can be done.

16 Likes

Thank you, this answers my question perfectly.

3 Likes

Hi! I’m curious, why use JS to do things HTML and CSS can? Why not add classes in HTML or change color with CSS?

2 Likes

I am still quite new to learning CSS, HTML and JavaScript! I’m learning these languages because I wanted a change in career and it is super challenging and makes me a little nervous knowing that im throwing myself into something so foreign to me. Although, I got to say it is quite intimidating but that’s what makes learning these languages so exciting.

10 Likes

Given you are at the learning stage, entry level, the best suggestion would be to get a stack of coil scribblers and a bundle of pencils. Don’t forget the pencil sharpener. Take copious notes as you go. This stuff only gets burned into your brain if you first imprint it. Writing on paper is the way to do this, and you have a full archive to refer to later. Write things down.

If you have the luxury of space, or even wall space, get a white board and start writing things down there when you want to explore. State what your idea is, lay out a plan, and then go to your canvas (editor) and paint your picture (write your algorithm).

This is all very daunting and new, but soon enough it will be child’s play and you will be in complete control. Don’t take your eye off the ball.

24 Likes

Ive been taking notes as I go through the lessons, thank you for the tips! I luckly have the space for a whiteboard so that is something I wil take into consideration. I will focus.

1 Like

Because JavaScript opens up for more than just changing the color or applying classes to an HTML element. While JavaScript could be used for all of that it can also be used for much more, thing that HTML and CSS alone can’t do. I’m not sure how far you’ve progressed since writing this but I’m sure you will get the hang of it and be able to answer your own question soon enough.

For instance JavaScript can be used to communicate with a database or make your website reactive with frameworks such as React, Vue, Angular, Svelte and so on (don’t get overwhelmed, you don’t need to know all of the to become a full-stack developer and can suually just pick one to learn more about). The sky is the limit for what you can make when you introduce the more advanced concepts of JavaScript into you web app/website.

EDIT: I guess I didn’t directly answer your question but my point is that you can use JS for more than just what you already can use HTML and CSS for. And there will be times where doing things that can be achieved with only HTML and CSS will be easier with the help with JS. I hate to put it this way but you’ll understand more about all of this when you get more hands-on experience. I wish you the best of luck with learning more about JavaScript! :slight_smile:

5 Likes

Hey! Thank you for the broad answer. Actually, I’m learning CSS now and I can’t wait to start learning JS

I’m glad you liked my answer. CSS is a ton of fun! Coming from HTML & CSS, JavaScript can be a bit overwhelming at first since it’s the first “actual” programming language, it’s exciting seeing all of the new things one could do but also a bit scary at first. I’ve always found it a tad bit hard myself and therefor preferred php frameworks like Livewire etc because I started out learning php instead of JS, but started getting on here to finally learn JS. Hopefully you’ll enjoy jour journey through JavaScript though since that really is what lands you a job today.

If you happen to have any other questions feel free to reach out to me and I might be able to give you an answer! :slight_smile:

1 Like

Yeah, I know it’ll be hard for me, but I will do this. I’m planning to join 100DaysOfCode challenge and write every day reports on Twitter and LinkedIn. I think it’ll be beneficial for the further job landing process.

So, are you using PHP in your work now? I know it’s a little bit old language, but companies need PHP developers. Why are decided to go for JS? Interested in Front?

1 Like

Since this is going a bit off-topic to the thread we are posting in I’ll send you a DM where we can talk to each other. I appreciate talking, see you in the DMs! :slight_smile:

1 Like

JavaScript is Behavior or interaction layer, it’s used for more.

The latest version of ECMAScript is no longer using var. It now uses let.

1 Like

which is stretching the truth. let was not introduced as a replacement, but a block scoped declaration, something var is not capable of, short of function bodies. The goal of let is to prevent leakage from block constructs, not just functions. A study of why this is of some value would be critical at this point, if time allows.

The language has not mutated away from the legacy, vanilla version, and neither should we expect it to include recommendations that suggest we no longer use something until it is resolved to deprecated or obsolete, something I cannot see happening to var.

4 Likes

do people still use var ?
const d = document.createElement(“div”) will be a better option, right?

var has not been deprecated or made obsolete, so can still be used. However, in the example above, const is appropriate if one wants the variable to persist for the whole session.

2 Likes

Hello! I always see you! i am a fan of you! wanna see my website?