- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript Data Types
JavaScript's versatility in handling various data formats is a key feature that contributes to its power. JavaScript allows programmers to create sophisticated applications and interactive websites because it supports a wide variety of data types, from strings and numbers to arrays and objects.
Because JavaScript is a dynamically typed language, we don't need to declare the type of a variable before using it, which is one of its great features. As a result, whatever value was assigned to a variable, the variable's type changed as a result of the assigned value.
Variables in JavaScript can be given values of various data types at various points while a program is being executed. A variable might be given a string value at one point and a numeric value at another. Because of its adaptability, JavaScript is a strong and useful language, but it can also cause unexpected behavior if not used carefully.
Primitive data types in JavaScript are the most fundamental data types and include:
- Numbers: Numerical values such as integers, decimals, and even NaN (Not-a-Number) are stored in numbers.
- Strings: Text values are stored in strings, which can contain single or double quotes.
- Booleans: Booleans are used to store values such as true or false.
- Undefined: Variables that have been declared but not yet given a value are referred to as undefined.
- Null: When a variable has been explicitly given the value "null," it denotes intentional absence of any object value, but it is still a value.
JavaScript supports reference data types in addition to primitive data types, such as:
- Objects: Collections of key-value pairs are kept in objects.
- Arrays: Arrays are used to store groups of data in a predetermined order.
- Functions: Functions are used to store chunks of code that can be run repeatedly.
For example:
x = "codescracker"; // x is of string type x = 120; // now x is of number type x = true; // now x is of boolean type
See the above example; there is no need to declare the type of a variable before initializing any value of that type.
JavaScript typeof: find the data type of a variable or value
The JavaScript typeof operator is used to find the type of value or variable. For example:
<!DOCTYPE html> <html> <body> <p id="para1"></p> <p id="para2"></p> <p id="para3"></p> <p id="para4"></p> <p id="para5"></p> <script> document.getElementById("para1").innerHTML = typeof "codescracker"; document.getElementById("para2").innerHTML = typeof 10; document.getElementById("para3").innerHTML = typeof true; document.getElementById("para4").innerHTML = typeof [1, 2, 3, 4, 5]; document.getElementById("para5").innerHTML = typeof function () {}; </script> </body> </html>
JavaScript allows three keywords to declare a variable: var, let, and const.
Consider the following code as another example demonstrating the "typeof" operator in JavaScript:
<!DOCTYPE html> <html> <body> <p id="para1"></p> <p id="para2"></p> <script> let x = 10; document.getElementById("para1").innerHTML = typeof x; document.getElementById("para2").innerHTML = typeof "codescracker"; </script> </body> </html>
The syntax of the typeof operator in JavaScript is:
typeof x
x refers to the variable or value whose type we need to find.
« Previous Tutorial Next Tutorial »