louiepi
Posts: 81
Joined: Mon Mar 13, 2017 6:54 pm

JavaScript on Raspberry pi

Fri Oct 06, 2017 9:58 pm

I want to learn JavaScript and can't work out how to set everything up. What is the difference between the text editor and the IDE? What do I need to do to be able to start writing in JavaScript and having the work output to a console. What is p5JS and atom? Is there a thing like atom for raspberry pi which I can use? Thanks for any help.

User avatar
r3d4
Posts: 912
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: JavaScript on Raspberry pi

Sat Oct 07, 2017 6:24 am

What raspberry pi version u haz ?
What distribution is used ?
louiepi wrote:
Fri Oct 06, 2017 9:58 pm
What do I need to do to be able to start writing in JavaScript and having the work output to a console.

install a javascript Interpreter
eg: the nodejs interpreter
with some kind of package manager or build and install from source !

run the javascript Read Eval Print Loop in a terminal

eg : https://nodejs.org/api/repl.html#repl_the_node_js_repl

Code: Select all

$ node
> const a = [1, 2, 3];
undefined
> a
[ 1, 2, 3 ]
> a.forEach((v) => {
...   console.log(v);
...   });
1
2
3
...more nodeJS Usage & Example's + https://nodejs.org/api/cli.html

learn some javascript https://learnxinyminutes.com/docs/javascript/

Heater
Posts: 8151
Joined: Tue Jul 17, 2012 3:02 pm

Re: JavaScript on Raspberry pi

Sat Oct 07, 2017 7:43 am

louiepi,
What is the difference between the text editor and the IDE?
A text editor is a program you use to edit text. I'm sure you know things like notepad on Windows.

An IDE is an Integrated Development Environment. IDE's have all kind of features to help when editing, running and debugging programs. Especially useful when using compiled languages like C and C++.

You don't really need an IDE for Javascript. You can just edit your code in any text editor and then run it. But it can be nice to have syntax highlighting and such features you get with an IDE.

I often use vim for Javascript as it is fast and does syntax highlighting. On a PC Microsofts Visual Studio Code is my favorite way the edit Javascript. I believe VS code now works on the Pi but I have never tried it.

https://code.visualstudio.com/
Install from here: https://code.headmelted.com/
What do I need to do to be able to start writing in JavaScript and having the work output to a console.
Use the node.js Javascript interpretter. It can be install from a package in Raspbian:

$ apt-get install nodejs

Then just run it and you can start typing JS into it as shown in a post above.

For you actual programs edit a file containing you code and run it from that:

$ node myProgram.js

p5js is a library for creating graphics in Javascript running in a web browser. No good for the console programs you say you want to create.

Atom is a program editor written in Javascript. It is also very good. Rather like VS Code. I have yet to hear of it working on the Pi.

louiepi
Posts: 81
Joined: Mon Mar 13, 2017 6:54 pm

Re: JavaScript on Raspberry pi

Sat Oct 07, 2017 11:51 am

Heater wrote:
Sat Oct 07, 2017 7:43 am
louiepi,

You don't really need an IDE for Javascript. You can just edit your code in any text editor and then run it. But it can be nice to have syntax highlighting and such features you get with an IDE.

I often use vim for Javascript as it is fast and does syntax highlighting. On a PC Microsofts Visual Studio Code is my favorite way the edit Javascript. I believe VS code now works on the Pi but I have never tried it.

https://code.visualstudio.com/
Install from here: https://code.headmelted.com
Thanks for the help. So I would have to install form the bottom link above the IDE. Is that the IDE? What is an interpreter. Why is node.js used on pi. And how could I use p5js as I would want to make graphics for the webpage, which I forgot to mention in the original question.

Heater
Posts: 8151
Joined: Tue Jul 17, 2012 3:02 pm

Re: JavaScript on Raspberry pi

Sat Oct 07, 2017 12:50 pm

louiepi,
So I would have to install form the bottom link above the IDE. Is that the IDE?
Not sure exactly. I have never tried it. I just found it with a quick google search whilst replying to your questions. Google is good that way. You could just follow the instructions, download and install what is at that link and see for yourself.
What is an interpreter.
In broad terms there is "compiled" code and "interpreted" code.

With languages like C and C++ the source code that you write is processed by a compiler program which produces a file containing binary code that can be executed by your computers processor directly.

With languages like JS and Python your source code is read by an interpreter. That interpreter then carries out the operations you ave specified. There is no binary executable file.
Why is node.js used on pi
Because Javascript is a very easy to use but sophisticated language. Because Javascript makes it very easy to deal with all kind of events going on in your system at the same time. For example reasing sensors on GPIO pins and juggling input and output from the network at the same time. Because Javascript is far faster than Python.
And how could I use p5js as I would want to make graphics for the webpage,
Why not follow the instructions on the p5js web site?

Personally I would not do that. One can create 2D graphics in JS in the web page easily enough using the HTML canvas. I like to make fast animated graphics in web pages with libraries like Ivank for 2D stuff or THREE.js for 3D stuff. Mind you they are not really viewable in a browser on a Pi as the OpenGL support is not there yet. But they are great for pages served up by a Pi.

http://lib.ivank.net/?p=demos
https://threejs.org/

louiepi
Posts: 81
Joined: Mon Mar 13, 2017 6:54 pm

Re: JavaScript on Raspberry pi

Sat Oct 07, 2017 5:54 pm

Thank you for the help.

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 10 guests