JavaScript definition
JavaScript (JS) is a prototype base, high level, interpreted, multi-paradigm, single-threaded dynamic. To break all this down, JavaScript being prototype base means it allows machine of object without first defining its classes. It has lots of abstraction so you don't have to deal with memory management and co as a high-level language, by interpreted language it means it runs without compilation and can be written as either an object oriented or functional programming as a multi paradigm language. JavaScript also runs only one line of code at any given time as a single threaded language and finally, as a dynamic language; the interpreter assigns a type of variable at runtime based on the variable value at that time
uses of JavaScript
Developers use JavaScript to build complex interactive websites and browser games, and to connect servers to websites and web applications. Because of this versatility, it's easy to see why this language is the most commonly used programming language in the world, so basically we can use JS for; Web development, Web applications, Mobile applications, Game, Presentation, Server applications, Web servers e.t.c .
History of JavaScript
JavaScript was invented by Brendan Eich in 1995. It was developed for Netscape 2, and became the ECMA-262 standard in 1997. After Netscape handed JavaScript over to ECMA, the Mozilla foundation continued to develop JavaScript for Firefox browser. Mozilla's latest version was 1.8
Fundamentals of JavaScript
In order to know and use JavaScript, you need to master its syntax. This fundamentals comprises of; variables, datatypes, operators, flow control, loops and functions
variables
In every programming language, variables are used to store data(values) temporally in the computer's memory, it is also a pointer to a data stored in the computer's memory. There are three keywords for declaring variables in JavaScript; the var, let and const keywords
Difference between var, let and const
Let and const keywords came the new edition of JavaScript, so var was already there. Speaking about their difference; var variables are function-scoped, while let and const variables are block-scoped. Also, a const variable can't be redeclared
sample code
Var variable1="hello world";
Let variable2="hello world";
Const variable3="hello world";
Console.log(variable1, variable2, variable3)
Output: hello world hello world hello world
Note: console.log(...) Is used for outputting our code result and every line of code should end with a semicolon (;).
We have what is called concatenation in JS, it's for outputting a massage and various variable at once using the plus sign(+) Example:
Const name = "John";
Const age =34;
Console.log("my name is "+name+", and I am"+age+" years old")
There is also what is called commenting in JS, when you comment, it means the interpreter will skip the code. We have single line and multiple line comments, the double forward slashes(//) is for single line while the forward slash and asterisk (/..../) is multiple line. This also help in code readability
datatypes
Datatypes determines the type of data to held by our variables, it is a classification of data which tells the interpreter how the programmer Intends to use the data. We have the primitive and none primitive datatypes
primitive datatypes
In JS, a primitive (primitive value and datatypes) is data that is not an object and has no methods or properties. We have the following under the primitive datatypes;
string
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols
code sample
Let name = "ifedili";
Note: string values are written with double quotes ("")
number datatype
This is made of both integers and floating point numbers, it stores numbers in particular
Code sample
Let numbers= 12345;
boolean
In JavaScript, a boolean holds a value that can either be TRUE or FALSE. If you need to know "yes" or "no" about something, then you would want to use the boolean function
code sample
Let answer= true;
undefined
This deals with empty values, a variable that has not been assigned a value is of type undefined. A function also returns undefined if a value was not returned.
code sample :
Let emptyvalue=
Or
Let emptyvalue=undefined
non-primitive datatypes
Non-primitive datatypes are also called reference datatypes or object references as they reference a memory location where data is stored. This consist of arrays and objects e.t.c
Arrays
An array is an object that can store multiple values of the same datatype at once
Code sample:
Const words= ["hello", "world", "welcome"];
Note:. Use the square brackets ([]) to list your arry value. The above array is an array of string, do we equally have array of other datatypes
Objects
An object is a standalone entity, with properties and type . We use objects to hold a group of data(key as the variable and value)
Code sample: Const person={ name: "John" age: 34 istall: true address:( Street: no 4 genesys city: Enugu ) }
//To output the values of keys
console.log(pesorn["name"])
//To get the value of a sub object
console.log(person["address"]["city"]) //or
console.log(person_address.city) We can have sub object in an object such as address in the above code.
operators
Operators is a special symbol used to perform operation on operands (values and variable)
arithmetic operators
This operators are used for performing arithmetic operation like addition and subtraction:
Addition; +
Subtraction; -
Multiplication; *
Division; /
Modules; %
Exponential; **
Code sample:
Console.log(1+23*4/5)
comparison operators
Greater than;>
Less than; <
Greater than or equal to; >=
Less than or equal to; <=
Equal to; ==
Not equal to; !=
Code sample:
console.log(2<3, 4==5)
logical operators
Not; !
And; &&
Or; ||
Null coalescing; ??
Code sample:
console.log(1>2 && 3=20)
flow control
This is a way to control the flow of codes and telling the program to do this if a particular condition is true/false. To do this, we use the following functions:
if else conditional statement
The if else conditional statement takes in conditions to control our code.
Code sample: Let i=0 If(ii//condition){ console.log("hello") }else{ console.log("no compliment")//this will run If non of the condition is true }
Note: the else serves as default if none of the conditions are true
Switch
This different from if else, this takes a parameter and compares the parameter with each case (it's made up of cases)
Code sample:
Const dayoftheweek=4; Switch (dayoftheweek){ Case 1: console.log (week day); break; default: console.log(weekend);
Note: the default will run if none of the cases are true.
loops
In every programming language, loops are used to print a cidr multiple times, there are three stages in a loop; the initialization, condition and increment/decrement
for loop
This is most common loop in programming language, like I said earlier it comprises of initialization, condition and increment/decrement.
Code sample:
For(let i=0//initialization;i<3//condition;I++//increment/decrement){ Code to multiply }
while loop
The while loop serves the same function as the for loop but has segments
Code sample: Let i=0
While (I<3//condition){ console.log("hi")//code multiply }
Note: code to run follows the condition and this will determine how many times the code will appear.
We have other loops but this is the basic ones.
function
This is last we are going to be treating, it is a block of code that takes in an input and returns an output. A function in js is noted by the function keyword followed by brackets where our parameters (variables to be used in our function) will be and a braces where our code will be Code sample:
Function add (x, y){ return x+y }
Note: "add" is our function variable name and we are to return a value using the keyword "return".
Arrow function
This is just like a shortcut for writing function in js it's called the arrow function because it comes with "=>" and doesn't have the function keyword.
Code sample:
const add (x,y)=>{return x+ y} console.log(add)
conclusion
This are the fundamentals of JavaScript with this you can get started with JS. JavaScript comprises of a lot, you need to go further in order to master JavaScript very well.