Javascript programming for absolute beginners

Ok, this tutorial will help those who have no background programming knowledge learn Javascript from the very basic.

1. Setting up environment.

There are many JS editors out there but my favorite is Firefox Developer Edition not because it is free, also has many features supported such as CSS, HTML5 and other tools to debug front-end web applications. You need some configuration  to get your JS environment ready for learning purpose.

After downloaded and installed, have your Firefox Developer opened and press Ctrl+Shift+I to open developer tools. Click on setting icon on the far right of developer tool, check Scratchpad, the Scratchpad icon will appear on the developer tool. Click on the icon open Scratch Pad. Under developer tools, click on Console tab.

You will see something similar like this picture.

2. Gettting started with programming in JS (Javascript).

Type in your Scratchpad.

1 var playerName = "Ralph";23 if (!playerName){4  console.log("inside if");5 }else{6 console.log("inside else");7 }89 playerName = "Robo";1011 console.log(playerName);Click on Run button on top of your scratchpad. You will see something like this under Console.

inside elseRoboWhat you just did is programming in JS. Let me explain how JS programming works. After you press Run, the computer reads the very first line and then come down to second line and keep going until it hits no more line or hits a line which tells it to stop running (reading).

The example above, computer reads line 1 first. What this line did is declare a variable. Variable is a place holder for your data. Thinking of it as a box where you stuff your stuffs in that box for for later use. The var keyword in front of playerName is basically tells the computer that hey I want to declare this as variable and I name it playerName. The computer reads the var keyword and does some calculations to preserve memory location for that variable. You don't need to know what computer does behind the scene, you only need to know that, you tell computer you declare a variable with the name playerName.

The computer then reads line 2. In this example line 2 has nothing written down, so computer does nothing, will read to next line 3. This third line the computer reads if keyword, this keyword tells computer start evaluate what inside the parenthesis (!playerName) which I will explain later, you only need to understand how computer reads JS for now. If the inside that parenthesis returns keyword true the computer will read line 4. If return keyword is false, computer will skip line 4 and jump to read line 6. In line 6 basically tells computer to write the words inside else to the screen, in the Console tab. The computer then continue read line 8, 9 ...11 write on the screen the word Robo, end of script.

Ok, simple right. It is not that hard when you know the basic.

2. Variables

Type this in your Scratchpad

// 5 primitive types.String : var playerName = "Ralph";Boolean : var eventSwitch = true;Number : var playerHP = 34;Null : var playerSprite = null;Undefined : var playerMP; // when define variable with no value, undefined will automatically assign.// reference typevar sprite = new Object();In the example above, you wrote instruction to computer by telling the computer that you declare some variables with the var keyword, follows variable's names. The equal sign =  tells computer you want the value to be assigned to that variable. You can think of variable like a box (place holder)  and you need to put stuffs in it otherwise what the point of having the box for!. So on the right side after = those are the values .

 However in JS, every variable has a type associate with that variable. This similar to every box can store the same type of stuffs. There are 6 different types of variables,  5 primitive types and reference type. The 5 primitives types are ; string, boolean, number, null, undefined. The reference type is an object. The different between primitive type and reference type is primitive type stores data while reference type (object) stores reference of object (pointer to memory location) which will explain later.

 

String type is actually a string of characters declare inside of double quotation marks " " or single quotation marks ' ' . Everything inside the marks computer will understand as string value.

Boolean type is either true or false represents to an on or off switches in game. That because computer has only two states 1 and 0. 1 is on and 0 is off. In programming language, these two states represent by boolean true or false . It is important to understand this concept because most of programming instructions written for computer return one of these two values. 

Number type is of math numbers (integers, float, decimal...etc.) and to differentiate with string value it is written without marks. So the computer can differentiate between string of for example "34" and math number of 34.

Null type is empty value preserve for another value to be assigned to that variable. When you declare variable with null type, you basically tell the computer preserve that memory location for your variable however you have not decided to assign any computation value into that variable yet. 

