Latest posts

10/recent/ticker-posts

Javascript Keywords | Keywords in JavaScript

 JavaScript Keywords

JavaScript is a programming language that is widely used to create interactive and dynamic websites.
It allows developers to add various features such as form validation, image sliders, and dynamic content.
In this blog post, we will disscussing some of the most important JavaScript keywords that you can use to enhance the functionality of your website.


What is a Keyword ?

In JavaScript, a keyword is a special word that has a special meaning in the language.
Keyword are used to identify and perform specified actions in the code, such as declaring variables, creating loops, and making decisions.
Keyword cannot be used as variable names or identifiers.

Variable Declaration Keywords

"var", "let", and "const" are used to declare variables in JavaScript.
"var" is the oldest keywoed and is used to declare bariables that can be reassigned and hoisted.
"let" and "const" are newer keywords and are used dclare variables that cannot be reassigned.
"let" is used to declare variables that can be reassigned while "const" is used to declare variables that cannot be reassigned or modified.

Check out our tutorial on "Variable declaration in JavaScript" for more information.

Function Declaration keywords

"function" is used to declare a Function in JavaScript.
Functions are blocks of code that can be reused multiple times.
Functions can accepts parameters and returns a value. This keyword is essential for creating reusable code and implementing logic in your website.
It is a reusable block of code that can be called by name.

A function can take in input (parameters) and return output (return value).
It can be declared in several ways:
1) Function Declaration,
2) Function Expression,
3) Arrow Function Expression

Check out our tutorial on "Functions in JavaScript", to learn more about Functions.
 function name(parameter1, parameter2, parameter3)
 {
  // code to be executed
 }

Kewords used in Conditional Statements

"if". "else" and "else if" are used to create conditional statements.
These keywords are used to execute different code based on certain conditions.
They allow you to make decisions in your code based on user input or other factors.
 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
 }

In jacascript, conditional statements are used to make secisions in code based on certain conditions.
The following keywords are commenly used in conditional statements :

1) "if" - The if statement is used to execute a block of code if a certain condition is true.
2) "else" - The else statements is used in conjunction with the if stateemnt to execute a block of code if the condition is false.
3) "else if" - The else if statement allows multiple conditions to be tested in a single if-else statement.
4) "switch" - The switch statement is used to perform different actions based on different conditions.
5) "case" - The case statement is used in conjunction with the switch statement to specify a condition to be tested.
6) "break" - The break statement is used inside a switch statement to break out of the switch block.
7) "default" - The default statement is used inside a switch statement to specify a block of code to be executed if none of the case condition are met.

Keywords used in Looping Statements.

"for" and "while" are used to create loops.
Loops are used to repeat a block of code multiple times. "for" loops are used to iterate through arrays and "while" loops are used to repeat a block of code as long as a certain condition is true.
 for (expression 1; expression 2; expression 3)
 {
  // code block to be executed
 }

In Javascript, looping statements are used to repeatedly execute a block of code until a certain condition in met.
The following keywords are commonly used in looping statements :

1) "for" - The for loop is used to repeatedly execute a block of code specified number of times.
2) "for...in" - The for...in loop is used to iterate over the properties of an object.
3) "for...of" - The for...of loop is used to iterate over the values of an iterable object, such as an array or a string.
4) "while" - The while loop is used to repeatedly execute a block of code as long as a certain condition is true.
5) "do...while" - The do...while loop is similar to the while loop, but the block of code will always be executed at least once.

"document.getElementById" keyword

The "document.getElementById" keyword in JavaScript is used to access an element on a webpage by its unique id.
The Id attribute of an HTML element is used to uniquely identify the element on the page.

The syntax for Using "document.getElementById" is as follows :
 document.getElementById(id)
Where "id" is a string containing the id of the element you want to access.

"this" Keyword

The "this" keyword in JavaScript refers to the current context or object that the code is being executed on.
The value of "this" can change depending on the context in which it is used.

In the global scope, "this" refers to the global object (i.e., window in the browser)
In the context of a function, "this" refers to this object that the function is a method of. For example, if a function is a method of an object, "this" inside the function will refer to that object.

Example : 
  <h3>Your FullName is : <span id="fname"></span> </h3>

 <script>
    const person = {
        firstName : prompt("Enter Your First Name !"),
        lastName : prompt("Enter Your Last Name !"),
        fullName : function () {
            return this.firstName + " " + this.lastName;
        }
    };

    document.getElementById("fname").innerHTML = person.fullName();
 </script>


Hope it will help you to learn JavaScript More...

Thank You..!!




Also Visit


Post a Comment

0 Comments