‘const‘ keyword is for creating a read only variable, something you can never change once created.
‘const‘ likes ‘let‘ keyword alos has block scope.
describe("using const", function(){ it("will make a variable read-only", function(){ const MAX_SIZE = 10; //MAX_SIZE = 12; //SyntaxError expect(MAX_SIZE).toBe(10); //true }); it("can shadow outer declaration", function(){ const x = 12; var doWork = function(){ let x = 10; return x; }; var result = doWork(); expect(result).toBe(10); //true expect(x).toBe(12); //true }); it("const also has block scope", function(){ if(true){ const x = 12; //SyntaxError, x is not defined } var doWork = function(){ let x = 10; return x; }; var result = doWork(); expect(result).toBe(10); //true expect(x).toBe(12); //true }); });
时间: 2024-10-10 17:27:36