Five Java Script Fundamentals that you need to know.
1.variable
For those of us who are completely new to programming, the first thing we need to know that what is variables? You can imagine that variable is a kind of box that you can declare any right place for your needs and can save any kind of value(array, string, number, bullian, object, function) inside this variable. for example, you are saying that “Mc Jhon is a good boy. but he has a lot of girlfriends”. Now follow here, you first said ‘Mc Jhon’ name, but the second time, you didn’t say his name. but every man who listens to you can understand that in the second you also mentioned Mc Jhon. Yes variable is some like this name(‘Mc Jhon’)
example: var FullName = ‘Md Khalid Hossain’;
here the ‘FullName’ is a variable which I declared.
Variables work just like this. And whenever you call a variable wherever you need it, you will get its value.
But before you can take a value of the variable, you must first declare it. Variables in JavaScript are declared with the var/const/let keyword.
2.Array
Array can be more than one element. And array declare as,
const numbers =[25, 24, 21, 20, 12, 13];
const respectedPerson = [‘Abdur Rouf’, ‘Jahangir’,’Motiur Rahman’];
const products = [{name: ‘Laptop’, price: 500},{name: ‘Mobile Phone’, price: 300},{name: ‘Watch’, price: 120} ];
3.Loop
Two kinds of loops have in Java Script.
Today I discuss only about for loop. If you have many elements inside an array and you want the value of these elements then you should through a loop.
syntax of for loop:
Here let index = 0, means the default position number of the elements is 0, index< array.length means The number of elements inside an array indicates its first to the last position & index++ means every time the index number will increase one(1).
Example: const products = [{name: ‘Laptop’, price: 500},{name: ‘Mobile Phone’,}];
Products is a bundle of some Product. now I want to know the total of how many products has inside the array. So at first, I should go inside the array. The accessible way to go to array through a loop.
4.JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it).
JavaScript Function Syntax:
- A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
- Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
- The parentheses may include parameter names separated by commas:
(parameter1, parameter2, …) - The code to be executed, by the function, is placed inside curly brackets: {}
5. Function parameters are listed inside the parentheses () in the function definition.
6. Function arguments are the values received by the function when it is invoked.
7. Inside the function, the arguments (the parameters) behave as local variables. more about function
Function Return 🤞
When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will “return” to execute the code after the invoking statement. Functions often compute a return value. The return value is “returned” back to the “caller”:
5.JavaScript Arrow Function
Arrow functions were introduced in ES6. It gets shorter! If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword
notes🤷♂️: This works only if the function has only one statement.
Got any questions or additions? Please let me know.
Thank you for reading :)