JavaScript Use Cases
Kaja Uvais

Kaja Uvais @kaja_uvais_a8691e947dd399

About: Hi there! I'm Uvais, a passionate website designer with a love for all things web development. Uvais Codes is your go-to resource for web design tips, coding tutorials, and everything you need to buil

Location:
India
Joined:
Sep 14, 2024

JavaScript Use Cases

Publish Date: Oct 17 '24
1 0

In this post we explore basic javascript use cases.

1. Add class to element in JavaScript

<div id="box"></div>
Enter fullscreen mode Exit fullscreen mode
const element = document.querySelector("#box");

// Note: only class name, without the '.'
element.classList.add("class-name");
Enter fullscreen mode Exit fullscreen mode

2. Call a function in JavaScript

function greetUser() {
  return "Welcome to code to go!";
}

let result = greetUser();

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to code to go!

3. Capitalize first letter in JavaScript

let str = "uvais codes";

str = str[0].toUpperCase() + str.substring(1);
Enter fullscreen mode Exit fullscreen mode

Output:

Uvais codes

4. Change CSS property in JavaScript

<div id="box"></div>
Enter fullscreen mode Exit fullscreen mode
const element = document.querySelector("#box");

//change background-color
element.style.backgroundColor = "red";
Enter fullscreen mode Exit fullscreen mode

5. Convert JSON to string in JavaScript

const object = {
  id: 1,
  name: "Leanne Graham"
};

JSON.stringify(object);
Enter fullscreen mode Exit fullscreen mode

Output"

{"id":1,"name":"Leanne Graham"}

Thanks for Reading

Comments 0 total

    Add comment