| by Arround The Web | No comments

Getting started with JavaScript on Raspberry Pi

JavaScript comes under the list of popular languages used for programming that is mainly used for web development. On the other hand, Raspberry Pi can be termed as a compact size computer that performs all the functions that a conventional size computer does. So, if you are thinking of using the Raspberry Pi platform for JavaScript then follow this guide as I have explained the process for using JavaScript on Raspberry Pi.

Installing node.js for JavaScript on Raspberry Pi

To start using the Raspberry Pi for JavaScript first there is need to install the node.js that is required for programming in JavaScript.

The process of installation of node.js on Raspberry Pi is quite simple just follow the below mentioned steps:

Step 1: To download the node.js runtime environment execute the following command:

$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash

Step 2: Once the file is downloaded install the application by executing the following command:

$ sudo apt install nodejs

Step 3: After installing the node js check the version to see if the same version is installed correctly by executing the following command:

$ node -v

Step 4: To avoid any issues while using JavaScript it is better to install the build-essential package by executing the command given below:

$ sudo apt install build-essential

Getting started with JavaScript on Raspberry Pi

NodeJS is primarily used for JavaScript to code outside the browser so let’s write a simple code of JavaScript. Just follow the steps mentioned-below:

Step 1: Now to get started with JavaScript first make the directory so for illustration I have named the directory “mycode” by executing the command given below:

$ mkdir mycode

Step 2: Next create a .js file in the mycode directory by running the command given below:

$ nano javascript.js

The name of the file in this example is “javascript”, you can name it anything. A text file will open in which you can write the code in JavaScript, so for demonstration I have written the following code:

let e = 5;
let f = 6:
let multiply = e * f;
console.log(“Welcome to linuxhint”)
console.log(`The multiplication of ${e} and ${f} is ${multiply}`)

Step 4: Now to run the JavaScript code execute the command given below:

$ node javascript.js

This is how you can code in JavaScript on Raspberry Pi.

Running javaScript on chrome browser in Raspberry Pi

As mentioned above you can also run JavaScript on the browser as well. For that just following the subsequent steps:

Step 1: Open your Raspberry Pi browser from the top menu bar:

From the right click menu click on the “Inspect” option:

Step 2: From there click on the “Console” option or use the ctrl+shift+c shortcut key to open the console of chrome browser, write your JavaScript code in the tab:

For illustration I have just printed linuxhint.com by using the following command:

alert(‘linuxhiunt.com);

Now for demonstration i have run the same code used in the .js file

let e = 5;
let f = 6:
let multiply = e * f;
console.log(“Welcome to linuxhint”)
console.log(`The multiplication of ${e} and ${f} is ${multiply}`)

Once you have pressed enter the the output of the code will be shown in the console as in the image below:

Conclusion

Raspberry Pi is the most liked platform among the programmers because of its compact size and ease in usability. Moreover, like every programming language JavaScript also needs some software for programming especially if it is to be done outside the browser. So, if you are looking to get started with JavaScript on Raspberry Pi then read this guide as it explains the two ways for coding in JavaScript on Raspberry Pi.

Share Button

Source: linuxhint.com

Leave a Reply