please dont rip this site

JavaScript Tutorial Basics

JavaScript is a programming language: A way for humans to order computers around. A human who has become a "programmer" can order computers to do useful things for themselves and others. Really good programmers write new languages which become widely used.1. Today, JavaScript is called ECMAScript^ and runs in the V8 engine^ in Chrome^, Node.JS^ and now Microsoft Edge, WebKit^ / Nitro in Apple's Safari^, and SpiderMonkey^ in Firefox^.

Not Java: JavaScript has nothing to do with Java. 1 It's more closely related to ( LISP ), C, and Pascal.

Syntax: Like any language, JavaScript has a "syntax" you have to follow. When Brendan Eich1 wrote the language (in only 10 days!1) he was attempting to build something that could understand the idea you are trying to type in and then turn it into code for the computer. There are many (many) different ways to express the same idea in human languages. He didn't have time to figure out every way you could mess up typing something or every way you could express your idea. Syntax is like grammar in human languages; you may understand what you said, and your best friend may understand you, but you have to explain what you mean in a grammar Brendan's mind understood so his JavaScript language program can translate it to the computer.

Statements: Each line, or part of a line terminated by a ";" is a statement. These are the "sentences" of JavaScript; they express one basic idea per line. Examples:

There are a zillion possible statements, but only a few core words from which most are built. Good IDEs (Integrated Development Environments) can help you figure out what words are available by "hinting" them as you type:

Keywords: The words JavaScript knows. These are the entries in the dictionary of JavaScript. Here are most of the common JavaScript Keywords:
break case catch const do else eval false finally for function if in isNaN length let Math NaN name new return switch this throw true try typeof undefined var while with

This is not a complete list, and there are a bunch more related to documents in browsers and other special stuff. Notice some are standard english (if, false, true) and actually mean pretty much the same thing. Some are English words that don't mean the same thing (for, throw). Others are made up words (var, NaN). A few are two words crammed together (typeof, isNaN).

Also, case is important in JavaScript. Math (object prototype with math functions) is not math (doesn't exist). NaN is not nan or NAN. Syntax. Or else. (Use the IDE, Luke to hint which one to use). But even then, Document is not document and each is valid.

Blocks "{ }" Some keywords need to work with other keywords. By default, they only work with one other keyword. e.g. if (false) this_dont_happen(); but_this_still_does(); So if you need to work with more than one, you put them in a "block", meaning you put { } around them. if (false) { this_dont_happen(); and_this_dont_either(); }

Variables: Names you give to pieces of the computers memory. They can be thought of as boxes in the memory closet, with names you use to label the front of the box, and values which are stored inside. JavaScript has specific types of boxes, of specific sizes, for specific uses. For example, there is a type of box for numbers, and one for text (strings, messages, etc...), etc... And it will figure out what type to use automatically... but it will switch from one sort of box to the next without telling you. And it will select the type based on it's best guess, which can get a bit wierd. e.g.

var myvar = +"12" //this is now a number, and it's value is 12
var myvar = "12"*1 //this is now a number, and it's value is 12
var myvar = "12"+0 //this is now a string, and it's value is "120"

You can use typeof() to find out what type it picked.

Scope: Variables "have scope" which means they can be hidden from different parts of the program. A "global" variable is visable everywhere in the program. But a "local" variable is only accessable in the local part of the program. This is really good when programs start to get large and you forget that you already used a variable name for something else and accidentally overwrite it's value.

let makes global variables or local variable if it's in a block { } scope or in any parameter list related to the block. Even if you use the same name for the new local box that use used for the global box, it just makes a new box, and puts the prior box with the same name away for later. When you leave the block, it brings the old box, with that name, back. E.g.

let i = 123
for (let i = 0; i < 2; ++i) { 
  console.log("i is:"+i); 
  //i is 0 then 1 inside this block
  }
console.log("i is:"+i) 
//i is 123 after the block

var makes global variables outside a function, or local variables inside a function, not just inside a block. That gets weird if you make a var part way down in a function, because the name of the box will be known all through the function, but it will only have the value you set from there down. To avoid that... just don't use var. That's why they replaced it with let.

const is just like let, but for two things: You must assign a value and you can not change that value. It's a constant. If you try to change the value after you set it, you get an error and the program stops.

Scope gets really complex and causes a lot of problems. It's good to take the time to learn it fully:

Objects: Almost everything in JavaScript is actually an object. e.g. myvar above is an object of type string or number. Objects are basically boxes of boxes. So there is a primative value like a string or number, but there are also other named boxes in the variable box and those boxes keep track of things like what type of box it is, how to do stuff to the value in the box, and so on. e.g.

myvar.length //tells us how long the value in myvar is. 
myvar.toString //contains a program that will convert myvars value to a string.
myvar.toString() //gives us the result of calling that program; the value as a string

Prototypes: Special objects are templates for making new objects with the new keyword. By convention, these "prototypical" objects are named with a single capital leter followed by lower case, and the objects created from them are all lower case. The document object has a prototype called Document. A more useful example is let my_array = new Array.

Functions: Functions take in parameters, (which automatically have local scope) then do stuff to them with a program and return values. Functions are also just objects like anything else, and can be stored in other objects, or have other objects or variables stored in them. Variables declared in a function have local scope. var makes one for the entire function, let for the local block.

Methods: You can put a Function inside an Object, with special access to the other junk in the object, and then call it a Method. It's a nice way of organizing things; all the stuff related to the object is in the object: Data, Functions, other objects, everything. The methods (functions in the object) can even be used to change things inside the object that we can't otherwise change; variables which are out of scope. This can be really good because the methods can check the changes we ask for and make sure they are valid. For example, Arrays know how to sort their elements.

See also:


file: /Techref/language/java/script/basics.htm, 12KB, , updated: 2020/1/6 20:16, local time: 2020/1/22 16:37,
TOP NEW HELP FIND: 
18.210.24.208:LOG IN

 ©2020 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF="http://techref.massmind.org/Techref/language/java/script/basics.htm"> JavaScript Basics</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .