Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
New | Continue | Delete |
Protected | Implements | Break |
Eval | Function | Arguments |
Goto | In | Public |
Class | For | Debugger |
Export | Throw | Super |
Package | With | InstanceOf |
Do | Return | If |
Yield | Null | While |
Default | Interface | Import |
Static | Else | Let |
This | Extend | Switch |
Case | Void | False |
True | Finally | Const |
Catch | Try | Enum |
Private | Await | Var |
TypeOf | ——— | ———- |
Now let’s take a look at live examples of JavaScript Keywords
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>
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>
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>
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"; }
The JavaScript Implements keywords are used to implement the interface in a class.
interface car method drive(whichSpeed) method break( ) class car1 implements car
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>
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();
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>
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>
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;
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>
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); } }
Class keyword is used to define a class in the program.
public class Employee { public String efn = "Joseph"; public String eln = "Doe"; }
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>
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>
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";
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; }
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
The Package Keyword is used to identify java classes and to execute the java method in a javascript.
inBlock['package'] = something;
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);};}}
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;
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);
This Keyword is used to return from the function or method with or without a value.
var func = function(){ return "Hello World"; }
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>
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);
The Null Keyword is used to represent a special data type no value.
var age = null; alert(age);
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>
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; }
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 }
This keyword is used to import the module in the javascript program.
import * as alias from '/modules/mymodule.js';
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>
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>
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>
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.