Hi!!! in this article I will be demystifying some basic programming concepts using javascript, if you are just getting started with programming this article is right for you to understand some techniques and foundation of programming syntax, alright let’s learn together.
What is javascript ?
Vanilla javascript is a scripting programming language for the web, javascript was initially created to make web pages interactive, it’s an object-oriented programming language that allows you to implement complex things on the web.
Uses of javascript
Javascript consist of some common programming features that allow you to do cool things like:
- Store useful values inside variables.
- Access any html element in the document and manipulate its look, content, and attributes.
- Handling events that trigger certain actions or features on a web page.
- Passing data or performing actions without refreshing the web page.
- Making the static web page responsive to users actions.
- Enables users to carry out some mathematical operation on a web page.
- And lot’s more cool features and possibilities with javascript…
A brief history of javascript
Javascript was created by Brendan Eich in 1995 at Netscape communications to make the web faster. so javascript was conceived of as a scripting language for the web for client-side. it has become the standard programming language of the web.
Versions of javascript
Javascript versioning brings about some little confusion to beginners and mid-level developers, javascript got the name ECMAScript from its standardization which brings about the acronym ES.
Javascript as an evolving language of the web, lots of improvement has made to the original language that was built within ten days.
The improvement was released in batches to keep track of these improvement each was versioned. ECMAScript 2 released in 1998 followed by ECMAScript 3 (ES3) released in 1999.
And lots of changes have happened over the years which bring about the present version ES2015/ES6 released in 2015. So when you hear ES5/ES6 it’s not a javascript framework, not a library and not a development pattern or a new programming language it just an updated version of the javascript/ECMScript you and I know.
Core concept of javascript programming
This part of the course we going to focus on some key foundation knowledge to aid your understanding of javascript and programming in general, knowing this basis we make it easy for you to learn other programming languages.
Let’s jump in!!
What is “Code”: Let’s start from the root.A program often refereed to as source code or just code is a set of special instructions to tell the computer what tasks to perform and those instruction are made up of lots of statement sometimes refereed to as syntax.Statement: In a computer language a group of words, numbers, and operators that performs a specific task is a statement.Expressions: Statements are made up of one or more expressions, an expression is any reference to a variable and value or a set of variables and values combined with operators.
Variables
A variable is a small portion of memory or a container that stores a particular piece of information it’s an expression that stores current values of other expressions/values.
//Variable example
let greet = “Good morning”;
//display result
alert(greet+” “ + “Mr John”);
Data Types
One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the formats in which values can be represented and manipulated in a programming language.
Javascript support different data types which are:
- number
- string
- boolean (logical type)
- null value
- undefined
- objects
Conditional Statements
The conditional statement is used to decide the flow of execution based on different conditions, javascript support three main conditional statements which are:
The if statement
The if statement is used to execute a block of code only if the specified condition evaluates to true.
if (condition){
//lines of code to be executed
};
The if else statement
The if…else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false.
if(condition){
//code to be executed
}else{
//code to be executed
}
The if.. Elseif..Else statement
The if...else if...else a special statement that is used to combine multiple if...else statements.
if(condition1) {
// Code to be executed if condition1 is true
} else if(condition2) {
// Code to be executed if the condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}
The loop statement
The looping statement or condition is used to execute the same block of code in a specified number of times or while a specified condition is true. Loops are very useful when you have to execute or iterate the same lines of code repeatedly as long as a specific condition is set.
There are mainly two categories of loop:
- The entry controlled loop
In this type of loop the test condition is tested before executing the command on the body of the loop e.g (for & while loop).
For loop: The for loop provides a short way of writing the loop statement, a for loop process different conditions in a line of code thereby making the loop structure shorter.
for(initialization; condition; increment) {
// Code to be executed
}
While loop: The while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition, it just like a repeating if statement.
while(condition) {
// Code to be executed
}
- The exit controlled loop
In these types of loop the test condition is tested or evaluated at the end of the loop command body, therefore the loop body will execute at least once e.g (do while loop).
Do while loop: The do-while loop is closely similar to while loop, but it checks for the condition after executing the statements that is the only difference which therefore makes it an exit control loop.
do {
// Code to be executed
}
while(condition);
Functions
Functions are very important part of programming that help make some features easy to execute and reusable functions are code blocks that mainly use to perform a particular task.
A function is a group of statements that perform specific tasks and can be kept and maintained separately from the main program. Functions provide a way to create reusable code packages that are more portable and easier to debug.
And functions can be executed by invoking it, to create a function in javascript it is defined by a name that can be used to invoke it and the code inside will be executed when it’s called, functions can optionally take parameters and arguments and return value.
Parameters are variables listed as part of the function definition.Arguments are values passed to the function when it is called.
There are different ways of declaring a function in javascript with different versions of javascript and new syntax support.
Let’s start with the “old”, pre-ES6/ES2015 syntax. Here’s a function declaration:
function dosomething(){
//do something
}
function greeting(){
console.log(good morning)
}
Functions can be assigned to variables (it is called a function expression):
const dosomething = function() {
//do something
}
And ES6 introduces a function syntax (called the arrow function):
const dosomething => {
//do something
}
Operators
Operators are used to manipulating values or variables know as an operand. An operator is a mathematical and logical computation on operands and they are also used to compare values or variables, javascript support various classes of operators.
Arithmetic Operators:
An arithmetic operator takes numerical values either literals or variables as their operands and returns a single value.
//Examples of arithmetic operator
//addition +
let num1 = 3;
let num2 = 4;
let totalNum = num1 + num2;
console.log(totalNum);
//subtraction -
let num1 = 3;
let num2 = 4;
let totalNum = num1 - num2;
console.log(totalNum);
//multiplication *
let num1 = 3;
let num2 = 4;
let totalNum = num1 * num2;
console.log(totalNum);
//division /
let num1 = 3;
let num2 =4;
let totalNum = num1 / num2;
console.log(totalNum);
//modulus %
let num1 = 3;
let num2 = 4;
let totalNum = num1 % num2;
console.log(totalNum);
//increment ++
let num1 = 3;
let totalNum = num1 ++ ;
console.log(totalNum);
//decrement --
let num2 = 4;
let totalNum = num2 --;
console.log(totalNum);
Assignment Operators:
An assignment operator assigns a value to its left operand base on the value of its right operand.
//Example of assignment operators
//assignment operator (=)
// it assign a value to a variable
let myVariable = 'Greetings';
//addition and assignment operator (+=)
let myVariable += 300;
//subtraction and assignment operator (-=)
let myVariable -= 300;
Comparison operator:
A comparison operator compares its operand and returns a logical value based on if the comparison is true, the operand can be numerical, string or object values.
//Example of comparison operator
//double equal sign (==)
//It compares the equality of two operand if equal then condition is true otherwise false.
let myVariable1 = 30;
let myVariable2 = 40;
myVariable1 == myVariable;
//triple equal sign (===)
//This operator compares equality of two operand with same type and value then condition is set true if both equal type and value otherwise it false.
//Not equal (!=)
// This compare the inequality of two operand. It's true if are not equal
Conclusion
Okay guys there are still lots of topics to be covered on javascript, sorry I can’t continue everything on this single article don't want to bore you out with lots of content, watch out for the upcoming articles for more exciting content.
please if you are not getting something right don't be in a rush just take your time to look at the error and search the error on google you ain't alone there will be an answer for it or you can reach out or ask someone who codes too.
thanks for the precious time you spent on this article I believe you have learnt something new see yah!!.