Basic JavaScripts Building Blocks

Annoydey
4 min readNov 2, 2020
  1. Number — In JavaScript conversion of numbers are slightly different. Numbers in javascript theirs is no such things like int, double, float just like other programming language. On the other hand if we want to convert string value to integer value then we can use parseInt() function. Syntax is like :- parseInt(string, radix).Here string means the string value which we want to be parse and radix it represents the base number used in mathematical systems (it’s optional). Example :-
var text = '42px';
var integer = parseInt(text, 10);
returns 42

If the value of number is non-numeric it shows NaN. NaN means Not a Number. On the other hand if we want to convert string value to point or decimal number then we have to use parseFloat(). Example:

var text = '3.14someRandom things';
var pointNum = parseFloat(text);
returns 3.14

Moreover, there is Number() function which is is used for convert string to number. Example:

Number('123');
// returns 123

2. String — In JavaScript if we want to use string then no need to declare “string” type or “char” type variable we may just use var x = “Hello World” . Here, x is variable and inside quotes we use strings.

There is a build in property which is called “length”. It is used for finding length of the string. Example:

var string = “abcdefg”;
var strlen = string.length;

3. Boolean — Javascript has Boolean type which contains only true and false.Besides this we can convert any value to Boolean. Here,

1. false, 0 , empty string (“”), NaN, null and undefined all are “false”.

2. Others values are “true”.

4. Variables — In JavaScript we have three type of variables keywords : let, const or var. “Let” is used when value of variables changes several time and variables value are not fixed. And it also a block level variable. For example :

for (let Variable = 0; Variable < 3; Variable++) {
// Variable is only visible in here
}
// Variable is *not* visible here

“const” is used for declare variables whose values are not changed in future. Example :

const x = 5.16; // variable x is set
x = 0; // here you cannot change a constant variable.

5. Operators — JavaScript operators are +,-,*,/ and %. “=” sign is used for assign a value.Not only that it also had a compound assignment statements such as “+=” and “-=” Example:

Y = Y + 5; here it can be written as  Y += 5;

We can use “++” as increment and “ — ” as decrement. Moreover, we may use “+” sign as concatenation for example:

‘hello’ + ‘world’;
//And the output will be “hello world”

On the other hand we can also add number and string with “+” operator but it will convert as string

X = ‘5’+ 4; //output will be 54

We can use “==” sign for comparisons. To avoid type coercion we may use “===” triple equals sign for comparisons. Not only that there is also “!=” and “!==” sign which means not equal sign , it works opposite to “==” and “===” sign.

6. Control Structure — Control Structure of JavaScript are similar to other programming language control structure like C.

First :- “if” and “else” is used for checking the condition. For example:

var x = 'aa';
if (x == 'bb') {
x += 'w';
} else if (x == 'k') {
x += 'm';
} else {
x += '!';
}
x == 'km';

Second :- “While” , “do-while” , “for” all these are use as loop. Example:

while (true) {
// an infinite loop!
}
var x;
do {
x = get_input();
} while (inputIsNotValid(x));
For loop is given below: ------------for (var i = 0; i < 5; i++) {
// Will execute 5 times
}

7. Switch, Case, Break — “switch” can be used for multiple branches based on string. “case” is used for choosing which case value is matched by switch and then enter to that case after execution complete it will terminate because of using ”break” and give the result according to that case. Here, “default” is used for if any of the case statement is not matched then it will enter to default. For example:

switch (action) {
case 'draw':
drawIt();
break;
case 'eat':
eatIt();
break;
default:
doNothing();
}

8. Function — JavaScript function is similar as other programming language like c , c++ . Example :

function add(x, y) {
var total = x + y;
return total;
}
// we are sending 1 to x and y to 2
add(1,2) // in this way we can send arguments to add function
// it returns 3

9. Array — It is special type of object and it has also an index. And index starts from 0 to …. Apart from this you can also call specific index value for example :

var a = ['dog', 'cat', 'hen']; // suppose this is an arraya[0] = 'dog'; // here, at 0 index the value is ‘dog’
a[1] = 'cat'; // here, at 1 index the value is ‘cat’
a[2] = 'hen'; // here, at 2 index the value is ‘hen’
//** Remember you can’t call a[3] because it’s index is start from 0 to 2a.length; // but its length is 3

And array length is always “+1” to value of index. Like if array index is upto 3 then length is (3+1) = 4 , again array index is upto 10 then length will be (10+1) = 11.

If we want to add or append items to an array then just simply push to an array. Use — “a.push(item);”

10. Inner Function — It’s like a nested Function. In this function there is one parent function and other one is child function. However, parent function can’t access child function variables others things are similar as normal function. For example :

function parent_Function() {
var a = 6;
function child_Function() {
var b = 6; // parent_Function can't use this
return a + b;
}
return child_Function(); // 12
}

--

--

Annoydey
0 Followers

Web Developer and Programmer