This JavaScript tutorial explains how to use literals (string, number, boolean and null) in JavaScript with examples.
Description
We’ll cover 4 kinds of literals – string literals, quantity literals, boolean literals and null literals.
String Literals
String literals are usually surrounded by single fees (‘) or double fees (“).
For example:
'ABC'
'TechOnTheNet'
"ABC"
"TechOnTheNet"
In JavaScript, you can declare a variable named h and give it the string value of ‘TechOnTheNet’.
var h = 'TechOnTheNet';
or
var h = "TechOnTheNet";
Number Literals
Number literals can be written with or except decimal places. Number literals can be either nice numbers or bad numbers. If you do now not specify a sign, then a fantastic quantity is assumed. Here are some examples of valid quantity literals:
15
3.14
-23
In JavaScript, you can declare a variable named counter and supply it the numeric price of 15.
var counter = 15;
Boolean Literals
Boolean literals can both be genuine or false. These values are exceptional keywords in JavaScript and do not need quotes. Here are the two types of Boolean literals:
true
false
In JavaScript, you can declare a variable named observed and supply it the Boolean value of false.
var found = false;
Null Literals
Null literals are a unique literal price in JavaScript. A null represents the absence of a value. Here is a null literal:
null
In JavaScript, you can declare a variable named h and supply it a fee of null.
var h = null;
Leave a Review