Javascript keywords with live examples - Informatic Point

Javascript keywords with live examples

Javascript keywords with live examples. In this tutorial, we will learn about javascript keywords and how they work. JavaScript keywords are reserved words. There are many reserved keywords in Javascript programming which has some significant meaning. These keywords can’t be used as an identifier for example for variable names, for loop labels or for function names, etc in our JavaScript programs.

Let’s dive into JavaScript Keywords:-

JavaScript Keywords Table.

NewContinueDelete
ProtectedImplementsBreak
EvalFunctionArguments
GotoInPublic
ClassForDebugger
ExportThrowSuper
PackageWithInstanceOf
Do ReturnIf
YieldNullWhile
DefaultInterfaceImport
StaticElseLet
ThisExtendSwitch
CaseVoidFalse
TrueFinallyConst
CatchTryEnum
PrivateAwaitVar
TypeOf——————-
Remember Javascript Is a Case Sensitive Language.

Now let’s take a look at live examples of JavaScript Keywords

javascript keywords with live examples.

1. New Keyword

The New keyword is used to create an Object.

<h1>New keyword</h1>
  <script>    
  function myFunc() {
  this.x = 100;
  return 200;  }
  var obj = new myFunc();
  alert(obj.x); 
  </script> 

2. Continue Keyword

Used into a loop to continue the loop and skip the following statements inside the loop.

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
  function myFunction() {
  var text = "";  
  var i = 0;
  while (i < 5) {
    i++;
    if (i === 3) {
      continue;   }
  text += "<br>The number is " + i;  }
  document.getElementById("demo").innerHTML = text;}
</script>

3. Delete Keyword

The Delete keyword are used to remove properties from the object.

<p id="demo"></p>
<script>
var person = {
  firstname:"John",
  lastname:"Doe",
  age:50,
  eyecolor:"blue"};
delete person.age;
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.age + " years old.";
</script>

4. Protected Keyword

An access modifier that can be used with attributes, classes, constructors, and methods which make it not accessible to other classes.

public class Car {
protected String name = "BMW";
}

5. Implements Keyword

The JavaScript Implements keywords are used to implement the interface in a class.

interface car
method drive(whichSpeed)
method break( )
class car1 implements car

6. Break Keyword

The Javascript break keyword is used in a loop to break or stop the execution of the loop.

<button onclick="myFunction()">Run</button>
<p id="demo"></p>
<script>
function myFunction() {
  var y = "";
  var i = 0;
  while (i < 5) {
    y += "<br>The number is " + i;
    i++;
    if (i === 3) {
      break; }}
  document.getElementById("demo").innerHTML = y;
}
</script>

7. Eval Keyword

Eval keyword is Used to evaluate a specified string. The eval used as a global function eval().

function myFunction( ) {
var str1=2;
var str1=3;
var res = eval(new String(str1 + str2));
document.write(res);}
myFunction();

8. Function Keyword

Javascript functions are used to define a function to execute a block of code.

<p id="demo"></p>
<script>
var a = myFunction(3, 3);
document.getElementById("demo").innerHTML = a;
function myFunction(a, b) {
  return a * b;
}
</script>

9. Arguments Keyword

Javascript Arguments keyword is used to represent the list of parameters passed to the function when calling the function.

<p id="demo"></p>
<script>
function myFunction() {
  var i;
  var sum = 0;
  for(i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }  return sum;}
document.getElementById("demo").innerHTML = myFunction(1, 2, 3, 4, 5);
</script>

10. Goto Keyword

The Goto keyword is used to return execution control to a specific location. In general, the goto can be accomplished by the break and continue keywords.

var no=0;
begin
document.write(" something print here ");
no++;
if(no < 10) goto begin;

11. In Keyword

In keyword is an operator returns true if the specified property is present in the specified object, else it returns false.

<p id="demo"></p>
<script>
  var person = {fname:"John", lname:"Doe", age:25}; 
  var text = "";
  var x;
  for (x in person) {
    text += person[x] + " ";  }
  document.getElementById("demo").innerHTML = text;
</script>

12. Public Keyword

Public Keyword is a path modifier that can be used with attributes, classes, constructors, and methods which make it accessible to other classes.

public class Manager {
public String Employee = "Joseph";
public String Employee1 = "Doe";
}
class MainClass {
public static void main(String[] args) {
Manager obj = new Manager ();
System.out.println("Name= " + obj.Employee + " " + obj.lname);
}
}

13. Class Keyword

Class keyword is used to define a class in the program.

public class Employee {
public String efn = "Joseph";
public String eln = "Doe";
}

14. For Keyword

For keyword is used to define a loop, for a loop to repeatedly execute a block of code until a condition is true.

<p id="demo"></p>
<script>
  var text = "";
  var i;
  for (i = 0; i < 10; i++) {
    text += "The number is " + i + "<br>"; }
  document.getElementById("demo").innerHTML = text;
</script>

