Vite / Vite and Vanilla JavaScript

Vite with Vanilla JS

This tutorial will teach you how to use Vite with Vanilla JavaScript to create a modern web application. You will learn how to setup a new project, write JavaScript code, and use …

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explains how to use Vite in a project with just JavaScript (no framework)

Introduction

This tutorial aims to help you understand how to utilize Vite with Vanilla JavaScript to develop modern web applications. You will learn how to set up a new project, write JavaScript code, and leverage Vite's fast development server.

By the end of this tutorial, you will:

  1. Understand the basics of Vite and how it works with Vanilla JavaScript.
  2. Know how to set up a new web development project using Vite.
  3. Understand how to write and organize your JavaScript code in a Vite project.

Prerequisites:

  • Basic knowledge of JavaScript.
  • Familiarity with terminal commands.
  • Node.js and npm installed on your machine.

Step-by-Step Guide

Vite is a build tool and development server created by Evan You, the original creator of Vue.js. It offers a faster and leaner development experience for modern web projects.

Setting up a new project

First, install create-vite globally using npm:

npm install -g create-vite

Next, create a new project:

create-vite my-project

Finally, navigate into the project directory and install the dependencies:

cd my-project
npm install

You now have a new Vite project ready to go.

Writing JavaScript code

In a Vite project, you can write Vanilla JavaScript in the main.js file located in the src directory. For example:

// src/main.js
console.log("Hello, Vite!");

Using the development server

You can start the Vite development server with the following command:

npm run dev

Now, if you open your browser and navigate to localhost:5000, you should see "Hello, Vite!" in the console.

Code Examples

Example 1 - Basic JavaScript in Vite

Here's a simple example of JavaScript code you could write in a Vite project:

// src/main.js

// A simple function to add two numbers
function add(a, b) {
  return a + b;
}

// Call the function and log the result
console.log(add(2, 3)); // Outputs: 5

Example 2 - DOM Manipulation

This example demonstrates how you can manipulate the DOM in a Vite project:

// src/main.js

// Select the body element
const body = document.querySelector('body');

// Create a new div element
const div = document.createElement('div');

// Set the div's text content
div.textContent = 'Hello, Vite!';

// Append the div to the body
body.appendChild(div);

When you run this code, you should see "Hello, Vite!" appear on your webpage.

Summary

In this tutorial, you've learned the basics of using Vite with Vanilla JavaScript. You've set up a new Vite project, written some JavaScript code, and used the Vite development server.

To continue learning about Vite, consider exploring its plugins and how to use them to enhance your development experience. The Vite documentation is a great place to start.

Practice Exercises

  1. Write a JavaScript function that calculates the factorial of a number.
  2. Write a JavaScript function that reverses a string.
  3. Write a JavaScript function that fetches data from a public API and logs the result.

Try to solve these exercises on your own, then check the solutions below.

Solutions

  1. Factorial function:
function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

console.log(factorial(5)); // Outputs: 120
  1. Reverse string function:
function reverseString(str) {
  return str.split('').reverse().join('');
}

console.log(reverseString('Hello, Vite!')); // Outputs: '!etiV ,olleH'
  1. Fetch data from API:
fetch('https://api.publicapis.org/entries')
  .then(response => response.json())
  .then(data => console.log(data));

Remember, practice is the key to mastering any programming concept. Keep experimenting and coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help