Sentiment Analysis using JavaScript and API
Last Updated :
28 Apr, 2025
Sentiment Analysis - Sentiment analysis, also known as opinion mining, is a computational technique used to determine and categorize the emotional tone or attitude expressed in a piece of text. Analyzing sentiment in text is a crucial task in natural language processing, allowing us to understand the emotional tone and attitude of the content. In this article, we will explore how to utilize the MeaningCloud API along with JavaScript to analyze the sentiment of an inputted text. MeaningCloud provides powerful sentiment analysis capabilities that can help us gain insights into the sentiment expressed in textual data.
Final Output Preview:

Prerequisites:
Before getting started, make sure you have the following:
- A MeaningCloud API key: Sign up on the MeaningCloud website to obtain an API key that grants access to their sentiment analysis service.
- Basic knowledge of JavaScript, HTML and CSS.
Approach:
- JavaScript Code Integration: Write JavaScript code to handle the user's input, communicate with the MeaningCloud API, and retrieve the sentiment analysis results.
- MeaningCloud API Integration: Incorporate the MeaningCloud API into your JavaScript code. Make an API request using the input text to send it for sentiment analysis.
- API Response Parsing: Receive the API response, which will contain sentiment-related information such as polarity (positive, negative, or neutral).
Below is the guidance for creating a web page that allows users to input text, and analyze the sentiment of the text using the MeaningCloud API.
Step-1 Create an HTML Page with Input - Start by creating an HTML page that includes a form where users can input text.
Step-2 Create a MeaningCloud API Account - Go to the MeaningCloud website and create an account. Once you have an account, you'll be able to access the API key required for making API requests.
Step-3Make an API Request - Use JavaScript to make an API request to analyze the sentiment of the input text using the MeaningCloud API. Replace 'YOUR_API_KEY' with your actual API key.
Project Structure :
--index.html
--style.css
--script.js
Example : Write the following code in respective files:
- index.html: Web page structure with HTML elements.
- style.css: Adds visual styles and layout to the page.
- script.js: Makes API call and fetches content, generating dynamic updates.
JavaScript
function analyzeSentiment(event) {
event.preventDefault();
const apiKey = "YOUR_API_KEY";
const text = document.getElementById("content").value;
const url =
`https://api.meaningcloud.com/sentiment-2.1?key=${apiKey}&txt=${encodeURIComponent(
text
)}&lang=en`;
fetch(url)
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("Request failed");
}
})
.then((data) => {
const sentiment = data.score_tag;
// console.log(`Text: ${text}`);
// console.log(`Sentiment: ${sentiment}`);
if (sentiment == "P+") {
document.getElementById("result").innerHTML = "Strong Positive";
}
if (sentiment == "P") {
document.getElementById("result").innerHTML = "Positive";
}
if (sentiment == "NEU") {
document.getElementById("result").innerHTML = "Neutral";
}
if (sentiment == "N") {
document.getElementById("result").innerHTML = "Negative";
}
if (sentiment == "N+") {
document.getElementById("result").innerHTML = "Strong Negative";
}
if (sentiment == "NONE") {
document.getElementById("result").innerHTML =
"Without Polarity";
}
})
.catch((error) => {
console.error("Error:", error);
});
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentiment Analysis</title>
<!-- Custom CSS -->
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div>
<form onsubmit="analyzeSentiment(event)">
<h1>GeeksforGeeks Sentiment Analyser</h1>
<textarea id="content"
placeholder="Type the text here to analyze sentiments!">
</textarea>
<br/>
<input type="submit" />
<div id="resultStyle"> Tone of speech: <span id="result"></span></div>
</form>
</div>
<script src="./script.js"></script>
</body>
</html>
CSS
body,
html {
margin: 0;
background-color: #fefae0;
}
h2 {
margin: 0;
}
.hero-text,
form {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #283618;
}
.hero-text button,
input[type="submit"] {
outline: 0;
display: inline-block;
width: 150px;
padding: 10px 0px;
border-radius: 5px;
color: #000000;
background-color: #bc6c25;
text-align: center;
cursor: pointer;
margin-top: 5%;
font-size: 20px;
font-family: 'Cormorant', serif;
}
.hero-text button:hover,
input[type="submit"]:hover {
border: 1px solid #dda15e;
}
textarea {
outline: 0;
padding: 10px 0px;
border-radius: 5px;
color: #000000;
background-color: #dda15e;
text-align: center;
margin-top: 5%;
font-size: 20px;
resize: none;
}
#resultStyle {
margin: 10px;
font-size: 30px;
color: #606c38;
font-weight: bolder;
}
a {
color: white;
text-decoration: none;
}
::-webkit-input-placeholder {
/* Edge */
color: white;
}
:-ms-input-placeholder {
/* Internet Explorer 10-11 */
color: white;
}
::placeholder {
color: white;
}
@media (min-width:420px) {
textarea {
width: 400px;
height: 170px;
}
}
@media (min-width:520px) {
textarea {
width: 500px;
height: 200px;
}
}
Output:

Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
Sequence Diagrams - Unified Modeling Language (UML) A Sequence Diagram is a key component of Unified Modeling Language (UML) used to visualize the interaction between objects in a sequential order. It focuses on how objects communicate with each other over time, making it an essential tool for modeling dynamic behavior in a system. Sequence diagrams
11 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read