Basics of Javascript I
I have been learning Javascript to learn front-end development. Below are the steps I took in learning. I actively utilized https://www.learn-js.org/ website.
History of Javascript:
Javascript, often abbreviated as JS, is one of the core technologies of the World Wide Web. Developed by Brandan Eich, a Netscape programmer, Javascript supports event-driven, functional, and imperative programming styles. Javascript is mainly a client-side language ; It runs on a computer within a brower. However, recent introduction of Node.js has allowed Javascript to execute code on servers.
Basics of Javascript I :
1. Variables and Types:
Every variable in Javascript is defined using the ‘var’ keyword.
var myNumber = 3; // a number
var myString = "Hello, World!" // a string
var myBoolean = true; // a boolean
var myArray = []; // an array
var myObject = {}; // an object
var emptyVariable = null; // null
var questionMark; //undefined
console.log(questionMark); //prints out undefined
2. Array:
Javascript can hold an array of variables that have different types.
var array = [1, 2, 3, 4]; //example array
var sameArray = new Array(1, 2, 3, 4); //same array
var diffArray = ["hi", 9, {}]. //array of different types
To address elements in an Array, use indexing.
var array = [1, 2, 3, 4];
console.log(array[1]); //prints out 3
console.log(array[array.length - 1]); //prints out 4
We can also assign variables to locations where previous cells were undefined.
var array = [];
array[2] = "hi";
console.log(array); //prints out [undefined, undefined, "hi"]
3. Array manipulation:
Arrays in Javascript can function as a Stack using ‘push’ and ‘pop’ methods.
var array = [1, 2, 3];
array.push(4);
array.push(5);
console.log(array); //prints out [1, 2, 3, 4, 5]
array.pop();
console.log(array); //prints out [1, 2, 3, 4]
Arrays in Javascript can also function as a Queue using ‘shift’ and ‘unshift’ methods.
var array = [1, 2, 3];
console.log(array.shift()); //prints out 1
console.log(array.shift()); //prints out 2
console.log(array); //prints out [3]
array.unshift(0);
console.log(array); //prints out [0, 3]
Arrays in Javascript also support splicing, which removes a certain part from an array to create a new array, made up from the part we took out.
var array = [0,1,2,3,4,5,6,7,8,9];
var splice = array.splice(3,5);
console.log(splice); // prints out [3, 4, 5, 6, 7]
console.log(array); // prints out [0, 1, 2, 8, 9]
4. Operators:
Addition in Javascript is used for addition and concatenation of strings.
var a = 1;
var b = 2;
var c = a + b; // c equals 3
var hello = "Hello";
var word = " World";
var full = hello + word; //full equals "Hello World"
Other Mathematical Operations:
var a = 5;
var b = 1;
var c = a - b; //c equals 4
console.log(a * b); // prints out 5
console.log(a / c); // prints out 1.25
console.log(a % b); // prints out 1
Javascript also has Math module.
Math.abs
calculates the absolute value of a numberMath.exp
calculates e to the power of a numberMath.pow(x,y)
calculates the result of x to the power of yMath.floor
removes the fraction part from a numberMath.random()
will give a random numberx
where 0<= x <1
Basics of Javascript I is to be continued in the next article Basics of Javascript II.
Stay Tuned!