JavaScript Use Cases
Kaja Uvais
Posted on October 17, 2024
In this post we explore basic javascript use cases.
1. Add class to element in JavaScript
<div id="box"></div>
const element = document.querySelector("#box");
// Note: only class name, without the '.'
element.classList.add("class-name");
2. Call a function in JavaScript
function greetUser() {
return "Welcome to code to go!";
}
let result = greetUser();
console.log(result);
Output:
Welcome to code to go!
3. Capitalize first letter in JavaScript
let str = "uvais codes";
str = str[0].toUpperCase() + str.substring(1);
Output:
Uvais codes
4. Change CSS property in JavaScript
<div id="box"></div>
const element = document.querySelector("#box");
//change background-color
element.style.backgroundColor = "red";
5. Convert JSON to string in JavaScript
const object = {
id: 1,
name: "Leanne Graham"
};
JSON.stringify(object);
Output"
{"id":1,"name":"Leanne Graham"}
Thanks for Reading
💖 💪 🙅 🚩
Kaja Uvais
Posted on October 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.