Undefined type is similar to writing instruction but you don't tell computer preserve any memory location for it. The computer don't know the existence of that value. So whenever you get "undefined" from computer  means computer don't know anything about it.
that's for sure man you risk to be really active here when MV will be in sale hahah

it's incredible how much tips you give : D

(and really good ones too!)
One thing that more basic tutorials really need to cover is syntax, not just keywords. I.e, why use brackets? What does !thing mean? Etc
Curly brackets is part of the language definition of JavaScript/ECMAscript. It defines a block of clode partaining to the preceding object. The !thing is universal among programming languages and means the value of thing is flipped. So if thing == true, !thing == false. I recommend going through some beginner courses in JavaScript if this confuses you 🙂
I recommend going through some beginner courses in JavaScript if this confuses you 🙂
Err, that's what this thread is - a course for absolute beginners with zero programming knowledge.  If you have to read other beginners courses elsewhere before you come here, then it will have failed in its mission.  So I think HFL was right to ask his question.
The problem with that is that the core principles of the language are far too broad to cover in a single thread and not make it really hard for readers to take in, in my opinion. It's also been written about extensively in better more readable form elsewhere so many times already. I can warmly recommend Mozilla's JavaScript guide, for instance: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

This kind of thread would be more valuable for getting started with JavaScript in MV, as that will require more specific knowledge, which of course will have to wait until MV is out. Just my 2 cents.
Not being a scripter in any language I cannot have an informed opinion about the quality of different resources.  What might be helpful would be a short list (no more than 3 or 4 items, otherwise it gets overwhelming) of things like that guide so that beginners don't have to guess what might be useful and beginner friendly.
I will just say that learning JavaScript before MV is out is going to feel like an overwhelming task for beginners. The reason is that anything you do in JS now will require you to learn about libraries or functions completely separated from RPG Maker MV. I'll explain why this is so.

The challenge with learning any scripting language in advance of the framework or software it's used in is that so much of how it works is directly tied to its host environment. Anyone that wants to learn JS right now before MV is out must be mindful that how things are done in MV will most likely differ quite a lot from any tutorials they read now, as existing tutorials will either talk about modifying HTML elements or using other libraries for drawing things on the screen, with a host of methods and functions you are unlikely to find (or find useful) in MV.

The reason for this is that JS by itself doesn't do much other than handle basic data types. It was originally conceived as a simple scripting language to modify text and simple HTML objects, and fundamentally speaking it still is. Learning JavaScript without the context of its host environment (for us that'd be MV) offers very little to the one learning it other than fundamental programming knowledge that is mostly universal for all languages. For example, JavaScript is used as one of two scripting languages in the Unity game engine, but writing JS for Unity and writing JS for MV will be very very different.

This is why beginners learning JS in advance of MV will have to be prepared to learn about things like HTML or separate graphic libraries that most likely won't be relevant to MV. It could be, but nobody can say yet. It might sound weird that I say this, since MV will use HTML5 (to be more specific, WebGL in a HTML5 canvas element) to display the game, but I am pretty sure you won't be directly interacting with HTML to draw things in MV; instead you'll work with intermediary functions. These will be unique to MV.

Now this doesn't mean that there is no value in getting into JS now. But I'm afraid there is no beginner friendly way to cover this that is more friendly than the host of well-written tutorials on learning how JS works in browsers today. When we see how MV structures its JS internals and what functions and objects it provide, it's possible to tailor more beginner friendly tutorials in the context of MV.

With that out of the way, I can't really cherry pick anything out of what I linked. But if you want to learn the very basics, I'd start with the following and then proceed to read the rest if you feel like it. The important thing to remember with learning a new language is this: take your time. If you feel frustrated, take a break and return later when you feel up for it.

  1. An introduction to what JavaScript is and the tools used to play around with it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Introduction
  2. The basic grammar of JS, comments, and defining variables: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types
  3. Controlling flow (if...else, switch) and error handling: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling
  4. Loops and iteration (for, while, do...while, for...in): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
I've also heard great things about Codecademy's JS course. I haven't gone through it myself, but it should be solid and for many it might potentially be easier to follow along with: https://www.codecademy.com/en/tracks/javascript

