DEV Community

Cover image for JavaScript Use Cases
Kaja Uvais
Kaja Uvais

Posted on

1

JavaScript Use Cases

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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more