Literals in JavaScript - Informatic Point

Literals in JavaScript

Basically, JavaScript Literals are constant states that can be allocated to the variables that are called literals. JavaScript Literals are syntactic descriptions for various types of information like boolean, numeric, string, array, etc. Literals in JavaScript present the power of displaying particular values in our program.

For Example, var name = “George”, a string variable specified name is represented and specified a string value “George”. The literal “George” represents, the value George for the variable name.

Literals in JavaScript

There are various types of literal that are held by JavaScript.

  • Array Literals.
  • Boolean Literals.
  •  String Literals.
  • Object Literals.

Array Literals.

Array literals are a list of constant values, each of which value is known as an array element. An array literal holds a list of elements inside square brackets ‘[ ]’. If no value is a permit when it performs an empty array with zero length. If elements are omitted then its length is set to the number of elements stated. Examples for string are var color = [ ], var fruits = [“Apple”, “Mango”, “Orange” ,”Banana“]. These four fruits’ names are elements of an Array.

Example:
<p id="soft"></p>
<script>
var fruits, soft;
fruits = ["Banana", "Orange", "Apple", "Mango"];

soft = "<ul>";
fruits.forEach(myFunction);
soft += "</ul>";
document.getElementById("soft").innerHTML = soft;

function myFunction(value) {
  soft += "<li>" + value + "</li>";
} 
</script>

Boolean Literals.

Boolean literals in JavaScript have only two values that are true and false. We use Boolean literals to check the values whether that is true or false.

Example:

<h1>JavaScript Boolean </h1>
<script>
document.write('Boolean(5 > 3) is ' + Boolean(5 > 3));
document.write('<br>');
document.write('Boolean(3 > 5) is ' + Boolean(3 > 5));
</script>

String Literals.

A string literals are a flow of zero or more attributes. A string literals are contained in the single quote or double quote as ( ‘ ) and ( ” ) sequentially and to concatenate two or more string we can use plus (+) operator. Examples for string are “George“, “George Kim“, “786“, “George” + “Kim” etc.

Example:

<h1>JavaScript String </h1>
<b id="3"></b><br>
<b id="4"></b><br>
<script>
var str = "This is first string";
document.getElementById("3").innerHTML = str.length;
</script>
<script>
var str = "This is first string";
document.getElementById("4").innerHTML = str+" This is second string";
</script>                   

Object Literals.

In JavaScript Object literals are group of zero or more key-value set of a comma-divided list, which are close in by a pair of curly braces ‘{ } ‘.Example for object literal with declaration are var kimObject = { }, var student = { f-name : “George”, l-name : “D”, “Kim” : 786, “marks” : 80}.

Example:

<h1>JavaScript Object </h1>
<p id= "1"> </p>
<script>
var student = {f_Name:"George", l_Name:"D", "rno" : 23, "marks" : 80+"%" };
document.getElementById("1").innerHTML =
student.f_Name + " got " + student.marks + " marks.";
</script>
 

Have a nice day.

Leave a Reply

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