This might seem overwhelming but there really is no shortcut if you don't want to scratch your head at every turn if you just jump into the deep end without knowing the basics of the language.
It is all very well and good saying "read the documentation" but it is often that the docs are not actually written in a newcomer-friendly way. Now that I can write PHP from scratch, docs make sense to me - but far too often when I first started, almost all of them assumed prior knowledge. It was like looking in a dictionary for the meaning of a word, and that very word's definition included the word that I wanted to find the meaning of!

Here is an example picked out really quickly from one of the docs you linked - I don't know if it is actually explained properly elsewhere, but it is a quick example:

The meaning of the term "const":

constDeclares a read-only named constant.That would've told me absolutely nothing as a newcomer.So I scrolled down for more information:

ConstantsYou can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters.And what is a read-only, named constant?(I am assuming at this point it is something that does not change since that is what the word "constant" means, but there is nothing here that is explicitly actually saying this until you get to the next point!)

A constant cannot change value through assignment or be re-declared while the script is running. It has to be initialized to a value.Right, good. So it cannot be changed or redeclared. This means it always stays the same.But then I run into another roadblock:

"initialized to a value"? And then I scratch my head. If I didn't know what initialization was, I'd be looking that up. Also, how does initializing TO a value work? Surely you apply values TO things?

And so on.

I could go on all day, but it's the usage of words, terms and abstract things that really do make a newcomer's head spin. You literally have to assume they know nothing of coding, and cannot even assume they know what "initialization" is. By making people go around in circles with coding documents, you really do lose them quickly, and it becomes very obscure and hard to understand.

I should know, it took me three months of constant work and reading to get to this point where I can actually understand what I'm reading, but I don't think many people have three months worth of time to do so for RM!

I am very tempted to write my own tutorial explaining one concept such as variables to illustrate my point and to explain it to absolute beginners.
Those are good points. But I would assume if people go "what does initialization mean?", they google it and will quickly find good coverage of it even for complete beginners, like wikipedia's article: https://en.wikipedia.org/wiki/Initialization_(programming)

Yes, you could present it nicer perhaps. I linked codecademy too because it teaches you some fundamentals in a friendlier way, but it also--for the sake of simplicity--glosses over a lot of concepts which when it comes to JavaScript can really come back to bite you in the ass later on. Personally I learned programming by going back and forth between documentations and messing about in the language making random stuff. I'm a visual learner so I tried things, saw them fail, tried to figure out why, and repeat. No matter what your approach discipline is important. And JS will be harder to get into than Ruby because of how it doesn't "read" as cleanly. Ruby was written to be a more "readable" language that reads more like human language. it has a nice flow to it and is usually easier for beginners to read. JavaScript is more of a standard programming language, full of curly brackets, declarations and more pitfalls if you don't grasp the fundamentals.
I only read documentations when I look for the functions/methods that the language has and when i'm studying the basic syntax, most of the time I just mess with it and try to make something up...
Learning programming requires patience and commitment and will take months or years continuous learning and practicing before you can be good at scripting. There are many tutorials out there and most repeat the same thing over, most of these tutorials tend to forget explaining the very basic of programming algorithm. This tutorial does not aim to teach you scripting like a pro. The tutorial only help you understand the basic concept so that you can be able to read and edit some variables in you scripts. Or at least get you into the realm of programming so you can have a clue to do research further.

Learning the very basic of programming is very important, because after some months or years you may be found yourself in situation that you need to go back to the very basic of how computer and programming work in order to advance your skills. Be patience and build a strong knowledge of programming foundation is important. It is ok to read the programming basics from many different sources/tutorials again and again. Because each source you read may offer valuable tips that are not in other source  and also help ink the basic concept in your permanent brain memory. I know some developers at enterprise level struggling when solving complex programming problems because they are either forget the very basic or lack of it. They failed because they thought they already know the basics well enough. The basic Im talking about is not something like; if else, loops,....etc. it is the basic of how computer works in correlation with programming algorithms. 

Original thread: https://forums.rpgmakerweb.com/threads/javascript-programming-for-absolute-beginners.43954/