8 Questions
Which keyword is used to declare a constant in JavaScript?
const
What happens when you try to assign a new value to a constant in JavaScript?
It throws a TypeError
In JavaScript, which data type declaration can not be changed once initialized?
'const' variables
What will be the output of console.log(typeof(str)) after executing the given code snippet?
'string'
Which method is used for explicit type conversion from number to string in JavaScript?
.String()
What error will occur if you try to declare a 'const' without initializing it?
SyntaxError: Missing initializer
What would be the outcome of executing the function fun() defined in the text?
'testing' and 1.1 printed on separate lines
How does JavaScript handle variable declarations without 'let' or 'var'?
Automatically assigns them as global variables
Study Notes
Type Conversion: Explicit
- Number to string:
- Using global method
String()
:console.log(String(num))
- Concatenating number with an empty string:
let str = "" + num
- Using
toString()
:let num = 1; let str = num.toString();
- Using
toString(base)
:let num = 10; let str = num.toString(8);
- Using global method
String to a Number
- Using unary
+
operator:let str = "1.11"; let num = +str;
- Using
parseInt()
:let str = "1.11"; let num = parseInt(str);
- Using
parseFloat()
:let str = "1.0px"; let num = parseFloat(str);
Float to an Integer
- Using
parseInt()
:let flt = 1.11; let num = parseInt(flt);
- Using
Math.round()
:let flt = 1.11; let num = Math.round(flt);
- Using
Math.ceil()
:let flt = 1.11; let num = Math.ceil(flt);
- Using
Math.floor()
:let flt = 1.11; let num = Math.floor(flt);
Variables and Constants
-
let
andvar
keywords are used to declare variables -
const
keyword is used to declare constants - Constants cannot be reassigned, and must be initialized during declaration
- The value of a constant cannot be changed, but the content of the value can be changed
Comments
- Comments are similar to those in C, C++, and Java
- Comments can be written between
//
and the end of the line - Java-style comments (`/** */) are treated as regular comments in JavaScript
Test your knowledge on JavaScript type conversion with this quiz covering topics such as converting numbers to strings, using different bases with toString, and converting strings to numbers using in-built functions like unary + operator and parseInt.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free