Into Programming

Building blocks of programming

Let’s start from beginning. What is Code? Code, a program, is set of special instructions to tell computer what to do? And the rules and valid format to write these programs is language. Language forms statement. Similarly in software development set of words, numbers and operators that performs specific task is statement.

a = b2; Here a and b are variables which hold some values, = is assignment operator and is multiplication operator and 2 is literal value. This statement says that multiply the value whatever is stored in b with 2 and assign it to a.

Now, this code is in human readable form but it needs to be converted into machine code so that machine can read it. The translation is typically done from top to bottom, line by line every time the program is run which is called interpreting the code. But for few language translation is done before hand then it executes. So when we run our code it’s already compiled code ready to go. Javascript code is processed each time it’s run but JS engine compiles the program on fly then immediately runs the compiled code.

Screenshot 2022-08-23 at 6.17.32 PM.png

Operators:- Operators are used to perform operations on variables and values like addition(+), multiplication(*) operator.

a = 2;

b = a+1;

console.log(b); //3

b = a*2;

console.log(b); //4

Assignment operator :- =

Math operator :- +, -, *, /, %

Compound operator :- +=, -=, *=, /=

a = 2;

a+=3;

console.log(a); //5

a-=3;

console.log(a); // 2

a*=2;

console.log(a); //4

Increment operator :- a++ == a+1

Decrement operator :- a — == a-1

Equality operator :- == (loose-equal), === (strict equal), != (loose not equal), !== (strict not equal)

Comparison operator :- < (less than), > (greater than), <= (less than or equal to), >=(greater than or equal to)

Logical operator :- && (and operator), || (or operator)

a && b (a and b both should be true) a|| b (one of a or b should be true)

Values and types :- to perform any operation we need values and every value has their type like if we want to perform some mathematical operation we need numbers, to print something on screen we need string, to make a decision we need boolean value either true or false.

Example :- var a = ‘I am string’;

console.log(typeof a); // string

a = “I am also string”;

console.log(typeof a); //string

a = 42;

console.log(typeof a); //Number

a = true;

console.log(typeof a); //boolean

Code comments :-

Suppose we have a shop and we want to maintain info about products for our employee for ease of accessibility so that future employee can read that info and can get the details of each item present in our shop. These details are not for customers but it’ll surely help our employees. Like that only we need to write comment in our code so that future developers can understand what we were trying to do while writing a piece of code.

Because it’s not just about writing the code that works we should strive to write code that makes sense. Our comment should explain why instead of what.

// this is single line comment

/* this is

Multiline comment

*/

var a = 10; // to assign age in variable

/ the following value is used Because it answers every questions in universe /

var a = 10;

Block :- Block is used to group a series of statements. In JS, we use curly braces {} to wrap series of statements. These curly braces form a block.

var amount = 99.99;

{

amount*=2;

console.log(amount); //199.98

}

Condition :-

Now suppose we wanna buy a mobile so we need to have bank balance equal to or greater than price of mobile. So, we’ll check this condition then only we’ll buy mobile.

var bank_balance = 300;

var mobile_price = 120;

if(bank_balance>=mobile_price){

console.log(‘you can buy mobile’)

}

Now if bank balance is lower than price of mobile but we want to walk out of shop.

var bank_balance = 100; var mobile_price = 120; If(bank_balance>mobile_price){ console.log(‘You can buy mobile’) } else{ console.log(‘Let’s earn some money’) }

Now if we have money less than price of mobile but more than earphone price and want to buy that we can add extra condition to buy earphone too.

var bank_balance = 100;

var mobile_price = 120;

var earphone_price = 80;

If(bank_balance>mobile_price){

console.log(‘You can buy mobile’);

} else if(bank_balance>earphone_price){

console.log(‘You can buy earphone’);

} else{

console.log(‘Let’s earn some more money’);

}

Like this we can add multiple else if in between to handle different conditions.

Loops :-

Suppose we want to keep buying mobile phones till our bank balance hits zero. There is repetitive process going on we can handle this using loop condition.

var bank_balance = 1000000

var mobile_price = 60000;

While loop :-

while(bank_balance>=mobile_price){

console.log(‘Buy a mobile’);

bank_balance -=mobile_price;

}

For loop :-

For(var value=bank_balance;value>=mobile_price; value=bank_balance-mobile_price){

Console.log(‘Buy a mobile’);

}

Function :-

Now we buy mobile one by one until unless our bank balance hits a boundary and for all these purchase mobile shop employee would like to carry a computer to calculate purchase amount and give you the remaining amount. It should be a task which should be defined once and can be used frequently whenever employee wants. This is where functions come in to picture. We will create a function to calculate purchase_amount and remaining amount and print. And will use the same function and call it on every purchase.

var bank_balance = 1000000;

var mobile_price = 60000;

while(bank_balance>=mobile_price){

console.log(‘Buy a mobile’);

getTaxDeductedAmoutRemainingAmount();

}

getTaxDeductedAmountRemainingAmount(){

console.log(‘Spent Amount’, mobile_price);

console.log(‘Remaining Amount’, bank_balance-mobile_price );

}

Lastly here are some building blocks of programming that we learnt in this blog :-

  • We need operator to perform any operation
  • We need value and variables to store data and to be operated on
  • We need conditional like if to make decisions
  • We need loops to repeat a talk until certain condition met
  • We need function to organise our code in logical and reusable form

This is summary of first chapter “Into Programming” of You don’t know JS : Up & Going. Which is first book in 6 volume series of You Don’t know JS written by Kyle Simpson. I’ll be writing blog on each chapter of this series of 6 books. If you liked it please give it like, add your comment/feedback and share it among your friends & peers.