Beginner's Guide to Writing JavaScript Code for Webflow | Tutorial with Examples

Published on
December 24, 2021

How to Write Your First JavaScript Code in Webflow
Do you want to learn how to write your first JavaScript code? In this tutorial, we’ll cover the fundamentals of JavaScript using code sandbox.io. Feel free to create a free account if you’d like to follow along. We'll start by creating a static project in code sandbox.io, which will set up an HTML site for us. When writing JavaScript, it’s usually best to place it just before the closing body tag in the HTML file.

Adding JavaScript to the HTML
To include JavaScript in the HTML file, you need to create opening and closing script tags. These tags are denoted by <script> and </script>. Inside these tags is where you can write your JavaScript code. Let's start with a simple alert command.

<script>  alert('2020');</script>

When you save and open the site in a new window, you'll see an alert that says "2020". Congratulations! You've just written your first line of JavaScript. It's good practice to have a separate file for all your JavaScript code instead of putting it directly inside the HTML file.

Creating a Separate Script File
To create a separate JavaScript file, click on the new file icon and name your file script.js. Move the JavaScript code we wrote previously to this file. You then need to link the script tag in the HTML file to the new script file.

<script src="script.js"></script>

When you save and reload the site, it should work the same way as before.

Understanding Variables
Variables are used to store information and can hold different types of data such as numbers, strings, booleans, or elements. Here's an example of how to declare and update a variable in JavaScript:

// Number typelet gamePoints = 500;// String typelet userName = "John";// Boolean typelet canFight = true;// Element typelet sprite = document.getElementById('sprite');

Performing Arithmetic Operations
JavaScript allows you to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. You can also combine variables of different types using concatenation.

let result = gamePoints + 100; // 600let message = "Congratulations! You've got " + gamePoints + " points";

Creating Functions
Functions are a core building block of JavaScript and are used to group together related bits of code for reusability. Here's an example of how to create a simple function in JavaScript:

function increasedPoints() {  let newPoints = 700;  alert("Congratulations! You've got " + newPoints + " points");}

Using If-Else Statements
If-Else statements in JavaScript allow you to check for conditions and execute different blocks of code based on those conditions.

let points = 200;if (points > 100) {  alert("You have at least 100 points");} else {  alert("You have less than 100 points");}

Putting It All Together in Webflow
Now, let’s put all this knowledge into practice by creating a simple game using JavaScript in Webflow.

  1. Create a new JavaScript file named game.js.
  2. Copy the URL of your site in Webflow.
  3. Search for the game.js file and import it to your project in Webflow.
  4. In your project, import the script file using the script tag in the HTML code.

Game Example
In this game example, we use JavaScript to create a basic timer and point tracking system. We also handle click events on specific elements to add or subtract points.

let timer = 5;let points = 0;$(".start").click(function() {  setInterval(function() {    timer = timer - 1;    $(".countdown").text(timer);    if (timer <= 0) {      $(".game-end").show();    }  }, 1000);  $(".overlay").hide();});$(".good").click(function() {  points = points + 5;  $(".points").text(points);});$(".bad").click(function() {  points = points - 5;  $(".points").text(points);});

Conclusion
In this tutorial, we’ve covered the basic fundamentals of JavaScript, including variables, functions, logic, and how to apply them to build a simple game. JavaScript is a powerful language that opens up a world of possibilities for web development. Now that you have a basic understanding of JavaScript, you can start building interactive and dynamic web experiences in Webflow. Happy coding!