Getting Started with JavaScript - 1

Getting Started with JavaScript - 1

Under this will go through the basics of Javascript.

What is JavaScript?

Javascript is well known as the programming language for the Web. It's a client-side scripting language.

What is its function?

Basically, javascript functions when some action is performed by the client. The actions can be hovering over using a mouse, clicking on something, scrolling, anything based on what's coded.

Syntax?

<script type = “text/javascript”> 
// Enter the code here.
</script>
   // Linking an external javascript file.
  <script src="example.js"></script>

Now let's dive into Javascript basics.

- Comments

// Single line comments are typed here
/* Multi line comments are typed in this. 
    Comments are used to explain the code, 
    Basically, to make the code understandable to the reader.*/

- Variables

These hold values and are used to perform different operations.

3 Types:

1) var - Frequently used. Values can be assigned can be modified over the operations and working. Works only in the functions its declared in.

var example = "value";

2) const - These values can't be modified again and again. The values assigned in the start remains unchanged until the end.

const example = "value";

3) let - Values assigned can change, works only inside the particular block.

let example = "value";

- Datatypes

Datatype Example Syntax
Number 300.3 var roll_no = 10;
Null null var example = null;
Undefined undefined var example; typeof example;
String "Hello" var text = "Hello";
Boolean True/False var answer = false;
Symbol Giovanni Rovelli var example = /()/;

- Objects

It's another type of data-type which stores in key-value pair format.

var student = {
// Here, name,roll,dob are the keys. 
// Opposite to them are their respective values.
  name : Bhumi Khokhani,
  roll : 10,
  dob : "2000-03-27",
  age : function() {
  // Code written for the function
  }; //Method
}

- Functions

Set of code written to perform a specific task. It runs when it's called. Having functions also increases the reusability of code.

Two steps to create as function:

1) Define it

2) Call it

//Defining the function.
function function_name ( parameters ) {
//Block of Code
var abc=parameters;
return abc; //returns this value at the end of the function. It's optional.
}
function_name(parameters); //Calling the function.

- Arrays

A group of similar datatypes is called an array. We can access by referring to the index number.

Syntax

// First way of defining an array
datatype array_name=["value1", "value2", "value3", ...];
//Second way of defining an array
datatype array_name = [];
array_name[0] = "value1";
array_name[1] = "value2";
array_name[2] = "value3";
//Third way of defining an array
datatype array_name = new Array("value1", "value2", "value3");
// Accessing the array elements
datatype variable_name = array_name[index_value];
// Altering a value of array
array_name[index_value] ="new value";
// Printing the array elements
for (var i = 0; i < array_name.length; i++) {
     console.log(array_name[i]);
}

Functions on Array

Function Definition Example
concat() Used for concatenating arrays. array_name.concat(arr1, arr2);
join() Joining all elements in the particular array giving back a string. array_name.join("*");
indexof() Returns the first position (index) of an array. array_name.indexof();
lastindexof() Returns the last position (index) of an array. array_name.lastindexof();
sort() Sorting of array elements in alphabetical order. array_name.sort();
reverse() Sorting of array elements in reverse alphabetical order. array_name.reverse();
shift() Popping of first element, and returning the value of the popped element. array_name.shift();
unshift() Pushing of element in the beginning of the array. array_name.unshift("value");
splice() Used for adding items to te array, it returns the deleted items of the array. // pushing index; no_of items to be deleted; item value array_name.splice(index1, no._of "values");
slice() Derives a new array form the existing array. Slicing doesn't affect the already existing array. // index1 slicing begins, index2 slicing end. array_name.slice(index1, index2);
toString() Converts the array separated by commas to a single string. array_name.toString();
push() Pushing an item to the end of the array. array_name.push(item);
pop() Popping an item from the end of the array. array_name.pop();

- Operators

1) Basic Operators

Operator Definition Example
+ Addition. var1 = var2 + var3;
- Subtraction. var1 = var2 - var3;
* Multiplication. var1 = var2 * var3;
/ Division. var1 = var2 / var3;
% Modulus. var1 = var2 % var3;
++ Increment. var1 ++;
-- Decrement. var1 --;

2) Bitwise Operator

Operator Definition Example
& AND var1 & var2
| OR var1 | var2
~ NOT. ~ var1
^ XOR var1 ^ var2
<< Left Shift var1 << var2
>> Right Shift var1>>var2

3) Comparison Operator

Operator Definition Example
== Equals to var1 == var2
=== Equal to or type var1 === var2
!= Not equal to var1 != var2
!== Not equal to or type var1 !== var2
< Less than var1 < var2
> Greater than var1>var2
? Ternary Operator var1 ? var2
<= Less than or equal to var1<=var2
>= Greater than or equal to var1>=var2

4) Logical Operators

Operator Definition Example
&& Logical AND var1 && var2
|| Logical OR var1 || var2
! Logical NOT !(var1 == var2)

Yet, Javascript consists of many different attributes, functions, methods etc., which I will cover in the next blog.


Wrapping Up

Hope this article gave you some basic knowledge on Javascript and it's working. Feel free to put up any questions if you have/had. I will try my best to answer them.

Also, you can leave you suggestions in the comment section and give a reaction if you enjoyed reading it 💖
Feel free to connect with me on LinkedIn | Twitter

If you like my work, you can extend your support by buying me a ☕. Thank you!

Did you find this article valuable?

Support Bhumi Khokhani by becoming a sponsor. Any amount is appreciated!