Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Javascript. No Comments on Data types and literals in Javascript
0
(0)

In this we will learn about data types and literals in JavaScript along with relevant examples.

 

JavaScript provides five scalar primitive types: Number, Boolean, String, Undefined, Null and two compound primitive types Array and Object.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Even though JavaScript supports object-orientation features, it still supports these scalar primitive data types simply because of performance reasons. Maintenance of primitive values is much faster than objects.

 

JavaScript also provides object versions known as wrapper objects for the primitive types Number, Boolean and String namely, Number, Boolean and String. All primitive values are stored on stack whereas objects are stored in heap.

 

String Data Type

 

A string literal is a string value enclosed in either double quotes or single quotes. Following are some of the examples of string literals:

“Hello World!”

‘Welcome to JavaScript’

“What is your name?”

‘”JavaScript” and “Java” are different programming languages’

 

A string with no character is an empty string. JavaScript does not support character data type. A character value is represented as a string containing a single character.

 

Number Data Type

 

A variable of type number can hold either integer or real numbers. JavaScript does not differentiate between integers and real numbers. All numbers are stored using 64-bit double precision.

 

JavaScript provides a special value NaN (Not A Number) that represents an error condition. Many JavaScript functions return this value, if the argument cannot be converted to a number.

 

Integers

 

Integer literals may be written either in decimal, hexadecimal or octal notation. Examples of decimal integer literals are given below:

2, 100, 0, -1, -200

 

A hexadecimal integer literal starts with “0x” or “0X”. Following are examples of hexadecimal integer literals:

0x4, -0x2, 0xFF, 0X10, 0XA2

 

Octal integer literals start with 0 and may contain digits 0 through 7. Following are examples of octal integer literals:

04, 05, 010, -02, 00, 012

 

Floating point numbers

 

Floating point numbers are written using base 10. They may contain fractional parts separated by a decimal point. Scientific notation is also allowed. Following are some examples of floating point numbers:

.001, 0.01, 3.4, 4, -2, 2e2, 2.3e4, 2e-3, 4.5e-2

 

Boolean Data Type

 

A boolean type variable can hold only two values: true or false. A non-zero value is treated as true and zero is treated as the boolean value false.

 

Comments

 

Every programming language supports comments through which a programmer can provide details like author name, creation date and purpose of the script. Apart from these details, comments allow programmers to write explanations or reviews about their script or elements in the script.

 

A single line comment in JavaScript starts with //. Everything followed by // in that line is treated as a comment.

 

A single line or multi-line comment starts with /* and ends with */. Everything in between these two markers is treated as a comment and will be ignored by the JavaScript interpreter.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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