JavaScript Use Cases

DDD
Release: 2024-10-17 20:16:03
Original
921 people have browsed it

JavaScript Use Cases

In this post we explore basic javascript use cases.

1. Add class to element in JavaScript

<div id="box"></div>
Copy after login
Copy after login
const element = document.querySelector("#box");

// Note: only class name, without the '.'
element.classList.add("class-name");
Copy after login

2. Call a function in JavaScript

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

let result = greetUser();

console.log(result);
Copy after login

Output:

Welcome to code to go!

3. Capitalize first letter in JavaScript

let str = "uvais codes";

str = str[0].toUpperCase() + str.substring(1);
Copy after login

Output:

Uvais codes

4. Change CSS property in JavaScript

<div id="box"></div>
Copy after login
Copy after login
const element = document.querySelector("#box");

//change background-color
element.style.backgroundColor = "red";
Copy after login

5. Convert JSON to string in JavaScript

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

JSON.stringify(object);
Copy after login

Output"

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

Thanks for Reading

The above is the detailed content of JavaScript Use Cases. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!