[Tutorial] Javascript Basics [Part 1]
Introduction:
> Javascript is client-side dynamic programming language. It syntactically resembles Java but being a scripting language such as VBS it is being interpreted rather than compiled. In Javascript every procedure is a function no matter whether it returns a value or not. Also, Javascript is case-sensitive which means that, for example, “A” is interpreted in one way and “a” in another.
Variables:
In Javascript we declare variables with the keyword var. You can also use variables without the need of declaring them:
var glblExample;
var glblExample = 1;
With the first one we simply declare the variable and with the second line we both, declare and assign an initial value for it. Take a loot at the glbl string we placed before the name of the variables. We can use it to define the scope of the variable as global which means we can use it anywhere in our code. So as for the variable to be valid in the whole code, it needs to be defined as global on the top of the code before any other functions. It is also not necessary to define the data type for the variables.
There are three types of variables:
[list]
[]Integers - containing numeric values (e.g. 4,88,33)
[]Strings - containing alpha values (e.g. keeper, hackforums)
[*]Booleans - containing logical operators (true() and false())
[/list]
During your process of coding you can assign different types of data for the variables, changing their subtype. For example:
var Firstone = 7;
var Secondone = 19;
var Both;
var Both = Firstone + Secondone;
The result of this will be 26. But let’s change the type of the first variable to string to see the output we’re gonna get.
var Firstone = "7";
var Secondone = 19;
var Both;
var Both = Firstone + Secondone;
Converting the first variable to a string value we’ll get the following: 719. Because of that, Javascript automatically converts the second variable to a string as well and the result will be just a merge of the values from both variables. This is called concatenation.
Creating objects:
The creation of objects in Javascript is done with the keyword new. The format is the following:
var Example = new ClassName;
This line directs Javascript that we are creating a new instance of the class and assign her the name ClassName. In Javascript, strings, integers and arrays are objects. When we create a new instance for one of those types in the following way:
var Example = new String('Keeper');
this is identical to a variable declared like so:
var Example = 'Keeper';
Arrays:
Arrays are objects that allow you to store a number of variables into a single one. In arrays you can assign the values to the variables and refer to them without the need of defining a new name for each one.
Simple Arrays:
Every array is an instance for the class Array. As we explained above, we create classes with the keyword new. Let’s take a basic example of an array:
var Hackers = new Array(4);
Hackers[0] = 'Kevin Mitnick'
Hackers[1] = 'Robert Morris'
Hackers[2] = 'Steve Wozniak'
Hackers[3] = 'Adrian Lamo'
Note: In Javascript indexing begins from 0 NOT from 1.
You can also declare the elements in the array the following way:
var Hackers = new Array('Kevin Mitnick', 'Robert Morris', 'Kevin Paulsen', 'Adrian Lamo');
Complex (Multi-dimensional) Arrays
Multi-dimensional arrays are arrays containing arrays. Let’s say, we’ve decided to make our array a little bit more complex by adding another column to it, showing whether the hacker is blackhat, whitehat or grayhat. To create a two-dimensional array, we’ll consider each line as a new array.
var Mitnick = new Array('Kevin Mitnick', 'Blackhat');
var Morris = new Array('Robert Moriss', 'Blackhat');
var Steve = new Array('Steve Wozniak', 'Whitehat');
var Lamo = new Array('Adrian Lamo', 'Grayhat');
var Hackers = new Array(Mitnick, Morris, Steve, Lamo);
Operators:
Below are the comparison operators:
[list]
[]== equality
[]!= inequality
[]< less than
[]> greater than
[]<= less than or equal to
[]=> greater than or equal to
[/list]
Below are the logical operators:
[list]
[]! NOT
[]&& AND
[*]|| OR
[/list]
Conditions:
If
The construction and purpose of the operator if is more than obvious.
if(condition){
construction 1;
construction 2;
}
If the value of the condition is true, both, construction 1 and construction 2 are being executed. If not, they don’t.
Else
Else is an expansion for if. It defines the code, that is going to be executed if the value of the condition is false.
if(condition){
construction 1;
construction 2;
}
else {
construction 3;
construction 4;
}
For
The cycle for is a fundamental way for organising the repetition of the programming code.
function Keeper(){
for(a = 0; a < 5; a++){
document.forms[0].elements[0].value =
document.forms[0].elements[0].values + '\n' + "The value of a is: " + a;
}
}
Upon execution of this code you will get the following output:
The value of a is: 0
The value of a is: 1
The value of a is: 2
The value of a is: 3
The value of a is: 4
While
The purpose of the cycle while is the same as the one of for.
function Keeper(){
while(a < 5){
document.forms[0].elements[0].value =
document.forms[0].elements[0].values + '\n' + "The value of a is: " + a;
a ++;
}
}
The result will be the same as the one for the previous code.
Do while
do while guarantees at least a single repetition of the code. In this cycle the condition is placed after the code which is to be executed.
function Keeper(){
do {
document.forms[0].elements[0].value =
document.forms[0].elements[0].values + '\n' + "The value of a is: " + a;
a ++;
}
while (a < 5)
}
Conclusion:
I tried to put it as most noob friendly as I could. I will for sure make a part 2 for the embedded objects and other stuff. For any questions leave a reply with them. Thanks for reading!
Nice article, just one little thing i thought of that can be quite good to keep in mind. The “==” and “!=” operators does not compare by type in javascript, and generally it’s a good idea to use “===” and “!==” instead.
For example, and empty string or array in javascript is concidered false if not compared by type.
```// Both these two statements are evaluated to true
“” == false
“” == []
// But if compared using the === and !== operators instead, they are both false
“” === false
“” === []```
print(", ".join([str(x) for x in range(1,100) if not [y for y in range(2, x) if x%y==0]]))
11 years ago
0
Hi keeper and Verath. Nice Article Keeper!
Good clear read. :)
[quote=“Keeper”]Take a loot at the glbl string we placed before the name of the variables. We can use it to define the scope of the variable as global which means we can use it anywhere in our code. So as for the variable to be valid in the whole code, it needs to be defined as global on the top of the code before any other functions.[/quote]
Not entirely true and the way you phrased it can be confusing to beginners.
You can use local variables inside functions as well by declaring them as global in the function using global variableName; or simply by passing it as an argument.
“[T]o be valid in the whole code” sounds like it suddenly stops working halfway through the execution. A better way to phrase it could be “To be valid outside the local scope, for example inside functions.”
[quote=“Keeper”]Booleans - containing logical operators (true() and false())[/quote]
Why are you using parentheses after the booleans? Surely they are not functions by any stretch.
Also, booleans do not contain logical operators (==, !=, >, < etc.) but rather logical values.
[quote=“Keeper”]Note: In Javascript indexing begins from 0 NOT from 1.[/quote]
You make it sound like the base index is usually 1, when in fact every major language (except LUA, if you count it as one) uses a base index of 0.
Perhaps phrase it as “Note: In Javascript and every major language, indexing starts from 0.”
In the For, While and Do While chapters you put them all inside a function, why?
You haven’t described what functions are or do yet so it can be really confusing to a beginner reading this.
Also, not only is it more difficult than it should be to use “document.forms[0].elements[0].value” to write output and it doesn’t even work if the HTML doesn’t contain a form with inner elements, but you also use “document.forms[0].elements[0].values” even though “values” it not a valid property of a DOM element. Just use document.write() or document.body.innerHTML.
In all of these loops you should probably explain how it works, especially the for-loop. For example, explain that there’s three arguments to the for-loop separated by a semicolon. The first one declares variables at the beginning of the loop. The second one is a condition which, when it evaluates to true, stops the loop execution. The third one declares what should be done after each iteration, in this case increment a by 1.
You might also want to explain the ++ operator.
Otherwise, good article. ^.^
Very nice @Keeper, another great post!! @Pseudonym made some good points, especially about the explanation of loops.
Thanks for the nice post Keeper, I’m not too good with JavaScript so this helps, definitely an area I need to improve on :D
I would love to change the world, but they won’t give me the source code.
For those who like myself, have not read [Tutorial] Javascript Basics [Part 1] by Keeper, let me refresh. Why? First , because it was very useful (Coding Level), once verath show me the way, and second [Tutorial] Javascript Basics [Part 2] is available. Once Coding Level complete check out [Solution discussion] to see what I mean! I would add the [Solution discussion] is in my opinion an underused resource for new/inexperienced hackers. Great Job Keeper!!
I’d rather see folks doubt what’s true than accept what isn’t.
Hmm , some nice basic tut there by keeper !!!
And the same keen cross-checks there by pseudonym !!! Well done
- @IAmDevil
Its good to be back! :D
When solving problems, dig at the roots instead of just hacking at the leaves.
No need to say thank you to everyone as it is just spamming the forums, if you can add to the discussion then that would be great, but please don’t post the same thing in every thread. Thanks.
I agree with @xzy123prog this time. Simple thanks all over the place can be considered as spamming posts.
10 years ago
0
I often wonder why this was not just made as an article and left at that.