The Wayback Machine - https://web.archive.org/web/20240701203615/https://www.geeksforgeeks.org/sugar-js-introduction-and-installation/
Open In App

Sugar.js Introduction and Installation

Last Updated : 16 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will try to understand all the details which are associated with the library called Sugar-JS. Sugar is an open-source JavaScript utility library that is used for working with native objects. Users can define methods or use plugins to handle specialized use cases.

Purpose of using Sugar-JS:

  • The main purpose of using Sugar-JS is that this library provides the utility functions for working with native objects (like Array, String, Date, Range, Object, and so on).
  • The Sugar is actually the main point of interaction (that is the global object) through which any user could the predefined methods available under this library.
  • Sugar defines helpful utility methods that can be called static functions on the Sugar global object.

Installation: Use the following command (to be entered on command prompt itself) for installing sugar in the local system-

npm install sugar --save

Running Sugar-JS: Sugar’s code could easily be run in two different ways:

  • Over browser’s console (since here sugar is already available and could easily be executed successfully)
  • Using custom build available (in local system or PC)

1. Over browser’s console: Since the browser’s console already has inbuilt sugar installed, therefore here we may directly use Sugar along with its predefined methods.

Let us see some of the following examples which if we write over browser’s console, may result in some output which is display over console itself (Note these code snippets we have to run over browser’s console using CTRL + Shift + I then writing or pasting code over there and pressing Enter to see the output)-

Example 1: In this example, we would be creating an array using the inbuilt method provided by Sugar itself.

Javascript




let new_array = Sugar.Array.create("abcde");
console.log(new_array);


Output:

Example 2: In this example, we will see some functions provided by Sugar which are of both Array and String.

Javascript




console.log(Sugar.Array.isEqual(['a','b'], ['a','b']));
console.log(Sugar.String.isEmpty(''));
console.log(Sugar.String.reverse('GeeksforGeeks'));
console.log(Sugar.String.shift('a', 7));


Output:

2. Using custom build available (in local system or PC):

Following steps you need to follow first to make custom build available in local system or PC-

Step 1: Go the link (mentioned forward): https://sugarjs.com/download/ 

Step 2: Scroll down until the Custom Build section is available in front.

Step 3: Two options are available on the bottom right part of the screen (Download Dev and Build Minified as shown in the below image too) and further click on the Build Minified button.

Step 4: After clicking on Build Minified Button, the custom build starts building its own and if it stops after getting built successfully it will output something like shown in the below image

Step 5: Click on Download Min and thereafter a custom build file is downloaded paste over any specific location in the local system or PC since it will be used in the later part of the sections (that is while writing the code snippet it is used)

Step 6: In order to use that JavaScript file that was downloaded in the previous step, one more file (which is a .html file)  has to be created and in that file, we have to attach (or import) the JavaScript file using the following tag line-

<script src = "<path_to_file\sugar-custom.min.js"></script>

Example 3: In this example, we will be implementing Sugar methods in the “.html” file and will see the result after running the file over the browser successfully.

HTML




<!DOCTYPE html>
<html lang="en">
    
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Sugar.js</title>
    <script src = "..\\sugar-custom.min.js"></script>
</head>
    
<body>
    <p id="print"></p>
  
    <p id ="print2"></p>
  
    <script>
        let array = Sugar.Array.construct(5, (i) => {
             return (i * i)
        });
        document.getElementById("print").innerHTML = array;
        let string_creation = Sugar.String.reverse('GeeksforGeeks');
        document.getElementById("print2").innerHTML = string_creation;
    </script>
</body>
    
</html>


Output:



Similar Reads

Svelte | Introduction and Installation
Svelte is the new methodology for creating web apps. It can be used in a small part of a code or in an entire single page application. It is a compiler not a framework, which is faster than other JavaScript libraries like ReactJS, AngularJS, VueJS. It is used to create reactive web apps. If any change occurs in the data that change will reflect on
2 min read
Introduction and Installation process of Gulp
Gulp can be defined as a task runner or a toolkit for automating time-consuming tasks such as magnification, concatenation, cache busting, unit testing and linting in web development. It uses Node.js as a platform and utilizes JavaScript code to help in running front-end tasks and large-scale web applications. These tasks can be run on PowerShell,
3 min read
Ant Design Introduction and Installation for Angular
Ant Design is a design pattern for enterprise-level products that can be integrated with other front-end frameworks such as Angular, React, or Vue. Ant Design official implementation is released with React but it can be used with other JavaScript frameworks. It is an open-source tool with approximately 50.4K GitHub stars, it is the world's second m
3 min read
MDBootstrap Introduction and Installation for Angular
Introduction: MDBootstrap (Material Design for Bootstrap) is a complete UI package that can be integrated with other frameworks such as Angular, React, Vue, etc. It is used to design a fully responsive and mobile-friendly layout using various components, plugins, animations to make it more attractive and user friendly which are compatible with othe
3 min read
SweetAlert Library Introduction and Installation with the Example
When building JavaScript-driven websites we often have the need to provide feedback to our users to let them know if the action they have performed has been successful or not. In the early days of the web, developers used to create messages using the window.alert() function. While alert() works in practice and it’s also consistent across browsers,
5 min read
AsgardCMS Introduction and Installation
AsgardCMS is made for developers. It is a full-featured, multilingual modular CMS developed on top of the Laravel framework. The built-in quick-development tools allow the developer to avoid spending their time on design and concentrate on functionality. It has the following characteristics: Completely Responsive: It has Bootstrap, which is fully r
3 min read
DayJS Introduction and Installation
Day js is a lightweight date and time manipulation library. It is an alternative to moment js with the same modern API. The latest version of day js has a size of 7Kb (minified) and 2Kb (Gzipped). Due to its small size, it takes less time to load and thus improving the overall performance of the application. Features: It is easy to use and since it
2 min read
ASP Introduction and Installation
ASP stands for Active Server Page. It is commonly known as Classic ASP or ASP Classic. ASP is a server-side scripting language that is used for executing the scripts on the webserver. This technology was developed by Microsoft. It is an old but still useful tool used to create dynamic webpages. ASP is a program that runs inside a web server. Active
3 min read
Introduction and Installation of Nightmare.js
Nightmare is provided by Segment and is a high-level browser automation library. The goal for this Nightmare is to follow the path of the user actions (like goto, type, click, etc.), in order to expose few simple methods with an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It has been designed for aut
2 min read
Framer-Motion Introduction and Installation
Framer-motion is an open-source, production-ready animation and gesture library for React. It provides a low-level API to simplify animation and gesture integration into the application while maintaining HTML and SVG semantics. Features: Animations: It will automatically generate animations by setting values in the animate prop.Gestures: It support
3 min read
three90RightbarBannerImg