JavaScript For Intermediate with JS Short note Part-1

JavaScript For Intermediate with JS Short note part-1
Table Of Contents

What is JavaScript?

javascript
JavaScript For Intermediate with JS Short note

Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side scripts to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities.

On-Page Script

Adding internal JavaScript to HTML.

<script type="text/javascript"> //Write your js code here </script>

External JS File
Adding external JavaScript to HTML

<script src="filename.js"></script>

Function
A function is a group of reusable code that can be called anywhere in your program. This eliminates the need of writing the same code again and again. JavaScript Function syntax

You may also like:

function nameOfFunction () {
//Write function content here
}

DOM Element
Changing content of a DOM Element.

document.getElementById("elementID").innerHTML = "Hello World!";

Output on the Console
This will print the value of an in JavaScript console or terminal

console.log(x);

Conditional Statements
Conditional statements are used to perform operations based on some conditions.

if (condition) {
//block of code to be executed if the condition is true.
//This code exrcute when the given condition is true.
}

If-else Statement
Use the else statement to specify a block of code to be executed if the condition is false.

if (condition) {
  //  block of code to be executed if the condition is true
} else {
  //  block of code to be executed if the condition is false
}

The else if Statement
Use the else if statement to specify a new condition if the first condition is false.

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}

Switch Statement
Use the switch statement to select one of many code blocks to be executed.

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each time with a different value.

For Loop
The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

While Loop
while loop runs on a specific condition if the condition is true.

while (condition) {
  // code block to be executed
}

Do While Loop
The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

do {
  // code block to be executed
}
while (condition);

Strings
JavaScript strings are for storing and manipulating text.

charAt method
Returns the character from the specified index.

str.charAt(3)

concat method
Joins two or more strings together.

str1.concat(str2)

index of method
Returns the index of the first occurrence of the specified character from the string else -1 if not found

//index of method
syntex--->str.indexOf('substr')

//match method->Searches a string for a match against a regular expression.
syntex--->str.match(/(chapter \d+(\.\d)*)/i;)

//replace method->Searches a string for a match against a specified string or char and returns a new string by replacing the specified values.
syntex--->str1.replace(str2)

//search method->Searches a string against a specified value.
syntex--->str.search('term')

//split method->Splits a string into an array consisting of substrings.
syntex--->str.split('\n')

//substring method->Returns a substring of a string containing characters from the specified indices.
syntex--->str.substring(0,5)

Arrays
The array is a collection of data items of the same type. In simple terms, it is a variable that contains multiple values.

variable
Containers for storing data.

var fruit = ["element1", "element2", "element3"];

Array methods

//concat method->Joins two or more arrays together.
syntex--->concat()

//indexOf method->Returns the index of the specified item from the array.
syntex--->indexOf()

//join method->Converts the array elements to a string.
syntex--->join()

//pop method->Deletes the last element of the array.
syntex--->pop()

//reverse method->This method reverses the order of the array elements.
syntex--->reverse()

//sort method->Sorts the array elements in a specified manner.
syntex--->sort()

//toString method->Converts the array elements to a string.
syntex--->toString()

//valueOf method->returns the relevant Number Object holding the value of the argument passed.
syntex--->valueOf()

This is part 1 of the javascript of the intermediate tutorial. This tutorial is continued in part 2.

Total
14
Shares

Leave a Reply

Related Posts