15. Debugger Keyword

This keyword is used to stop the execution of javascript code and call debugging function if define. Debugger keyword word the same as the break.

<p id="demo"></p>
<script>
var x = 15 * 5;
debugger;
document.getElementById("demo").innerHTML = x;
</script>

16. Export Keyword

This keyword is used to export objects, functions, or values from the module so that can be used in another program with the help of an import statement.

export let var fruits = ["apple", "banana", "guava"];
export const fruit= "apple";

17. Throw Keyword

The Throw Keyword is used in a try block to explicitly throw an exception object.

Var i=1; try {
if(i == "") throw "is Empty";
if(x > 0) throw "positive";
if(x < 0) throw "negative";}
catch(msg) {
message.innerHTML = "Input " + msg;
}

18. Super Keyword

The Super Keyword is used to call the function or method of a parent class.

super.disp(); //the disp is a method of a parent class

19. Package Keyword

The Package Keyword is used to identify java classes and to execute the java method in a javascript.

inBlock['package'] = something;

20. With Keyword

The keyword used for iterating, is just, in short, it is shortened for iteration.

var  fruits = ["apple", "banana", "orange"];
for ( var i = fruits.length; i--; ) {
with ({ no : i }) {
link.onclick = function() {
alert(no);};}}

21. instanceof Keyword

The Instanceof Keyword returns true if the object is an instance of the class otherwise false.

var fruits=["apple", "banana", "orange"]; // Returns true
fruits instanceof Object;   // Returns true
fruits instanceof Array;   // Returns false
fruits instanceof String;

22. Do Keyword

The Do Keyword is used to define a do-while loop.

var a=1;
do {document.write("loop is running for " + a + "times</p>");
a++;}
while(a <= 10);

23. Return Keyword

This Keyword is used to return from the function or method with or without a value.

var func = function(){
return "Hello World";
}

24. If, Else Keyword

The if keyword is used to define a conditioned construct. If statement run when the given condition is true and else statement executes when the condition is false.

<p id="demo"></p>
<script>
  var hour = new Date().getHours(); 
  var greeting;
  if (hour < 18) {
    greeting = "Good day";  } 
  else {
    greeting = "Good evening";  }
  document.getElementById("demo").innerHTML = greeting;
</script>

25. Yield keyword

The Yield keyword is used to pause and resume a generator function. The generator function is the same as a normal function but for returning a value in place of return it uses yield keyword.

function* iter( a ) {
while (a < 4) {
yield a++;}}
const i = iter ( 1 );
console.log(i.next().value);  
console.log(i.next().value);  
console.log(i.next().value);  

26. Null Keyword

The Null Keyword is used to represent a special data type no value.

var age = null;
alert(age);

27. While Keyword

The while keyword is used for the while loop, the while loop executes the block of code until the condition is true.

<p id="demo"></p>
<script>
var text = "";
var i = 0;
while (i < 5) {
  text += "<br>The number is " + i;
  i++;}
document.getElementById("demo").innerHTML = text;
</script>

28. Default Keyword

The default Keyword is used in a switch expression to specify the actions to be performed if no case

var date = new Date();
switch(date.getDay()) {
case 6:
alert("This is weekend.");
break;
case 0:
alert("This is weekend.");
default:
alert("Looking for a weekend.");
break;
}

29. Interface Keyword

This Keyword is used to define an interface (interface contains all abstract methods).

interface car
method drive(whichSpeed)
method break( )
class car1 implements car
{
// Class code here
}

30. Import Keyword

This keyword is used to import the module in the javascript program.

import * as alias from '/modules/mymodule.js';

31. Let Keyword

The let keyword is used to declare a variable limited to a scope of a block of code, unlike a variable declared by the var keyword.

<p id="demo"></p>
<script>
var carName = "Volvo";
myFunction();
function myFunction() {
  document.getElementById("demo").innerHTML = "I can display " + carName;}
</script>

32. This Keyword

This Keyword is used to refer to the current object.

<p id="demo"></p>
<script>
// Create an object:
var person = {
  firstName: "John",
  lastName : "Doe",
  id     : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
// Display data from the object:
document.getElementById("demo").innerHTML = person.fullName();
</script>

33. Const Keyword

This Const Keyword is used to define a constant variable that cannot be further reassigned.

<p id="demo"></p>
<script>
var  x = 10; // Here x is 10
{  const x = 2;  // Here x is 2}
// Here x is 10
document.getElementById("demo").innerHTML = x;
</script>

34. Case Keyword

The Case Keyword is used in a switch-case construct, where the value of an expression compares with the case clause value and executes the statements associated with the case whose case value is matched.

var date = new Date();
switch(date.getDay()) {
case 6:
alert("This is weekend.");
break;
case 0:
alert("This is weekend.");
default:
alert("Looking for a weekend.");
break;
}

That’s It.

Have a nice day.

Leave a Reply

Your email address will not be published. Required fields are marked *